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

2020-07-13 13:48发布

问题:

So I call this PHP script from the command line:

/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:

/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?

Thanks!

回答1:

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


回答2:

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`.



回答3:

you can try 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


回答4:

I guess this could help>>

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



标签: linux bash