将Bash脚本中的变量设置为文件内容加上后面的一些文本
如何在redhat Linux中的shell脚本中将Bash变量设置为文件的内容(只有一行),然后在其后面添加一些文本 例如:将Bash脚本中的变量设置为文件内容加上后面的一些文本,bash,Bash,如何在redhat Linux中的shell脚本中将Bash变量设置为文件的内容(只有一行),然后在其后面添加一些文本 例如: echo "Version 1.2.3.4" > $TMPDIR/devver env=dev export ${env}_result="`cat $TMPDIR/${env}ver` (No response - version is from cache)" echo $dev_result 我希望输出为: Version 1.2.3.4 (No res
echo "Version 1.2.3.4" > $TMPDIR/devver
env=dev
export ${env}_result="`cat $TMPDIR/${env}ver` (No response - version is from cache)"
echo $dev_result
我希望输出为:
Version 1.2.3.4 (No response - version is from cache)
相反,我会在版本后换行,如下所示:
Version 1.2.3.4
(No response - version is from cache)
问题不在于变量设置,而在于文件创建。echo在文件内容后追加一个换行符。所以使用
echo -n "Version..."
一切都应该正常。实用程序
tr
可能有助于:
echo "Version 1.2.3.4" | tr '\n' ' '> $TMPDIR/devver
env=dev
export ${env}_result="`cat $TMPDIR/${env}ver` (No response - version is from cache)"
echo $dev_result
测试:)它可以工作。使用bash内置:
export ${env}_result="$(< $TMPDIR/${env}ver) (No response - version is from cache)"
echo "$dev_result"
export${env}\u result=“$(<$TMPDIR/${env}ver)(无响应-版本来自缓存)”
回显“$dev_result”
记录:
“命令替换
$(cat文件)
可以替换为等效但更快的$(
”谢谢!成功了。感谢您的帮助。请使用printf“Version…”
而不是echo-n“Version…”
以提高便携性。在这种情况下没关系,但这是一个好习惯。