Background
I have a long bash script which setup a large environment of interconnected software, taking several hours to complete. A few of the tasks it performs need to be run as root, for which I use sudo .... The whole process is then paused until the user notices and types in the root password. I seek some way for the user to type in the root password only at the beginning of the script, and then automatically supply it when required by sudo later.
My thoughts on possible (bad) solutions
I could store the password directly in a variable and then supply it using
echo "${root_password}" | sudo -S ...
but something tells me that this is bad practice.
Another workaround would be to force the user to run the entire script as root, but wouldn't that lead to different permissions for all of the files generated without the use of sudo?
sudoinside the script as well to switch back to the original user for the commands that, for example, should create a non-root owned file. You are correct that storing the root password is a bad idea. Another possibility is to configuresudoto allow the user to run your script (or just the commands inside your script that require root permissions) without a password. A lot of this just requires careful thought about how the script is designed.sudo?sudoto change back to the user from the root?sudo -u <origuser> ...inside the script. However, I thought there was a way to get the original user's identity from inside the script, and that assumption appears to be mistaken. The only answer I could give right now is to invoke the script using something likesudo script "$USER" ..., so that the script has the identity of the user it should downgrade as an argument.sudo script $USERsignature, if called without arguments. The real problem is that I not have to prependsudo -u <origuser>to every command (except the few that requires root privileges)! I have tried placing a bunch of commands in a subshell or a function, but thensudowon't accept it...