如何在文本文件中扩展Shell变量? [英] How to expand shell variables in a text file?

查看:62
本文介绍了如何在文本文件中扩展Shell变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑一个ASCII文本文件(可以说它包含非shell脚本语言的代码):

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/logfile.log';
login 'username' 'password';
....

现在,如果这是一个Shell脚本,我可以将其作为$ sh Text_File.msh运行,并且Shell会自动扩展变量. 我想做的是让Shell扩展这些变量,然后按如下方式创建一个新文件,如Text_File_expanded.msh所示:

Now if this were a shell script I could run it as $ sh Text_File.msh and the shell would automatically expand the variables. What I want to do is have shell expand these variables and then create a new file as Text_File_expanded.msh as follows:

Text_File_expanded.msh:

spool on to '/expanded/path/of/the/log/file/../logfile.log';
login 'username' 'password';
....


考虑:


Consider:

$ a=123
$ echo "$a"
123

因此从技术上讲,这应该可以解决问题:

So technically this should do the trick:

$ echo "`cat Text_File.msh`" > Text_File_expanded.msh

...但是它不能按预期工作,并且输出文件与源文件相同.

...but it doesn't work as expected and the output-file while is identical to the source.

因此,我不确定如何实现此目标..我的目标是使维护非shell脚本中嵌入的目录路径更加容易. 这些脚本不能包含任何UNIX代码,因为它不是UNIX shell编译的.

So I am unsure how to achieve this.. My goal is make it easier to maintain the directory paths embedded within my non-shell scripts. These scripts cannot contain any UNIX code as it is not compiled by the UNIX shell.

推荐答案

此解决方案虽然不美观,但可以使用.创建脚本调用shell_expansion.sh:

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

这篇关于如何在文本文件中扩展Shell变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆