在bash字符串比较。 [[: 未找到 [英] String comparison in bash. [[: not found

查看:130
本文介绍了在bash字符串比较。 [[: 未找到的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在bash比较字符串。我已经找到了如何做到这一点的计算器的答案。在剧本我想,我现在用在提到的问题提交亚当code:

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

我也尝试从 ubuntu论坛的方法,你可以在第二后发现

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?

推荐答案

[是一个bash,内置。你的 /斌/庆典似乎并没有一个实际的bash。

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

这篇关于在bash字符串比较。 [[: 未找到的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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