MacOS – How to ask for input and then pass the variable in terminal

macosterminal

I'm currently trying to do something quite odd. I want to automate updates on multiple computers in my office and first I need to open a program and said program has two fields; one for username and another for a password.

Obviously walking around the office to 50 computers and inputting my user and password is quite annoying so what I'd rather do is be able to assign my username and password to variables and then pass those variables to the username and password fields in the program.

So far I can open the program via the remote desktop app on all the machines and I can use osascript to send keystrokes but I want terminal to ask me for the username and password so that I don't save them into my script. Is there an easy way to do this and then tell those variables that I save my pass and username into to be the keystrokes? Thanks!!

Best Answer

These lines will prompt for your username and password:

echo -n 'Username: '
read username

echo -n 'Password: '
read -s password #The -s means silent, so your password is not shown in the terminal.

You can then use the variables $username and $password in the rest of your script, like this:

osascript -e 'tell app "System Events" to keystroke "'"$username"'" & tab & "'"$password"'"'