〜运算子附近的语法错误

〜运算子附近的语法错误

本文介绍了=〜运算子附近的语法错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我运行此脚本时:

#!/bin/bash

if [[ "abcd" =~ ^.*$ ]]; then
    echo "something"
fi

我得到:

./tmp2.sh: line 3: conditional binary operator expected
./tmp2.sh: line 3: syntax error near `=~'
./tmp2.sh: line 3: `if [[ "abcd" =~ ^.*$ ]]; then'

我尝试了所有发现的建议,但仍然相同:/请帮帮我!

I've tried all suggestions I've found, but still the same:/Help me, please!

推荐答案

鉴于您看到的是 bash特定的错误消息,我们可以排除 bash 之外的 strong 以外的其他东西正在运行脚本(如果它是仅POSIX功能的外壳,例如在某些系统上为sh,则您会看到一个与[[有关的错误消息.

Given that you're seeing a bash-specific error message, we can rule out that something other than bash is running the script (if it were a POSIX-features-only shell, such as sh on some systems, you'd instead see an error message relating to [[).

最可能的解释:

  • 您的 bash版本是< 3.0 ,因此不支持=~运算符.
    • 要进行验证,请在脚本中运行echo $BASH_VERSION.
    • Your bash version is < 3.0, and therefore doesn't support the =~ operator.
      • To verify, run echo $BASH_VERSION in your script.

      您看到的特定错误是bash的表达方式:我看到的字符串我不认识为运算符."

      The specific error you're seeing is bash's way of saying: "I'm seeing a string I don't recognize as an operator."

      这篇关于=〜运算子附近的语法错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-10 23:26