我有一个使用strace,cp,awk和stat的脚本来创建带有进度条的cp.以下是调用cp的代码部分:
strace -q -ewrite cp -- `printf '%q ' $@` 2>&1 | awk {Lots of code here}
Run Code Online (Sandbox Code Playgroud)
问题是,我不能用空格复制任何东西.我该如何修改这个脚本以便它可以使用空格?谢谢
编辑:这是输出:
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% [??]
Run Code Online (Sandbox Code Playgroud)
看到第一个?这工作正常,执行cp -- q file.现在,下一个,cp -- q 'file\' 'with\' spaces 我该如何解决?
使用 "$@"
男子打击:
When the expansion occurs within double quotes, each parameter expands to a
separate word. That is, "$@" is equivalent to "$1" "$2" ...
Run Code Online (Sandbox Code Playgroud)
在您的情况下,请使用此表单:
strace -q -ewrite cp -- "$@" | ...
Run Code Online (Sandbox Code Playgroud)