1

我想检查目录是否存在并在下面编写脚本,但这不能正常工作。

#!/bin/sh
if [ -d "~/sample" ]
then
    echo 'exists'
else
    echo 'NOT exists'
fi

下面的脚本可以工作。

#!/bin/sh
if [ -d "/home/user01/sample" ]
then
    echo 'exists'
else
    echo 'NOT exists'
fi

if [ -d "~/sample" ]有什么问题吗?

4

1 回答 1

2

是的,双引号是不让 ~ 扩展的……以下将起作用:

if [ -d ~"/sample" ]; then
   echo "exists"
fi

通常最好使用:

if [ -d "$HOME/sample" ] ; then
   echo "exists"
fi

$HOME 通常由 Bourne shell 设置

于 2011-11-15T06:55:06.990 回答