Reading variable from another file into bash

7,614

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

Share:
7,614

Related videos on Youtube

saurav
Author by

saurav

Updated on September 18, 2022

Comments

  • saurav
    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
      ctrl-alt-delor over 5 years
      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.
    • saurav
      saurav over 5 years
      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
    • ctrl-alt-delor
      ctrl-alt-delor over 5 years
      show me the code, (edit question).
    • saurav
      saurav over 5 years
      I have edited and added some snippet
    • RalfFriedl
      RalfFriedl over 5 years
      And which part of the code you added is the minimum to show your problem?
    • saurav
      saurav over 5 years
      send -- "$::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
      Mario Orlandi over 5 years
      take a look at sexpect (Expect for Shells) with which you can write Expect scripts with shell code only.
  • Angel Todorov
    Angel Todorov over 5 years
    With bash, you can avoid cat using: snp_pw=$(<passwd_file) -- ref gnu.org/software/bash/manual/bash.html#Command-Substitution
  • Angel Todorov
    Angel Todorov over 5 years
    Also, 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