Ubuntu – What other bash variables are available during execution such as $USER that can assist on the script

bashenvironment-variablesscripts

This is related to question Why is this bypassing the SUDO password?. In that one of the responders answered the question in an awesome way, and very, very clear to any newbie. Now here is a question that I can't seem to figure.

I wrote a script for starting the vmware firefox plugin (don't worry. I gave that up and now run vBox very happily. I left vmware for my servers 🙂 )

I needed to start the plugin as sudo, but I also needed to pass an argument (password) to it, that happened to be the same.

So, if my password was Hello123, the command would be: sudo ./myscript.sh hi other Hello123

Running from the command line, the script would ask for my sudo password and then run. I wanted to capture that password and pass it as well. I also wanted to run graphically, so I tried gksudo, and there is an option -p that returns the password for variable assignment.

Well, that was a nightmare because I would still get prompted for the original sudo: see below

Find UserName

vUser=$USER
Find password (and hopefully enable sudo)

vP=gksudo -p -D somedescriptiontext echo
Execute command

gksudo ./myscript.sh hi $vUser $vP

And I still get prompted twice.

So my question is tri-fold:

  1. Is there a variable I can use for the password, just like there is one for user, $USER?

  2. Is there a different way I should be assigning the value resulting of the command I have in $vP? I am wondering if executing the way I have it, does it in an uninitiated session and not the current one, since I am getting some additional warning type errors on some variables.

  3. I tried using Zenity to just capture the text, but then of course, I couldn't pass that value to sudo, so I could only use it as a parameter, which puts me back in 2 prompts.

Best Answer

You can assign certain commands to run without having to provide a password with sudo (man visudo). For example, you might have a line like this, if your username is psusi:

%psusi ALL = NOPASSWD: /usr/local/bin/myscript.sh

Use extreme caution with granting all-trusting sudo access.

Related Question