使用bash,我如何编写一条if语句来检查存储在名为"$DIR“的脚本变量中的某个目录是否包含不包含”.或者".."?
谢谢,-戴夫
发布于 2011-07-22 03:07:14
正如评论指出的那样,在过去的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发布于 2011-07-23 00:28:05
下面是一个更简约的解决方案,它将在一行中执行测试。
ls $DIR/*/ >/dev/null 2>&1 ;
if [ $? == 0 ];
then
echo Subdirs
else
echo No-subdirs
fi通过将/放在*通配符后面,您可以只选择目录,因此如果没有目录,则ls将返回error-status 2并打印消息ls: cannot access <dir>/*/: No such file or directory。2>&1捕获stderr并通过管道将其传送到stdout,然后将整个批处理通过管道传送到null (如果有文件,它也会删除常规的ls输出)。
发布于 2011-07-22 03:26:16
我不太清楚您在这里想要做什么,但是您可以使用find
find /path/to/root/directory -type d如果你想编写脚本:
find $DIR/* -type d应该能行得通。
https://stackoverflow.com/questions/6781225
复制相似问题