在我的bash脚本中,我做:
mkdir product;
Run Code Online (Sandbox Code Playgroud)
当我不止一次运行脚本时,我得到:
mkdir: product: File exists
Run Code Online (Sandbox Code Playgroud)
在控制台中.
所以我期待只有在目录不存在时才运行mkdir.这可能吗?
kon*_*box 188
做一个测试
[[ -d dir ]] || mkdir dir
Run Code Online (Sandbox Code Playgroud)
或者使用-p选项:
mkdir -p dir
Run Code Online (Sandbox Code Playgroud)
sr0*_*853 74
if [ ! -d directory ]; then
mkdir directory
fi
Run Code Online (Sandbox Code Playgroud)
要么
mkdir -p directory
Run Code Online (Sandbox Code Playgroud)
-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.
Run Code Online (Sandbox Code Playgroud)
尝试使用这个:-
mkdir -p dir;
Run Code Online (Sandbox Code Playgroud)
注意:-这还将创建任何不存在的中间目录;例如,
查看mkdir -p
或者试试这个:-
if [[ ! -e $dir ]]; then
mkdir $dir
elif [[ ! -d $dir ]]; then
echo "$Message" 1>&2
fi
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
131687 次 |
| 最近记录: |