How to expand shell variables in a text file?

前端 未结 10 1453
夕颜
夕颜 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:45

    This solution allows you to keep the same formatting in the ouput file

    Copy and paste the following lines in your script

    cat $1 | while read line
    do
      eval $line
      echo $line
      eval echo $line
    done | uniq | grep -v '\$'
    

    this will read the file passed as argument line by line, and then process to try and print each line twice: - once without substitution - once with substitution of the variables. then remove the duplicate lines then remove the lines containing visible variables ($)

提交回复
热议问题