3

如何在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 response - version is from cache)

相反,我在版本之后得到一个换行符,如下所示:

Version 1.2.3.4
(No response - version is from cache)
4

3 回答 3

3

您的问题不在文件创建中的变量设置中。echo 将换行符附加到文件内容。所以使用

echo -n "Version..."

事情应该会奏效。

于 2013-01-04T03:43:54.187 回答
2

Use a bash builtin:

export ${env}_result="$(< $TMPDIR/${env}ver) (No response - version is from cache)"
echo "$dev_result"

Documented here: "The command substitution $(cat file) can be replaced by the equivalent but faster $(< file)."

于 2013-01-04T12:09:02.077 回答
1

该实用程序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

经过测试:)它有效。

于 2013-01-04T03:45:30.440 回答