I have a C project consisting of about 1350 C-files which need to be compiled and am using the following script for that (edited for brevity):
echo -n Password:
read -s password
echo
./configure
make
make check
sudo make install $password
Because of its size, when the script has reached the final make install step, it has already forgotten the password, at which point my script stalls. Currently I have two workarounds: 1: skip test file compilation and 2: execute make check after make install.
Is there a more structural solution such that my script remembers the password for a little longer?
scripts don't usually suffer from amnesia - something else is wrong.
First, instead of:
echo -n Password:
read -s password
do
read -s -p 'Password: ' password
Secondly,
does your make install step take an optional $password parameter? I doubt it.
Are you trying to pass $password to sudo? What's the intent of:
sudo make install $password
Put set -x at the top of your script (after the she-bang - e.g. #!/bin/bash) and see what happens.
You can comment out ALL lines, but the last sudo line.
Once authorized, the script will have password in runtime.
Be aware tho, any method of examining the process would reveal such passwords baked into environment variables, likes of : cat /proc/<PID of program>/environ
For anything in your script that needs to run as sudo, you just use the plain command, as the whole script is already at the extended privileges level.
You only need to sudo -u myUser to user level for commands that need to use that user's permissions: for example, so the results of your compile are owned by that user, not by root.
The additional benefit is that you can apply the -u to different users for each such command, if you need to.