Reading variable from another file into bash

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
1
down vote

favorite












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.










share|improve this question



















  • 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 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 at 22:39






  • 2




    show me the code, (edit question).
    – ctrl-alt-delor
    Dec 4 at 22:39










  • I have edited and added some snippet
    – saurav
    Dec 4 at 22:45











  • And which part of the code you added is the minimum to show your problem?
    – RalfFriedl
    Dec 4 at 23:00














up vote
1
down vote

favorite












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.










share|improve this question



















  • 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 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 at 22:39






  • 2




    show me the code, (edit question).
    – ctrl-alt-delor
    Dec 4 at 22:39










  • I have edited and added some snippet
    – saurav
    Dec 4 at 22:45











  • And which part of the code you added is the minimum to show your problem?
    – RalfFriedl
    Dec 4 at 23:00












up vote
1
down vote

favorite









up vote
1
down vote

favorite











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.










share|improve this question















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.







bash shell-script expect






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 4 at 22:46









Rui F Ribeiro

38.5k1479128




38.5k1479128










asked Dec 4 at 22:33









saurav

83




83







  • 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 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 at 22:39






  • 2




    show me the code, (edit question).
    – ctrl-alt-delor
    Dec 4 at 22:39










  • I have edited and added some snippet
    – saurav
    Dec 4 at 22:45











  • And which part of the code you added is the minimum to show your problem?
    – RalfFriedl
    Dec 4 at 23:00












  • 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 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 at 22:39






  • 2




    show me the code, (edit question).
    – ctrl-alt-delor
    Dec 4 at 22:39










  • I have edited and added some snippet
    – saurav
    Dec 4 at 22:45











  • And which part of the code you added is the minimum to show your problem?
    – RalfFriedl
    Dec 4 at 23:00







1




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 at 22:36




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 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 at 22:39




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 at 22:39




2




2




show me the code, (edit question).
– ctrl-alt-delor
Dec 4 at 22:39




show me the code, (edit question).
– ctrl-alt-delor
Dec 4 at 22:39












I have edited and added some snippet
– saurav
Dec 4 at 22:45





I have edited and added some snippet
– saurav
Dec 4 at 22:45













And which part of the code you added is the minimum to show your problem?
– RalfFriedl
Dec 4 at 23:00




And which part of the code you added is the minimum to show your problem?
– RalfFriedl
Dec 4 at 23:00










3 Answers
3






active

oldest

votes

















up vote
0
down vote



accepted










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|improve this answer



























    up vote
    0
    down vote













    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.






    share|improve this answer



























      up vote
      0
      down vote













      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





      share|improve this answer




















      • With bash, you can avoid cat using: snp_pw=$(<passwd_file) -- ref gnu.org/software/bash/manual/bash.html#Command-Substitution
        – glenn jackman
        Dec 5 at 19:32










      • 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
        – glenn jackman
        Dec 5 at 19:34










      Your Answer








      StackExchange.ready(function()
      var channelOptions =
      tags: "".split(" "),
      id: "106"
      ;
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function()
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled)
      StackExchange.using("snippets", function()
      createEditor();
      );

      else
      createEditor();

      );

      function createEditor()
      StackExchange.prepareEditor(
      heartbeatType: 'answer',
      convertImagesToLinks: false,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: null,
      bindNavPrevention: true,
      postfix: "",
      imageUploader:
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      ,
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      );



      );













      draft saved

      draft discarded


















      StackExchange.ready(
      function ()
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f486018%2freading-variable-from-another-file-into-bash%23new-answer', 'question_page');

      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      0
      down vote



      accepted










      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|improve this answer
























        up vote
        0
        down vote



        accepted










        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|improve this answer






















          up vote
          0
          down vote



          accepted







          up vote
          0
          down vote



          accepted






          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|improve this answer












          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|improve this answer












          share|improve this answer



          share|improve this answer










          answered Dec 5 at 5:44









          G-Man

          12.8k93164




          12.8k93164






















              up vote
              0
              down vote













              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.






              share|improve this answer
























                up vote
                0
                down vote













                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.






                share|improve this answer






















                  up vote
                  0
                  down vote










                  up vote
                  0
                  down vote









                  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.






                  share|improve this answer












                  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.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Dec 5 at 0:01









                  thrig

                  23.9k22955




                  23.9k22955




















                      up vote
                      0
                      down vote













                      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





                      share|improve this answer




















                      • With bash, you can avoid cat using: snp_pw=$(<passwd_file) -- ref gnu.org/software/bash/manual/bash.html#Command-Substitution
                        – glenn jackman
                        Dec 5 at 19:32










                      • 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
                        – glenn jackman
                        Dec 5 at 19:34














                      up vote
                      0
                      down vote













                      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





                      share|improve this answer




















                      • With bash, you can avoid cat using: snp_pw=$(<passwd_file) -- ref gnu.org/software/bash/manual/bash.html#Command-Substitution
                        – glenn jackman
                        Dec 5 at 19:32










                      • 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
                        – glenn jackman
                        Dec 5 at 19:34












                      up vote
                      0
                      down vote










                      up vote
                      0
                      down vote









                      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





                      share|improve this answer












                      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






                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Dec 5 at 2:13









                      Stephen Harris

                      24k24477




                      24k24477











                      • With bash, you can avoid cat using: snp_pw=$(<passwd_file) -- ref gnu.org/software/bash/manual/bash.html#Command-Substitution
                        – glenn jackman
                        Dec 5 at 19:32










                      • 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
                        – glenn jackman
                        Dec 5 at 19:34
















                      • With bash, you can avoid cat using: snp_pw=$(<passwd_file) -- ref gnu.org/software/bash/manual/bash.html#Command-Substitution
                        – glenn jackman
                        Dec 5 at 19:32










                      • 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
                        – glenn jackman
                        Dec 5 at 19:34















                      With bash, you can avoid cat using: snp_pw=$(<passwd_file) -- ref gnu.org/software/bash/manual/bash.html#Command-Substitution
                      – glenn jackman
                      Dec 5 at 19:32




                      With bash, you can avoid cat using: snp_pw=$(<passwd_file) -- ref gnu.org/software/bash/manual/bash.html#Command-Substitution
                      – glenn jackman
                      Dec 5 at 19:32












                      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
                      – glenn jackman
                      Dec 5 at 19:34




                      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
                      – glenn jackman
                      Dec 5 at 19:34

















                      draft saved

                      draft discarded
















































                      Thanks for contributing an answer to Unix & Linux Stack Exchange!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid


                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.

                      To learn more, see our tips on writing great answers.





                      Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                      Please pay close attention to the following guidance:


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid


                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.

                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f486018%2freading-variable-from-another-file-into-bash%23new-answer', 'question_page');

                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown






                      Popular posts from this blog

                      How to check contact read email or not when send email to Individual?

                      Bahrain

                      Postfix configuration issue with fips on centos 7; mailgun relay