bash中的字符串比较。 [[: 未找到
我试图比较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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
[[
是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你如何运行你的脚本?
如果你这样做了,
你应该尝试:
或者,如果脚本是可执行的:
sh和bash是两个不同的 shell。在第一种情况下,您将脚本作为参数传递给 sh 解释器,而在第二种情况下,您在第一行决定将使用哪个解释器。
How you are running your script?
If you did with
you should try:
or, if the script is executable:
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.
脚本中的第一行是:
还是
sh shell 产生此错误消息,而不是 bash
Is the first line in your script:
or
the sh shell produces this error messages, not bash
正如 @Ansgar 提到的,
[[
是一种 bashism,即内置于 Bash 中并且不适用于其他 shell。如果您希望脚本可移植,请使用[
。比较还需要不同的语法:将==
更改为=
。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=
.I had this problem when installing Heroku Toolbelt
This is how I solved the problem
As you can see, /bin/sh is a link to “ dash”(不是bash),
[
是bash句法糖。因此,我只是更换了指向 /bin /bash的链接。 在系统中使用这样的RM小心!I had this problem when installing Heroku Toolbelt
This is how I solved the problem
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!如果您知道自己正在使用 bash,但仍然收到此错误,请确保在 if 中写入空格。
If you know you're on bash, and still get this error, make sure you write the if with spaces.
运行脚本时指定 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
在你的终端中执行:
Execute in your terminal:
我遇到了同样的错误(在 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.
使文件可执行,然后在不使用 sh 的情况下执行。
Make the file executable and then execute without sh.