bash 中的字符串比较。[[: 未找到

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12230690/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 22:37:19  来源:igfitidea点击:

String comparison in bash. [[: not found

bashshellubuntu-11.04string-comparison

提问by user1581900

I am trying to compare strings in bash. I already found an answer on how to do it on stackoverflow. In script I am trying, I am using the code submitted by Adam in the mentioned question:

我正在尝试比较 bash 中的字符串。我已经在stackoverflow上找到了关于如何做到这一点的答案。在我正在尝试的脚本中,我使用了亚当在上述问题中提交的代码:

#!/bin/bash
string='My string';

if [[ "$string" == *My* ]]
then
  echo "It's there!";
fi

needle='y s'
if [[ "$string" == *"$needle"* ]]; then
  echo "haystack '$string' contains needle '$needle'"
fi

I also tried approach from ubuntuforumsthat you can find in 2nd post

我还尝试了ubuntuforums 的方法,你可以在第二篇文章中找到

if [[ $var =~ regexp ]]; then
  #do something
fi

In both cases I receive error:

在这两种情况下,我都会收到错误消息:

[[: not found

What am I doing wrong?

我究竟做错了什么?

采纳答案by Ansgar Wiechers

[[is a bash-builtin. Your /bin/bashdoesn't seem to be an actual bash.

[[是 bash 内置的。你/bin/bash似乎不是一个真正的bash。

回答by Akos K

How you are running your script? If you did with

你如何运行你的脚本?如果你做了

$ sh myscript

you should try:

你应该试试:

$ bash myscript

or, if the script is executable:

或者,如果脚本是可执行的:

$ ./myscript

shand bashare two different shells. While in the first case you are passing your script as an argument to the sh interpreter, in the second case you decide on the very first line which interpreter will be used.

shbash是两个不同的 shell。在第一种情况下,您将脚本作为参数传递给 sh 解释器,在第二种情况下,您决定在第一行使用哪个解释器。

回答by Wiley

Is the first line in your script:

是脚本中的第一行:

#!/bin/bash

or

或者

#!/bin/sh

the sh shell produces this error messages, not bash

sh shell 产生此错误消息,而不是 bash

回答by Amedee Van Gasse

As @Ansgar mentioned, [[is a bashism, ie built into Bash and not available for other shells. If you want your script to be portable, use [. Comparisons will also need a different syntax: change ==to =.

正如@Ansgar 所提到的,[[是一种bashism,即内置于Bash 中,不适用于其他shell。如果您希望脚本可移植,请使用[. 比较还需要不同的语法:更改===.

回答by jperelli

I had this problem when installing Heroku Toolbelt

我在安装Heroku Toolbelt时遇到了这个问题

This is how I solved the problem

这就是我解决问题的方法

$ ls -l /bin/sh
lrwxrwxrwx 1 root root 4 ago 15  2012 /bin/sh -> dash

As you can see, /bin/sh is a link to "dash" (not bash), and [[is bash syntactic sugarness. So I just replaced the link to /bin/bash. Careful using rm like this in your system!

如您所见,/bin/sh 是“破折号”(不是 bash)的链接,并且[[是 bash 的语法糖度。所以我只是替换了 /bin/bash 的链接。在你的系统中小心使用 rm !

$ sudo rm /bin/sh
$ sudo ln -s /bin/bash /bin/sh

回答by Smeterlink

Specify bash instead of sh when running the script. I personally noticed they are different under ubuntu 12.10:

运行脚本时指定 bash 而不是 sh。我个人注意到它们在 ubuntu 12.10 下是不同的:

bash script.sh arg0 ... argn

bash script.sh arg0 ... argn