1

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.

  • 1
    Please 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. – ctrl-alt-delor Dec 4 '18 at 22:36
  • First 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 – saurav Dec 4 '18 at 22:39
  • 2
    show me the code, (edit question). – ctrl-alt-delor Dec 4 '18 at 22:39
  • I have edited and added some snippet – saurav Dec 4 '18 at 22:45
  • And which part of the code you added is the minimum to show your problem? – RalfFriedl Dec 4 '18 at 23:00
0

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 snp_pw.  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 }.)

0

A minimal test case shows that both bash and expect do read the same environment variable:

#!/bin/bash
export foo="bar$$"
echo "bash $foo"

expect <(cat <<'EOD'
puts "tcl  $env(foo)"
EOD
)

which when run shows the same values in both languages:

-bash-4.2$ bash mixin
bash bar7242
tcl  bar7242
-bash-4.2$ bash mixin
bash bar7247
tcl  bar7247
-bash-4.2$ 

so it is not clear what you mean by "it is not reading" nor what your overall goal is for this code.

0

You can set a variable based on the contents of a file with a syntax like

snp_pw="$(cat passwd_file)"

e.g.

$ cat passwd_file 
anewpassword

$ cat x
#!/bin/bash

snp_pw=$(cat passwd_file)

echo Password is $snp_pw

$ ./x
Password is anewpassword

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.