终端shell:如何使用echo而不是文本文件作为命令行参数

时间:2015-03-24 10:11:11

标签: bash shell terminal command-line-arguments home-automation

我有一个命令行实用程序,它需要一个文本文件(格式化输出)作为参数。我只需要纯值而不需要格式化,所以我想要一个单行脚本来获取值。

文本文件template.file仅包含:

$1

以下是我的实用程序示例:

vclient -h 10.0.0.131:3002 -t template.file -g getTempKist

我想要的是这样的:

vclient -h 10.0.0.131:3002 -t $(echo '\$1') -g getTempKist

是否有人知道如何使用echo(或替代)的结果而不是外部文本文件?

我希望有人可以提供帮助。

马库斯

3 个答案:

答案 0 :(得分:3)

您可以尝试两件事:

首先,您可以使用stdin作为输入:

echo '$1' | vclient -h 10.0.0.131:3002 -t - -g getTempKist

有些工具支持文件名参数的特殊值-,其中-代表stdin。但是这取决于命令的实现。

如果您的shell(例如bashzsh)支持,则可以使用的第二件事是使用process substitution

vclient -h 10.0.0.131:3002 -t <(echo '$1') -g getTempKist

答案 1 :(得分:2)

如果您尝试使用echo来避免创建临时文件,那么您可能更愿意尝试查看是否可以直接粘贴到stdin。许多工具都支持这一点:

$ cat foo.json 
[1,2,3]

$ python -m json.tool foo.json 
[
    1,
    2,
    3
]

$ python -m json.tool /dev/stdin
[1,2,3]  <-- Paste this into the terminal then press Ctrl-D
[
    1,
    2,
    3
]

因此,对于OP中的工具:

$ vclient -h 10.0.0.131:3002 -t /dev/stdin -g getTempKist
\$1  <-- Paste this into the terminal then press Ctrl-D

答案 2 :(得分:0)

使用``包围的命令(抱歉,代码格式化使我对这些字符有麻烦)首先评估命令的结果,所以用你的echo围绕`可能有效。