使用[!时,在Bash的IF-ELSE条件下找不到命令. -d"$ DIR"] [英] Command not found in Bash's IF-ELSE condition when using [! -d "$DIR"]

查看:496
本文介绍了使用[!时,在Bash的IF-ELSE条件下找不到命令. -d"$ DIR"]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的代码

#!/bin/bash 
DIR="test_dir/";
if [! -d "$DIR"]; then
    # If it doesn't create it
    mkdir $DIR
fi

但是为什么要执行它给我呢?

But why executing it gave me this:

./mycode.sh: line 16: [!: command not found

什么是正确的方法?

推荐答案

在[和!之间添加空格.还有[]之前.

Add space between [ and !. And before ] as well.

#!/bin/bash 
DIR="test_dir/";
if [ ! -d "$DIR" ]; then
    # If it doesn't create it
    mkdir $DIR
fi

引用变量也是一个好主意:

It's also a good idea to quote your variable:

    mkdir "$DIR"

这篇关于使用[!时,在Bash的IF-ELSE条件下找不到命令. -d"$ DIR"]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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