如何通过 strace 将多个带空格的文件名传递给命令?

发布于 2024-11-26 19:44:08 字数 1448 浏览 1 评论 0原文

我有一个使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

安静 2024-12-03 19:44:08

使用 "$@"

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 -- "$@" | ...

use "$@"

man bash:

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

In your case, use this form:

strace -q -ewrite cp -- "$@" | ...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文