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


当前回答

这个答案被包装成一个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";

其他回答

你有没有考虑过只做你想做的事,而不是三思而后行?

也就是说,如果您想在输入目录之前检查其是否存在,请尝试执行以下操作:

if pushd /path/you/want/to/enter; then
    # Commands you want to run in this directory
    popd
fi

如果您给pushd的路径存在,您将输入它,它将以0退出,这意味着将执行语句的then部分。如果它不存在,什么都不会发生(除了一些输出说目录不存在,这可能对调试有帮助的副作用)。

这似乎比这更好,这需要重复自己:

if [ -d /path/you/want/to/enter ]; then
    pushd /path/you/want/to/enter
    # Commands you want to run in this directory
    popd
fi

同样的方法也适用于cd、mv、rm等……如果您在不存在的文件上尝试它们,它们将以错误退出并打印一条消息表示不存在,然后将跳过您的then块。如果您在确实存在的文件上尝试它们,则命令将执行并退出,状态为0,从而允许您的then块执行。

file="foo" 
if [[ -e "$file" ]]; then echo "File Exists"; fi;

较短形式:

# if $DIR is a directory, then print yes
[ -d "$DIR" ] && echo "Yes"

注意-d测试可能会产生一些令人惊讶的结果:

$ ln -s tmp/ t
$ if [ -d t ]; then rmdir t; fi
rmdir: directory "t": Path component not a directory

下面的文件:“什么时候目录不是目录?”答案:“当它是指向目录的符号链接时。”

if [ -d t ]; then 
   if [ -L t ]; then 
      rm t
   else 
      rmdir t
   fi
fi

您可以在Bash手册中找到有关Bash条件表达式、内置命令和[[复合命令的更多信息。

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

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