2014-04-09 84 views
1

我在使用Bash語法時遇到問題,希望有些大師能夠幫助我解決問題。Bash執行時間錯誤

bash的是:

(sort | grep po | wc) </etc/passwd> out ; cat out 

exec /usr/bin/time -f "real %e user %U sys %S maxmem %M" (sort | grep po | wc) </etc/passwd> out; cat out 

第一行運行正常。第二行給出了錯誤:

./sh: line 2: syntax error near unexpected token `(' 
./sh: line 2: `exec time (sort | grep po | wc) </etc/passwd> out' 

我試圖讓1號線在:

exec /usr/bin/time -f "real %e user %U sys %S maxmem %M" --Line 1 Solution Here-- 

形式。

我已經試過了我能想到的所有引用內容,並且似乎無法實現。作爲一個側面提示,刪除-f和參數以及前面的exec將允許它運行,但在我的場景中,我需要它們處於最終解決方案中。

由於我在這裏嘗試了很多東西,嘗試在它周圍放置雙引號的反應,試試這個或試試會得到否定的投票。

回答

0
exec /usr/bin/time -f "real %e user %U sys %S maxmem %M" /bin/bash -c 'sort | grep po | wc' </etc/passwd> out; cat out 

感謝當地的同事。

+0

請注意,這是可行的,因爲'/ usr/bin/time'需要一個可執行文件作爲它的參數,而不是一個任意的shell命令。 – chepner