There is actually a much simpler way to do this. For portability, this is my implementation but feel free to manipulate it to suit your need.
Enter your sudo password as a parameter when starting the script, capture it, and echo it with each command which will prompt for the sudo password.
#!/bin/bash
PW=$1
echo $PW | ./playback_delete_data_patch.sh 09_delete_old_data_p.sql
./command_wo_sudo.sh <param>
echo $PW | ./other_command_requires_sudo.sh <param>
You can add a prompt and capture after the script is kicked off like so:
echo "enter the sudo password, please"
read PW
But if someone else monitors what's run on the node; has access to logs created by it; or is just looking over your should randomly when you run a test, that could compromise security.
This also works with running commands/scripts that require a yes to continue:
echo $PW | yes | ./install.sh
The echo is in response to a prompt, so you can use anything you need to, there, if you're running other scripts that have prompts for progress, in sequential order. Make sure you know that order, though, or bad things can happen.
sudo
password - the script won't be able to run fully if someone is not there to enter it.sudoers
setup . No big deal. It just needs fixed.sudo
) does affect the environment (i.e. available variables), though. This is important in some cases, for example withgsettings
.