If you have commands that don't require sudo
, you might check out my answer on this question.
For brevity, though, here is a quick answer:
#!/bin/bash
PW=$1
echo $PW | apt-get update
echo $PW | apt-get upgrade
echo $PW | apt-get install eclipse
Run with bash file_name.bash sudo_pw
The reason for passing it this way is people can't necessarily tell what the parameter you're passing means, so even if someone monitors in some way (even looking over your shoulder), it's still secure.
The primary reason I do this is because most of the commands I run with sudo create objects that need to be manipulated later on, and using sudo command
will usually make them inaccessible, even if I sudo next_command
. A simplistic example would be creating an Excel file and wanting to manipulate it with another process. Even running with sudo
, most of the time, I can't access it again without overwriting it, which then recreates it as an inaccessible object.
Note: This also works as a shell script as there is nothing BASH specific in the syntax, so you could use #!/bin/sh
and ./file_name.sh sudo_pw
instead, if that is your preference.
sudo
) during execution if multiple commands will need it. Do all the commands need to be run as superuser? – Thomas Ward Jan 26 '14 at 00:59