2010-04-22 123 views
4

所以我把這個PHP腳本的命令行輸入:使用命令的輸出作爲下一個命令

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

,其輸出是:

file1 file2 file3 

我想做是得到這個輸出和飼料的「RM」命令,但我認爲我沒有這樣做的權利:

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

我的目標是刪除任何文件命名PHP腳本輸出。應該怎樣才能做到這一點?

謝謝!

回答

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

工作! xargs命令是什麼?謝謝! – r2b2 2010-04-22 09:27:41

+0

man xargs:「xargs - 從標準輸入建立和執行命令行」。你應該閱讀那個手冊頁;) – 2010-04-22 09:31:10

3

你可以嘗試xargs

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

,或只是簡單地使用一個循環

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

簡單的解決方案:

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

什麼是反引號(``) is run and the output is passed as argument to之間rm`。

+1

xargs稍微好一點:它可以通過多次運行'rm'來處理對於最大命令行長度來說太長的輸出。 – 2010-04-22 09:36:00

+0

我沒有說*更好*。我說*簡單* :)。 – Felix 2010-04-22 09:53:17

+1

注意:如果Php腳本生成帶空格的文件,可能不起作用 – ghostdog74 2010-04-22 12:58:11

0

我想這可以幫助>>

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