Linux 多行命令与 for

发布于 2025-01-11 07:16:42 字数 297 浏览 0 评论 0原文

我想测试和比较两个文件夹中的文件。为此,我使用了下一个命令

for FILE in folder_one/*; do
    test -f folder_one_arch/$FILE && echo $FILE " exists" || echo $FILE " not exists"
    cmp -s $FILE folder_one_arch/$FILE && echo "same" || echo "different";
done

,但该命令不起作用,问题出在哪里?

I want to test and compare files in two folders. For this purpose, I used the next command

for FILE in folder_one/*; do
    test -f folder_one_arch/$FILE && echo $FILE " exists" || echo $FILE " not exists"
    cmp -s $FILE folder_one_arch/$FILE && echo "same" || echo "different";
done

but this command is not working, from where is the problem occuring?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

太阳公公是暖光 2025-01-18 07:16:42

问题来自 $FILE:它包含您期望文件时的路径。

我建议您采用以下方法:有两个变量,PATH(它是for循环中的变量)和FILE(它只是文件基名)。

for PATH in folder_one/*; do
    echo "PATH is $PATH "
    FILE=${PATH##*/}
    echo "FILE is now $FILE"
    test -f folder_one_arch/$FILE && echo $FILE " exists" || echo $FILE " not exists"
    cmp -s $PATH folder_one_arch/$FILE && echo "same" || echo "different";
done

请参阅https://mywiki.wooledge.org/BashFAQ/073

The problem comes from $FILE: it contains a path while you were expecting a file.

I propose you the following approach: have two variables, PATH which is the variable in the for loop and FILE which is only the file basename.

for PATH in folder_one/*; do
    echo "PATH is $PATH "
    FILE=${PATH##*/}
    echo "FILE is now $FILE"
    test -f folder_one_arch/$FILE && echo $FILE " exists" || echo $FILE " not exists"
    cmp -s $PATH folder_one_arch/$FILE && echo "same" || echo "different";
done

See https://mywiki.wooledge.org/BashFAQ/073

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