Bearett
Bearett

Reputation: 3

Two possible Sudo passwords in shell script

I have a problem with Mac computers being deployed with two possible root admin passwords. I'm trying to create a shell script to uninstall an application, and need to inject both passwords. If "password 1" is correct, the script finishes without any errors. This is where I am now. Any suggestions?

echo "password 1" | sudo "command"
if [ $? –ne 0 ]; then
## last command didn’t work, let’s try another password
  echo "password 2" | sudo "command"
fi

output

RT20787-MAC:desktop tsstech$ sh Uninstall_script.sh 
Password:Sorry, try again.
Password:
sudo: 1 incorrect password attempt
Uninstall_script.sh: line 21: [: –ne: binary operator expected
Password:

Upvotes: 0

Views: 871

Answers (2)

meuh
meuh

Reputation: 12255

Just provide both passwords to the one sudo. My sudo needs the option -S to read passwords from stdin.

echo -e "password 1\npassword 2" | sudo -S "command"

This assumes your "command" doesnt need to read from stdin, but see also the comments below.

Upvotes: 2

Pico12
Pico12

Reputation: 1279

There is a problem in your test :

if [ $? –ne 0 ] #–ne: binary operator expected

Add quote to $? :

if [ "$?" -ne 0 ]

or use the != operator :

if [ $? != 0 ]

Upvotes: 1

Related Questions