linux - 搜索文件夹中的所有子文件夹时,得到 "[: if-then: unexpected operator "

标签 linux shell ubuntu

当我在 Ubuntu 中运行此脚本时:

   for item in *
do
    if  [ -d $item ]
    then
        echo $item
    fi
done

我得到了这个输出: output

我不明白为什么我得到“[:if-then:意外的运算符”

最佳答案

转义变量。了解它 here .

for item in *
do
    if  [ -d "$item" ]
    then
        echo "$item"
    fi
done

关于linux - 搜索文件夹中的所有子文件夹时,得到 "[: if-then: unexpected operator ",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52418947/

相关文章:

linux - 从 Linux 内核模块执行 copy_to_user 时出现 "Bad Address"错误

ubuntu - 在 Ubuntu 中使用 Gstreamer 通过 RTP 协议(protocol)流式传输 Mp4 视频

Ubuntu安装以前版本的erlang解决方案包

linux - Bash 脚本复制最新文件并将输出发送到电子邮件 ID

c - 系统命令在 C 程序中挂起,但是当我在 bash 上运行命令时它成功了

linux - 如何通过linux命令获取某个接口(interface)的统计信息?

linux - 将文件名作为脚本中的密码值

linux - 在 shell 脚本中 : redirect echo shell commands as they are executed

regex - 使用 posix shell 测试字符串中的正则表达式

在远程 ubuntu 服务器上运行的 Java Jar