I'm trying to make a shell script which asks the user in the beginning for a password and then stores it in a variable so it may be used each time the script requires root access.
My thought was to do something like this:
read -s "Please enter your password: " PASSWORD
echo -e $PASSWORD | su -c 'do some root action here'
But that is not working. How could this be accomplished?
EDIT Ok so I've started to investigate expect and this is what I've got so far:
#!/usr/bin/env bash
user=$(whoami)
echo "Pleas enter your root password. [Followed by Enter]"
read -s "Password:" PASSWORD
/usr/bin/expect <<EOD
spawn su -c 'echo \"$user ALL=(ALL) ALL\" >> /etc/sudoers'
expect "Password:"
send $PASSWORD
interact
EOD
So this isn't working but the output I get from the terminal looks like I'm getting closer to my goal.
Is anyone able to help me solve this?
Thank you.