bash的双支架问题 [英] bash double bracket issue

查看:111
本文介绍了bash的双支架问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很新的bash脚本编程,并使用双括号时,我遇到了一个问题。我似乎无法让他们在Ubuntu 11.10服务器在所有的工作。我下面的脚本是在if_test.sh。

I'm very new to bash scripting and am running into an issue when using double brackets. I can't seem to get them to work at all in Ubuntu Server 11.10. My script below is in if_test.sh.

#!/bin/bash

if [[ "14"=="14" ]]; then 
    echo "FOO"
fi

当我运行这个简单的shell脚本输出我得到的是:if_test.sh:5:[[:未找到

When I run this simple shell script the output I get is: if_test.sh: 5: [[: not found

看来,我是从终端运行bash --version后运行GNU bash的版本4.2.10。任何帮助将大大AP preciated。谢谢!

It seems that I'm running GNU bash version 4.2.10 after running bash --version from the terminal. Any help would be greatly appreciated. Thanks!

推荐答案

问题就出在你的脚本调用。你发出命令:

The problem lies in your script invocation. You're issuing:

$ sudo sh if_test.sh

在Ubuntu系统中, / bin / sh的 破折号,而不是庆典 破折号不支持双支架关键字(或在此张贴的时间都没有,我没有双重检查)。您可以通过调用明确解决您的问题庆典而不是:

On Ubuntu systems, /bin/sh is dash, not bash, and dash does not support the double bracket keyword (or didn't at the time of this posting, I haven't double-checked). You can solve your problem by explicitly invoking bash instead:

$ sudo bash if_test.sh

另外,你可以使你的脚本的可执行文件,并依靠家当线

$ chmod +x if_test.sh
$ sudo ./if_test.sh

另外要注意的是,双方括号之间使用时 == 是的模式匹配运营商,而不是平等运营商。如果你想测试平等,您可以使用 -eq

Also note that, when used between double square brackets, == is a pattern matching operator, not the equality operator. If you want to test for equality, you can either use -eq:

if [[ "14" -eq "14" ]]; then 
    echo "FOO"
fi

双括号的:

if (( 14 == 14 )); then 
    echo "FOO"
fi

这篇关于bash的双支架问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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