i want to write script inside the script i will do many commands that require to be root like
sudo apt-get update
sudo apt-get upgrade
sudo apt-get install eclipse
but i want to write the password just one time
i tried to write in top
sudo su
to run as root but it stop the script so what i can so to write the password
The obvious solution: run the script as root
Then all commands in the script will be run as root.
If some commands don't need root, just use
sudo
to change back to a normal userIf 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:
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 Isudo next_command
. A simplistic example would be creating an Excel file and wanting to manipulate it with another process. Even running withsudo
, 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.