How to execute multiple commands using sudo in script

Is it possible to use heredocs to run multiple commands using sudo?

I ran into a problem (you need to pass a password for each command) while running several commands:

echo 'password'| sudo -S ls
echo 'password'| sudo -S cat /var/log/abc.log

Can someone help me how to automate this in a script? For instance:

echo 'password' | sudo -S << ENDBLOCK
ls
cat
ENDBLOCK
+4
source share
3 answers

you can run sh -c ..., but do not forget to specify correctly.

sudo sh -c 'id; echo another command ; id'

sudoshould see this as the only argument for the team sh.

Of course, you can use a new line instead of a semicolon:

sudo sh -c '
  echo "I am root"
  id
  echo "another command"
  id
'
+3
source

- script , sudo, script sudo.

+2

script.

  • sudo./ script.sh
  • set permissions for script.sh in /etc/sudoers.d; this way you will no longer need to enter a password (for this script)
+1
source

Source: https://habr.com/ru/post/1588933/


All Articles