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

发布于 2025-01-20 14:35:24 字数 702 浏览 0 评论 0原文

我试图比较bash中的字符串。我已经找到了关于如何在 Stackoverflow 上找到答案。在脚本中,我正在尝试,我正在使用Adam提交的代码中提到的问题:

#!/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

我还尝试了 ubuntuforums 您可以在第二篇文章中找到

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

我收到错误的第二篇文章:

[[: not found

我在做什么错?

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:

#!/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 ubuntuforums that you can find in 2nd post

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

In both cases I receive error:

[[: not found

What am I doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

才能让你更想念 2025-01-27 14:35:24

[[是bash-builtin。您的/bin/bash似乎不是实际的bash。

从评论中:

添加#!/bin/bash在文件的顶部

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

From a comment:

Add #!/bin/bash at the top of file

短叹 2025-01-27 14:35:24

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

$ sh myscript

你应该尝试:

$ bash myscript

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

$ ./myscript

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

How you are running your script?
If you did with

$ sh myscript

you should try:

$ bash myscript

or, if the script is executable:

$ ./myscript

sh and bash are 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.

沉睡月亮 2025-01-27 14:35:24

脚本中的第一行是:

#!/bin/bash

还是

#!/bin/sh

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

Is the first line in your script:

#!/bin/bash

or

#!/bin/sh

the sh shell produces this error messages, not bash

静若繁花 2025-01-27 14:35:24

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

if [ $MYVAR = "myvalue" ]; then
    echo "true"
else
    echo "false"
fi

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 =.

if [ $MYVAR = "myvalue" ]; then
    echo "true"
else
    echo "false"
fi
隔纱相望 2025-01-27 14:35:24

I had this problem when installing 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”(不是bash),[是bash句法糖。因此,我只是更换了指向 /bin /bash的链接。 在系统中使用这样的RM小心!

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

I had this problem when installing 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!

$ sudo rm /bin/sh
$ sudo ln -s /bin/bash /bin/sh
荒芜了季节 2025-01-27 14:35:24

如果您知道自己正在使用 bash,但仍然收到此错误,请确保在 if 中写入空格。

[[1==1]] # This outputs error

[[ 1==1 ]] # OK

If you know you're on bash, and still get this error, make sure you write the if with spaces.

[[1==1]] # This outputs error

[[ 1==1 ]] # OK
简单气质女生网名 2025-01-27 14:35:24

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

bash script.sh arg0 ... argn

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

bash script.sh arg0 ... argn

唔猫 2025-01-27 14:35:24

在你的终端中执行:

sudo update-alternatives --install /bin/sh sh /bin/bash 100

Execute in your terminal:

sudo update-alternatives --install /bin/sh sh /bin/bash 100
零度℉ 2025-01-27 14:35:24

我遇到了同样的错误(在 Mac 上) - 但由于事实上,我在“[”之前输入了一个空格,并且选项键已经按下,用于输入“[”。所以空格字符不是我想要输入的空格字符,而是一个“不间断空格”。在文本编辑器中不可见 - 但导致“[[:找不到命令”错误。

I had the same error (on a Mac) – but due to the fact, that I entered a space before the "[" with the option key already down for typing the "[". So the space character was not the space character I wanted to type, but a "nonbreaking space". Not visible in the text editor – but caused a "[[ : command not found" error.

优雅的叶子 2025-01-27 14:35:24

使文件可执行,然后在不使用 sh 的情况下执行。

  1. 通过 $ chmod +x filename 使其可执行,
  2. 然后使用 ./filename 代替 sh filename

Make the file executable and then execute without sh.

  1. make it executable by $ chmod +x filename
  2. then instead of sh filename use ./filename
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文