繁体   English   中英

如何在命令行上创建文件并作为参数传递给命令

[英]How to create a file on the command line and pass as an argument to a command

我这样称呼图书馆:

# myFile.txt is a file that already exists read by `grun`
$ grun TestLexer tokens -tokens myFile.txt

但是,我不想传递文件,而是每次在命令行中运行它时都创建/重新创建它,如下所示:

$ grun TestLexer tokens -tokens "echo 'x+1' > myFile.txt"

真正做到这一点的正确方法是什么?

Bash 有进程替换

您可以运行命令并使其 output 可用于另一个命令,就像它是一个文件一样:

$ grun TestLexer tokens -tokens <(echo 'x+1')

要查看发生了什么,请考虑:

$ echo <(echo 'x+1')

如果未提供文件名,则grun从 stdin 读取,因此您也可以在数据中传递 here-doc /here-string或 pipe :

$ grun TestLexer tokens -tokens <<<'x+1'
$ echo 'x+1' | grun TestLexer tokens -tokens

这会是一个选择吗?

echo 'x+1' > myFile.txt && grun TestLexer tokens -tokens myFile.txt

或者在可以同时运行多次的情况下(以防 myFile.txt 被覆盖)或者您不想提交文件名:

tmp=$(mktemp) && echo 'x+1' > $tmp && grun TestLexer tokens -tokens $tmp  && rm -f $tmp

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM