Unix,显示消息"没有这样的目录"

时间:2014-04-08 02:49:26

标签: unix

#!/bin/sh

LOCATION=$1

if [ "$#" -ne "1" ]
then
echo "Usage:./test1 <directory_name>"

else
echo "Number of directories: $(find $LOCATION -type d | wc -l) "
echo "Number of files: $(find $LOCATION -type f | wc -l)"
echo "Number of readable: $(find $LOCATION -type f -readable | wc -l )"
echo "Number of writable: $(find $LOCATION -type f -writable | wc -l )"
echo "Number of executable: $(find $LOCATION -type f -executable | wc -l )"
fi

if [ $Location does not exist? ]
then
echo "This Directory does not exist"
if

我对unix很新,我不知道怎么做最后一部分?如果目录不存在,我应该在里面说什么?

1 个答案:

答案 0 :(得分:2)

这很容易实现。 Bash包括提供此类功能的File Test Operators

示例

    if [ ! -d $LOCATION ]
    then
        echo "This Directory does not exist"
    fi

编辑:同时将其移到顶部,这样如果目录不存在,则不会浪费时间和资源来检查不存在的目录。这也将避免进一步的警告。

示例

#!/bin/sh

LOCATION=$1

if [ "$#" -ne "1" ]
then
echo "Usage:./test1 <directory_name>"
else
   if [ -d $LOCATION ]
   then
        echo "Number of directories: $(find $LOCATION -type d | wc -l) "
        echo "Number of files: $(find $LOCATION -type f | wc -l)"
        echo "Number of readable: $(find $LOCATION -type f -readable | wc -l )"
        echo "Number of writable: $(find $LOCATION -type f -writable | wc -l )"
        echo "Number of executable: $(find $LOCATION -type f -executable | wc -l )"
   else
        echo "This Directory does not exist"
   fi
fi

希望有所帮助。