Can we use heredocs to run multiple commands using sudo?
I am facing an issue (need to pass password for every command) while running multiple commands:
echo 'password'| sudo -S ls
echo 'password'| sudo -S cat /var/log/abc.log
Can anyone help me how to automate this in a script? Like:
echo 'password' | sudo -S << ENDBLOCK
ls
cat
ENDBLOCK
sudo
will remember the password used for a short period (5 minutes by default), so there should be no need to supply the password to the second call if the first doesn't take long to run. However, a better solution is to configuresudo
to allow your specific commands to be run without supplying a password, which keeps the password out of the script altogether. – Fulviah