2017-02-23 29 views
2

在osx中​​我試圖檢測何時將新文件添加到文件夾,然後執行腳本來處理該新文件。似乎很簡單,對吧?使用osx中的xargs接受fswatch中已更改文件的名稱

我這樣做:

fswatch -0 ~/motion-detection | xargs -0 -n1 -I {} ./detectmotion.sh 

這將調用shell腳本detectmotion.sh其中包括:

echo "File added: " $1 

現在,至少。我只是想通過改變文件的文件名來detectmotion.sh

得到了一個空白$ 1

有人能看到我在做什麼錯在這裏?

在此先感謝!

回答

2

它預計爲爲空,因爲您沒有將參數傳遞給您從xargs獲得的腳本detectmotion.sh。這本來是

fswatch -0 ~/motion-detection | xargs -0 -n1 -I {} ./detectmotion.sh "{}" 

記住-I {}標誌xargs代表值管道內襯xargs一個佔位。因此,作爲問題的一部分,您只需將該值存儲在佔位符({})中,而不是實際將其傳遞給腳本以使其有用。

因此,我修改了命令,以便您實際上將接收到的值傳遞給腳本,腳本中可以打印的腳本現在可以打印爲$1

man xargs頁面報價爲-I

-I replace-str 
    Replace occurrences of replace-str in the initial-arguments with names read from 
    standard input. Also, unquoted blanks do not terminate input items; instead the 
    separator is the newline character. Implies -x and -L 1. 

在我們的例子中replace-str被用作{}

+1

工作,謝謝! – Uberbug

+0

@Uberbug:很高興你發現它很有用。 – Inian

相關問題