Bash脚本:“ [[:找不到”

何基et

这是我的代码:

#!/bin/bash
if [[ -d ~/viwiki ]]; then
 cd ~/viwiki
else
 mkdir ~/viwiki
 cd ~/viwiki
fi
if ! [[ -d ./log ]]; then
 mkdir log
 mkdir log/log
 mkdir "log/wget"
elif ! [[ -d ./log/log ]]; then
 mkdir log/log
elif ! [[ -d "./log/wget" ]]; then
 mkdir "log/wget"
fi

运行时,出现错误:

tuankiet65@UbuntuPC:~$ sh viwik/test2.sh
viwik/test2.sh: 2: viwik/test2.sh: [[: not found
viwik/test2.sh: 8: viwik/test2.sh: [[: not found

我怎样才能解决这个问题?

丹尼尔·安德森(Daniel Andersson)

我猜如果你跑步

readlink -f $(which sh)

您将不会获得Bash作为返回值,而是获得Dash。您具有正确的序言,但是仅当您在使脚本./test2.sh可执行后运行脚本时才有意义。

现在,您可以通过sh解释器(可能是Dash)强制运行脚本,并且该[[]]构造是Bash特定构造。

这就是为什么?”。如果仅将双括号替换为单括号(并更改#!/bin/bash#!/bin/sh,因为脚本现在无论如何都仅使用POSIX函数),则它应按预期运行。


在Debian上的演示test.sh,内容如下:

#!/bin/bash
if [[ "string" == "string" ]]; then
    echo This is Bash
fi

有时候是这样的:

$ readlink -f $(which sh)
/bin/dash
$ sh test.sh 
test.sh: 2: test.sh: [[: not found
$ bash test.sh 
This is Bash
$ chmod 755 test.sh
$ ./test.sh
This is Bash

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章