如何通过 strace 将多个带空格的文件名传递给命令?
我有一个使用 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\' space
我该如何解决这个问题?
I have a script that uses strace, cp, awk, and stat, to create a cp with a progress bar. Here is the part of the code where it calls cp:
strace -q -ewrite cp -- `printf '%q ' $@` 2>&1 | awk {Lots of code here}
The problem is, I cannot copy anything with a space. How should I modify this script so it will work with spaces? Thanks
EDIT: Here is the output:
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% [→→]
See the first one? That works fine, which executes cp -- q file
. Now, the next one, cp -- q 'file\' 'with\' spaces
how do I fix that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用
"$@"
man bash:
在您的情况下,请使用以下形式:
use
"$@"
man bash:
In your case, use this form: