bash 中的不等号波浪号

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

The Not Equal Tilde in bash

bashshellequalstilde

提问by Armageddon

Like presented in the title, What's the oppositeof "=~"operator? I really googled for that and I fund just the following solution: [[ ! $str1 =~ $str2 ]]Is there any operator that verify the oppositeof "=~"operator?

如标题所示,operator反义词是什么?我真的一派为我刚才的基金以下解决方案: 是否有任何操作员核实相反操作"=~"[[ ! $str1 =~ $str2 ]]"=~"

Thanks.

谢谢。

采纳答案by DevSolar

There is no opposing operator in bash up to version 4.3 (current at the time of this post).

在 bash 版本 4.3 之前没有反对的运算符(在本文发布时是最新的)。

[[ ! str1 =~ str2 ]]is the way to go.

[[ ! str1 =~ str2 ]]是要走的路。

For such questions, you should use maninstead of your favourite search engine. The man page of the tool involved -- bash, in this case -- is authoritative, the 'web is hearsay (unless, of course, it led you to the man page ;-) ).

对于此类问题,您应该使用man而不是您最喜欢的搜索引擎。所涉及的工具的手册页——在这种情况下是 bash——是权威的,'web 是道听途说的(当然,除非它把你带到了手册页 ;-) )。

回答by PradyJord

Answer is There is no such Operator in bashWhich does !~as its there in perl, if you do not want to use already known ( [[ ! $str1 =~ $str2 ]] )for the purpose then you shall have to use grepor perl. Something like:

答案是在 bash 中没有这样的运算符,!~它在 perl 中的作用与它一样,如果您不想( [[ ! $str1 =~ $str2 ]] )为此目的使用已知的,那么您必须使用grepperl。就像是:

x="I like negation"
y="like"


if [[ $(perl -ne "!/$y/ && print" <(echo $x)) == "$x" ]]; then 
echo "Contains"
else
echo "Doesn't contain"

But I don't find any reason to miss !~in bash as [[ ! $str1 =~ $str2 ]]solves the purpose pretty well. May be that the reason its not there in bash.

但是我没有找到任何理由!~在 bash 中错过,因为它[[ ! $str1 =~ $str2 ]]很好地解决了这个目的。可能是因为它不在 bash 中。

Consider the first comment on this Answer from DevSolar, It holds when you will go with grepor perl, which again makes [[ ! $str1 =~ $str2 ]]the best out of available.

考虑一下 DevSolar 对此答案的第一条评论,它适用于您何时使用grepperl,这再次使[[ ! $str1 =~ $str2 ]]最好的可用。