我有一个使用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
如何解决这个问题?
答案 0 :(得分:5)
使用"$@"
man bash:
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 -- "$@" | ...