如何运用Bash创建一个文件夹,如果它不存在? [英] How to use Bash to create a folder if it doesn't already exist?

查看:443
本文介绍了如何运用Bash创建一个文件夹,如果它不存在?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#!/bin/bash
if [!-d /home/mlzboy/b2c2/shared/db]; then
    mkdir -p /home/mlzboy/b2c2/shared/db;
fi;

这似乎并没有工作。谁能帮助?

This doesn't seem to work. Can anyone help?

推荐答案

首先,在bash[只是一个命令,它预计字符串]作为最后一个参数,所以右括号前的空白(以及!作为与-d,这需要两个单独的论点太)之间,是重要的:

First, in bash "[" is just a command, which expects string "]" as a last argument, so the whitespace before the closing bracket (as well as between "!" and "-d" which need to be two separate arguments too) is important:

if [ ! -d /home/mlzboy/b2c2/shared/db ]; ...

其次,由于您使用-p开关设置为的mkdir ,这个检查是没用的,因为这是摆在首位什么呢。只要写:

Second, since you are using -p switch to mkdir, this check is useless, because this is what does in the first place. Just write:

mkdir -p /home/mlzboy/b2c2/shared/db;

和多数民众赞成它。

这篇关于如何运用Bash创建一个文件夹,如果它不存在?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆