什么命令检查Bash shell脚本中是否存在目录?


当前回答

ls命令与-l(长列表)选项一起返回有关文件和目录的属性信息。特别是ls-l输出的第一个字符通常是d或a-(破折号)。在d的情况下,列出的是一个目录。

仅一行中的以下命令将告诉您给定的ISDIR变量是否包含指向目录的路径:

[[ $(ls -ld "$ISDIR" | cut -c1) == 'd' ]] &&
    echo "YES, $ISDIR is a directory." || 
    echo "Sorry, $ISDIR is not a directory"

实际用途:

    [claudio@nowhere ~]$ ISDIR="$HOME/Music" 
    [claudio@nowhere ~]$ ls -ld "$ISDIR"
    drwxr-xr-x. 2 claudio claudio 4096 Aug 23 00:02 /home/claudio/Music
    [claudio@nowhere ~]$ [[ $(ls -ld "$ISDIR" | cut -c1) == 'd' ]] && 
        echo "YES, $ISDIR is a directory." ||
        echo "Sorry, $ISDIR is not a directory"
    YES, /home/claudio/Music is a directory.

    [claudio@nowhere ~]$ touch "empty file.txt"
    [claudio@nowhere ~]$ ISDIR="$HOME/empty file.txt" 
    [claudio@nowhere ~]$ [[ $(ls -ld "$ISDIR" | cut -c1) == 'd' ]] && 
        echo "YES, $ISDIR is a directory." || 
        echo "Sorry, $ISDIR is not a directoy"
    Sorry, /home/claudio/empty file.txt is not a directory

其他回答

这个答案被包装成一个shell脚本

示例

$ is_dir ~                           
YES

$ is_dir /tmp                        
YES

$ is_dir ~/bin                       
YES

$ mkdir '/tmp/test me'

$ is_dir '/tmp/test me'
YES

$ is_dir /asdf/asdf                  
NO

# Example of calling it in another script
DIR=~/mydata
if [ $(is_dir $DIR) == "NO" ]
then
  echo "Folder doesnt exist: $DIR";
  exit;
fi

is目录

function show_help()
{
  IT=$(CAT <<EOF

  usage: DIR
  output: YES or NO, depending on whether or not the directory exists.

  )
  echo "$IT"
  exit
}

if [ "$1" == "help" ]
then
  show_help
fi
if [ -z "$1" ]
then
  show_help
fi

DIR=$1
if [ -d $DIR ]; then 
   echo "YES";
   exit;
fi
echo "NO";

作为“[-d]”和“[-h]”选项的替代,您可以使用stat获取文件类型并对其进行分析。

#! /bin/bash
MY_DIR=$1
NODE_TYPE=$(stat -c '%F' ${MY_DIR} 2>/dev/null)
case "${NODE_TYPE}" in
        "directory") echo $MY_DIR;;
    "symbolic link") echo $(readlink $MY_DIR);;
                 "") echo "$MY_DIR does not exist";;
                  *) echo "$NODE_TYPE is unsupported";;
esac
exit 0

测试数据:

$ mkdir tmp
$ ln -s tmp derp
$ touch a.txt
$ ./dir.sh tmp
tmp
$ ./dir.sh derp
tmp
$ ./dir.sh a.txt
regular file is unsupported
$ ./dir.sh god
god does not exist

如果要检查目录是否存在,无论它是真实目录还是符号链接,请使用以下命令:

ls $DIR
if [ $? != 0 ]; then
        echo "Directory $DIR already exists!"
        exit 1;
fi
echo "Directory $DIR does not exist..."

解释:如果目录或符号链接不存在,则“ls”命令会给出错误“ls:/x:No such file or directory”,并将返回代码设置为非空(通常为“1”),您可以通过“$?”检索该代码。请确保在调用“ls”后直接检查返回代码。

在三元形式中,

[ -d "$directory" ] && echo "exist" || echo "not exist"

通过测试:

test -d "$directory" && echo "exist" || echo "not exist"

这里有一个非常实用的成语:

(cd $dir) || return # Is this a directory,
                    # and do we have access?

我通常将其包装在函数中:

can_use_as_dir() {
    (cd ${1:?pathname expected}) || return
}

Or:

assert_dir_access() {
    (cd ${1:?pathname expected}) || exit
}

这种方法的好处是,我不必想到好的错误消息。

cd会给我一条标准的单行消息,告诉我标准错误。它还将提供我无法提供的更多信息。通过在子shell(…)中执行cd,该命令不会影响调用者的当前目录。如果目录存在,则此子shell和函数只是一个no-op。

接下来是传递给cd:${1:?路径名应为}的参数。这是一种更为复杂的参数替换形式,将在下面进行更详细的解释。

T1;dr:如果传入此函数的字符串为空,我们将再次从子shell(…)退出,并返回带有给定错误消息的函数。


引用ksh93手册页:

${parameter:?word}

若参数设置为非空,则替换其值;否则,打印word并退出shell(如果不是交互式的)。若单词被省略,则打印标准消息。

and

如果在上述表达式中省略冒号:,则shell只检查是否设置了参数。

这里的措辞是shell文档特有的,因为单词可以指任何合理的字符串,包括空格。

在这种特殊情况下,我知道标准错误消息1:parameter not set是不够的,所以我放大了我们期望的值类型——目录的路径名。

哲学笔记:

shell不是面向对象的语言,因此消息中显示的是路径名,而不是目录。在这个层次上,我宁愿保持简单——函数的参数只是字符串。