如何在bash脚本中使用正则表达式对测试求反? [英] How do I negate a test with regular expressions in a bash script?
本文介绍了如何在bash脚本中使用正则表达式对测试求反?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
使用GNU bash(版本4.0.35(1)-发行版(x86_64-suse-linux-gnu)),我想使用正则表达式取消测试.例如,我想有条件地向PATH变量(如果路径尚不存在),如:
Using GNU bash (version 4.0.35(1)-release (x86_64-suse-linux-gnu), I would like to negate a test with Regular Expressions. For example, I would like to conditionally add a path to the PATH variable, if the path is not already there, as in:
TEMP=/mnt/silo/bin
if [[ ${PATH} =~ ${TEMP} ]] ; then PATH=$PATH; else PATH=$PATH:$TEMP; fi
TEMP=/mnt/silo/Scripts:
if [[ ${PATH} =~ ${TEMP} ]] ; then PATH=$PATH; else PATH=$PATH:$TEMP; fi
TEMP=/mnt/silo/local/bin
if [[ ${PATH} =~ ${TEMP} ]] ; then PATH=$PATH; else PATH=$PATH:$TEMP; fi
export PATH
我敢肯定有百万种方法可以做到这一点,但是我想知道的是,是否可以以某种方式否定条件,如(错误的)中所示:
I'm sure there are a million ways to do this, but what I would like to know is if the conditional can be negated somehow, as in (the erroneous):
TEMP=/mnt/silo/bin
if ![[ ${PATH} =~ ${TEMP} ]] ; then PATH=$PATH:$TEMP; fi
TEMP=/mnt/silo/Scripts:
if ![[ ${PATH} =~ ${TEMP} ]] ; then PATH=$PATH:$TEMP; fi
TEMP=/mnt/silo/local/bin
if ![[ ${PATH} =~ ${TEMP} ]] ; then PATH=$PATH:$TEMP; fi
export PATH
推荐答案
您说对了,只需在!
和[[
之间放置一个空格,如if ! [[
You had it right, just put a space between the !
and the [[
like if ! [[
这篇关于如何在bash脚本中使用正则表达式对测试求反?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文