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 回答