Linux [[ 带有 bash 的条件表达式中的语法错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9483851/
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-08-06 04:53:15  来源:igfitidea点击:

Syntax error in [[ conditional expression with bash

linuxbash

提问by Sandra Schlichting

When I execute this

当我执行这个

regex='^[-a-z0-9]+$'
string='abcd1--'
if [[ $string =~ $regex ] -a ![ grep - "--" ]]
then 
    echo "valid"
else
    echo "not valid"
fi

I get

我得到

~$ sh t.sh 
t.sh: line 3: syntax error in conditional expression
t.sh: line 3: syntax error near `]'
t.sh: line 3: `if [[ $string =~ $regex ] -a [ grep - "--" ]]'
~$ 

It is suppose to return not valid.

它是假设返回not valid

Can someone figure out what's wrong?

有人能弄清楚出了什么问题吗?

采纳答案by Mat

You're mixing [and [[syntax in a strange way.

你以一种奇怪的方式混合[[[语法。

Try:

尝试:

if [[ ( $string =~ $regex ) && !( $string =~ "--" ) ]]

and check bash's man page.

并检查 bash 的手册页。