bash 使用命令的输出作为下一个命令的输入

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2689537/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 21:59:48  来源:igfitidea点击:

Use the output of a command as input of the next command

linuxbash

提问by r2b2

So I call this PHP script from the command line:

所以我从命令行调用这个 PHP 脚本:

/usr/bin/php /var/www/bims/index.php "projects/output"

and its output is:

它的输出是:

file1 file2 file3

What I would like to do is get this output and feed to the "rm" command but I think im not doing it right:

我想做的是获取此输出并提供给“rm”命令,但我认为我做得不对:

/usr/bin/php /var/www/bims/index.php "projects/output" | rm 

My goal is to delete whatever file names the PHP script outputs. What should be the proper way to do this?

我的目标是删除 PHP 脚本输出的任何文件名。这样做的正确方法应该是什么?

Thanks!

谢谢!

回答by Victor Sorokin

/usr/bin/php /var/www/bims/index.php "projects/output" | xargs rm

回答by Felix

Simplest solution:

最简单的解决方案:

rm `/usr/bin/php /var/www/bims/index.php "projects/output"`

What is between the backticks (`` ) is run and the output is passed as argument torm`.

反引号之间是什么 (`` ) is run and the output is passed as argument torm`.

回答by ghostdog74

you can try xargs

你可以试试 xargs

/usr/bin/php /var/www/bims/index.php "projects/output" | xargs rm 

or just simply use a loop

或者只是简单地使用一个循环

/usr/bin/php /var/www/bims/index.php "projects/output" | while read -r out
do
  rm $out
done

回答by TestBud

I guess this could help>>

我想这可能会有所帮助>>

grep -n "searchstring" filename | awk 'BEGIN { FS = " " };{print $1}'

grep -n "searchstring" 文件名 | awk 'BEGIN { FS = " " };{print $1}'