Reading variable from another file into bash
I'm not sure of everything that's happening, and what is supposed to be happening, but when you say
export snapshot_pw=snp_pw
you are setting the environment variable snapshot_pw
to the string s
n
p
_
p
w
.
You probably want to do
export snapshot_pw="$snp_pw"
to set the environment variable snapshot_pw
to the value of variable snp_pw
, i.e., $snp_pw
.
Also, you should quote the shell variable "$mail_log"
and any others you use without quoting.
(You don't need the {
and }
.)
Related videos on Youtube
saurav
Updated on September 18, 2022Comments
-
saurav almost 2 years
I have written a bash script which calls other process (the ones that ask for password on terminal).
#!/bin/bash source pc.txt snp_pw=$export_snapshot export snapshot_pw=snp_pw echo_time() { date "+%d:%b:%Y:%H:%M:%S" } # Export Snapshot echo "$(echo_time) :STARTING EXPORT SNAPPSHOT SCRIPT" | tee -a ${mail_log} loop_var=3 echo "$(echo_time) :ON FAILURE TRY AT MAX ${loop_var} ATTEMPTS" | tee -a ${mail_log} i=1 while [ "${loop_var}" -gt 0 ]; do expect <(cat << 'EOD' spawn $::env(Snp_Script_Path)/export_service_instance.sh bootstrap Export_Files/test.bar expect "Enter RPD Password:" send -- "$::env(snapshot_pw)\r" expect "Re-enter RPD Password:" send -- "$::env(snapshot_pw)\r" interact EOD ) &> ${export_snapshot_log_location}
Instead of directly hardcoding the password here, how can I read it from within a variable in other file?
It has to be a bash and expect both. I am currently trying to read the file through source and $pass gives password. But within expect, it is not working.
I don't want to encrypt/encode anything. I just want to keep it simple.
-
ctrl-alt-delor over 5 yearsPlease show what you have tried. I can imagine a few errors that you could have made. But as I can not see what you did, I do not know.
-
saurav over 5 yearsFirst I am reading a text file(It has got pwd=value in it) using source source passcode.txt pass=$pwd and then within expect this pass variable I am unable to read
-
ctrl-alt-delor over 5 yearsshow me the code, (edit question).
-
saurav over 5 yearsI have edited and added some snippet
-
RalfFriedl over 5 yearsAnd which part of the code you added is the minimum to show your problem?
-
saurav over 5 yearssend -- "$::env(snapshot_pw)\r" -> This ideally reads bash variables. But in this case it is not reading.. This is what i want to reslove
-
Mario Orlandi over 5 yearstake a look at sexpect (Expect for Shells) with which you can write Expect scripts with shell code only.
-
-
Angel Todorov over 5 yearsWith bash, you can avoid
cat
using:snp_pw=$(<passwd_file)
-- ref gnu.org/software/bash/manual/bash.html#Command-Substitution -
Angel Todorov over 5 yearsAlso, if you do this, you'll want to make the password file hidden and unreadable by others:
mv passwd_file .passwd_file; chmod 600 .passwd_file