检查循环中的文件是否是目录时出现问题

发布于 2024-10-02 17:01:16 字数 460 浏览 0 评论 0原文

此代码是检查路径是否为目录的工作示例:

if [ -d "$1" ]; then
        printf "Directory exists\n"
else
        printf "Does not exists\n"
fi 

我将其更改为此以尝试测试目录中的文件是否为目录:

for file in "$1/*"; do
        if [ -d "$file" ]; then 
                printf "Directory: %s\n" $file
        else
                printf "File: %s\n" $file
        fi  
done

但它不起作用 - 目录显示为就好像它们是文件一样。为什么它不起作用?我怎样才能让这段代码做我想做的事?

干杯。

This code is a working example of checking if a path is a directory:

if [ -d "$1" ]; then
        printf "Directory exists\n"
else
        printf "Does not exists\n"
fi 

I changed it to this to try and test if files in a directory are directories or not:

for file in "$1/*"; do
        if [ -d "$file" ]; then 
                printf "Directory: %s\n" $file
        else
                printf "File: %s\n" $file
        fi  
done

But it does not work - directories are displayed as if they were files. Why wont it work? And how can I get this code to do what I want?

Cheers.

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

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

发布评论

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

评论(1

策马西风 2024-10-09 17:01:16

您不能引用星号。

for file in "$1"/* ; do

You can't quote the asterisk.

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