2

strace、cp、awk、およびstatを使用して、プログレスバー付きのcpを作成するスクリプトがあります。cpを呼び出すコードの部分は次のとおりです。

    strace -q -ewrite cp -- `printf '%q ' $@` 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 どうすれば修正できますか?

4

1 に答える 1

5

使用する"$@"

マンバッシュ:

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

あなたの場合、このフォームを使用してください:

strace -q -ewrite cp -- "$@" | ...
于 2011-07-29T20:43:50.793 に答える