2011-07-29 33 views
2

我有一個使用strace,cp,awk和stat的腳本來創建一個包含進度條的cp。下面是它調用cp的代碼部分:如何通過strace將多個帶空格的文件名傳遞給命令?

strace -q -ewrite cp -- `printf '%q ' [email protected]` 2>&1 | awk {Lots of code here} 

問題是,我無法用空格複製任何東西。我應該如何修改這個腳本,以便它能與空格一起工作?由於

編輯:這裏是輸出:

matt: ~/tmp $ bash -x cp-progress "q" "file" 
++ printf '%q ' q file 
++ stat -c %s q 
+ strace -q -ewrite cp -- q file 
+ awk '{ 
     count += $NF 
      if (count % 10 == 0) { 
       percent = count/total_size * 100 
       printf "%2d%% [", percent 
       for (i=0;i<=percent/2;i++) 
        printf "→" 
       printf "→" 

       printf "]\r" 
      } 
     } 
     END { print "" }' total_size=5242880 count=0 
100% [→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→] 
matt: ~/tmp $ bash -x cp-progress "q" "file with spaces" 
++ printf '%q ' q 'file with spaces' 
+ strace -q -ewrite cp -- q 'file\' 'with\' spaces 
++ stat -c %s q 
+ awk '{ 
     count += $NF 
      if (count % 10 == 0) { 
       percent = count/total_size * 100 
       printf "%2d%% [", percent 
       for (i=0;i<=percent/2;i++) 
        printf "→" 
       printf "→" 

       printf "]\r" 
      } 
     } 
     END { print "" }' total_size=5242880 count=0 
0% [→→] 

見的第一個?這工作正常,執行cp -- q file。現在,下一個,cp -- q 'file\' 'with\' spaces我該如何解決這個問題?

回答

5

使用"[email protected]"

人的bash:

When the expansion occurs within double quotes, each parameter expands to a 
separate word. That is, "[email protected]" is equivalent to "$1" "$2" ... 

在你的情況下,使用這種形式:

strace -q -ewrite cp -- "[email protected]" | ... 
+0

你的意思是'printf的 '%Q' 「$ @」'?這是行不通的。 – Matt

+0

'strace -q -ewrite cp - 「$ @」| ...'? –

+0

哦。對,那是有效的。 – Matt

相關問題