Bash:如何处理输入文件中的变量?
我有一个 bash 脚本,它从这样的文件中读取输入:
while IFS="|" read -r a b
do
echo "$a something $b somethingelse"
done < "$FILE"
它读取的文件看起来像这样:
http://someurl1.com|label1
http://someurl2.com|label2
但是,我希望能够在适合我的情况下将变量的名称插入到该文件中,并具有脚本在看到它们时对其进行处理,因此文件可能如下所示:
http://someurl1.com?$VAR|label1
http://someurl2.com|label2
例如,$VAR 可能是今天的日期,产生如下输出:
http://someurl1.com something label1 somethingelse
http://someurl2.com?20100320 something label2 somethingelse
I've got a bash script that reads input from a file like this:
while IFS="|" read -r a b
do
echo "$a something $b somethingelse"
done < "$FILE"
The file it reads looketh like this:
http://someurl1.com|label1
http://someurl2.com|label2
However, I'd like to be able to insert the names of variables into that file when it suits me, and have the script process them when it sees them, so the file might look like this:
http://someurl1.com?$VAR|label1
http://someurl2.com|label2
So $VAR
could be, for example, today's date, producing an output like this:
http://someurl1.com something label1 somethingelse
http://someurl2.com?20100320 something label2 somethingelse
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可能会发现此页面很有用。对于您给出的示例,请尝试:
...尽管如果您使用的文件格式或语言不是一成不变的,则可能有更好的替代方案;)
You might find this page useful. For the example you gave, try:
...though if the file format or language you're using aren't set in stone, there might be better alternatives ;)
您在寻找这样的东西吗?
Are you looking for something like this?
对于替换任何环境变量的通用解决方案(以及执行任何代码,因此如果您不控制变量或清理输入,请不要使用此解决方案),请使用 eval:
For a general solution that'll replace any environment variable (as well as execute any code, so don't use this if you don't control the variables or sanitize the input), use eval: