在我的bash脚本中,我做:
mkdir product;
当我不止一次运行脚本时,我得到:
mkdir: product: File exists
在控制台中.
所以我期待只有在目录不存在时才运行mkdir.这可能吗?
kon*_*box 188
做一个测试
[[ -d dir ]] || mkdir dir
或者使用-p选项:
mkdir -p dir
sr0*_*853 74
if [ ! -d directory ]; then
  mkdir directory
fi
要么
mkdir -p directory
-p如果directory不存在则确保创建
使用mkdir的-p选项,但请注意它也有另一种效果.
 -p      Create intermediate directories as required.  If this option is not specified, the full path prefix of each oper-
         and must already exist.  On the other hand, with this option specified, no error will be reported if a directory
         given as an operand already exists.  Intermediate directories are created with permission bits of rwxrwxrwx
         (0777) as modified by the current umask, plus write and search permission for the owner.
尝试使用这个:-
mkdir -p dir;
注意:-这还将创建任何不存在的中间目录;例如,
查看mkdir -p
或者试试这个:-
if [[ ! -e $dir ]]; then
    mkdir $dir
elif [[ ! -d $dir ]]; then
    echo "$Message" 1>&2
fi