简体   繁体   English

如何在bash中将输出作为命令行参数传递?

[英]How to pass output as command line argument in bash?

I have this two step bash command: 我有这两个步骤bash命令:

L=`wc -l testfile | cut -d' ' -f1`
myprogram testfile $L testfile.out

Long story short, myprogram needs the line count as an input. 长话短说, myprogram需要行数作为输入。

I want to combine this into one line. 我想将其合并为一行。

This does not work because using redirect | 因为使用的是重定向这不起作用 | to - passes stdout stream as a file, not a string. -通过标准输出流作为一个文件,而不是一个字符串。

wc -l testfile | cut -d' ' -f1 | myprogram testfile - testfile.out

Is there a way to combine this into one line? 有没有办法将其组合成一条线?

Use process substitution: 使用流程替代:

myprogram testfile $(wc -l < testfile) testfile.out
                   ^^^^^^^^^^^^^^^^^^^

This way, wc -l < testfile is evaluated together with the call of the program and you have both commands combined. 这样, wc -l < testfile与程序调用一起被评估,并且您将两个命令组合在一起。

Note wc -l < file returns you just the number, so you don't have to do cut or any other thing to clean the output. 注意wc -l < file仅返回数字,因此您不必执行cut或其他任何操作来清理输出。

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

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