如何使用 bash 编写 if 语句来检查存储在名为“$DIR”的脚本变量中的某个目录是否包含不是“.”的子目录。还是“..”?
谢谢,-戴夫
最佳答案
正如评论所指出的,在过去的 9 年里情况发生了变化!点目录不再作为查找的一部分返回,而是 find
命令中指定的目录。
因此,如果您想继续使用这种方法:
#!/bin/bash
subdircount=$(find /tmp/test -maxdepth 1 -type d | wc -l)
if [[ "$subdircount" -eq 1 ]]
then
echo "none of interest"
else
echo "something is in there"
fi
(2011 年最初接受的答案)
#!/usr/bin/bash
subdircount=`find /d/temp/ -maxdepth 1 -type d | wc -l`
if [ $subdircount -eq 2 ]
then
echo "none of interest"
else
echo "something is in there"
fi
关于bash - 如何检查目录是否有子目录?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6781225/