I have created a bash script to run a few js scripts into MongoDB. Basically, what I am doing is, running the bash script and passing a parameter in this case called version, example:
./script.sh 1.0
That will go and execute all scripts for the version 1.0. Now, it is possible that MongoDB requires authentication user/pass, so I have an option in the script execution that will ask the user if it requires authentication.
read -p "Username: " mongo_user; read -s -p "Password: " mongo_pass;
My question is: what would be the best way to kind cache the same credentials to call the script multiple times? For example:
./script.sh 1.0 ./script.sh 1.1 ./script.sh 1.2 and on..
I don’t want to type in the same credentials every time the script runs.
Advertisement
Answer
Caio,
As stated in my comment here’s how I did it:
Thanks for Charles Duffy for the printf
solution:
#!/bin/bash ePass() { read -sp "Password: " pass echo "" printf '%sn' "$pass" | perl -e 'chomp($passwd=<>); chomp($encoded=pack("u",$passwd));print "$encodedn"' > .pswd cat .pswd } dPass() { dPass=`cat .pswd | perl -e 'chomp($encoded=<>); chomp($passwd=unpack("u",$encoded)); print "$passwdn"'` echo $dPass } ePass dPass
You can add these functions to your script. When you want to set the password ePass will do:
[KUBO@home ~]$ ./test.sh Password:
It will mask the input to avoid over-the-shoulder reading. Then it will echo the encoded output (remove after testing):
Password: Hello >>> %2&5L;&`
Then you dPass:
Hello
So when you call your mongo scripts you can use the dPass output as your arg.