How to expand shell variables in a text file?

前端 未结 10 1442
夕颜
夕颜 2021-02-04 02:05

Consider a ASCII text file (lets say it contains code of a non-shell scripting language):

Text_File.msh:

spool on to \'$LOG_FILE_PATH/lo         


        
相关标签:
10条回答
  • 2021-02-04 02:28

    This solution is not elegant, but it works. Create a script call shell_expansion.sh:

    echo 'cat <<END_OF_TEXT' >  temp.sh
    cat "$1"                 >> temp.sh
    echo 'END_OF_TEXT'       >> temp.sh
    bash temp.sh >> "$2"
    rm temp.sh
    

    You can then invoke this script as followed:

    bash shell_expansion.sh Text_File.msh Text_File_expanded.msh
    
    0 讨论(0)
  • 2021-02-04 02:29

    One limitation of the above answers is that they both require the variables to be exported to the environment. Here's what i came up with that would allow the variables to be local to the current shell script:

    #!/bin/sh
    FOO=bar;
    FILE=`mktemp`; # Let the shell create a temporary file
    trap 'rm -f $FILE' 0 1 2 3 15;   # Clean up the temporary file 
    
    (
      echo 'cat <<END_OF_TEXT'
      cat "$@"
      echo 'END_OF_TEXT'
    ) > $FILE
    . $FILE
    

    The above example allows the variable $FOO to be substituted in the files named on the command line. I'm sure it can be improved, but this works for me so far.

    Thanks to both previous answers for their ideas!

    0 讨论(0)
  • 2021-02-04 02:31

    This question has been asked in another thread, and this is the best answer IMO:

    export LOG_FILE_PATH=/expanded/path/of/the/log/file/../logfile.log
    cat Text_File.msh | envsubst > Text_File_expanded.msh
    

    if on Mac, install gettext first: brew install gettext

    see: Forcing bash to expand variables in a string loaded from a file

    0 讨论(0)
  • 2021-02-04 02:34

    #logfiles.list:

    $EAMSROOT/var/log/LinuxOSAgent.log
    $EAMSROOT/var/log/PanacesServer.log
    $EAMSROOT/var/log/PanacesStrutsGUI.log
    

    #My Program:

    cat logfiles.list | while read line
        do
            eval Eline=$line
            echo $Eline
        done
    
    0 讨论(0)
  • 2021-02-04 02:42

    If a Perl solution is ok for you:

    Sample file:

    $ cat file.sh
    spool on to '$HOME/logfile.log';
    login 'username' 'password';
    

    Solution:

    $ perl -pe 's/\$(\w+)/$ENV{$1}/g' file.sh
    spool on to '/home/user/logfile.log';
    login 'username' 'password';
    
    0 讨论(0)
  • 2021-02-04 02:43

    If you want it in one line (I'm not a bash expert so there may be caveats to this but it works everywhere I've tried it):

    when test.txt contains

    ${line1}
    ${line2}
    

    then:

    >line1=fark
    >line2=fork
    >value=$(eval "echo \"$(cat test.txt)\"")
    >echo "$value"
    line1 says fark
    line2 says fork
    

    Obviously if you just want to print it you can take out the extra value=$() and echo "$value".

    0 讨论(0)
提交回复
热议问题