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

查看:56
本文介绍了如果文件夹不存在,如何使用 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 ]; then
  mkdir -p /home/mlzboy/b2c2/shared/db;
fi

其次,由于您使用 -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天全站免登陆