【问题标题】:ksh regex - how to insert variableksh 正则表达式 - 如何插入变量
【发布时间】:2014-10-29 18:25:36
【问题描述】:

Ksh 正则表达式问题(我可以用 sh 轻松完成类似的脚本)。

问题很简单:当我尝试在正则表达式中插入 $variable 时,正则表达式失败。 但是当我用它的实际值替换 $variable 时它工作正常。 我需要在 $variable 中使用多个 Ksh 正则表达式,该怎么做?

示例 1 正则表达式的作用是 echo "hourra" :

#!/bin/ksh
file=BEFORE_ONE_AFTER
name="(ONE|TWO)"

if [[ $file = @(BEFORE_(ONE|TWO)_AFTER) ]] ; then
echo "hurra!"
fi

示例 2 正则表达式失败,没有回显:

#!/bin/ksh
file=BEFORE_ONE_AFTER
name="(ONE|TWO)"

if [[ $file = @(BEFORE_${name}_AFTER) ]] ; then
    echo "ohde!"
fi

【问题讨论】:

    标签: regex variables ksh


    【解决方案1】:

    在 ubuntu 上为我工作:

    efs@efs-VirtualBox:~$ cat x.sh
    #!/bin/ksh
    file=BEFORE_ONE_AFTER
    name="(ONE|TWO)"
    
    if [[ $file = @(BEFORE_${name}_AFTER) ]] ; then
      echo "ohde!"
    fi
    efs@efs-VirtualBox:~$ ./x.sh
    ohde!
    efs@efs-VirtualBox:~$ 
    

    【讨论】:

      【解决方案2】:

      使用 AIX ksh,在参数周围添加引号。

       #!/bin/ksh
       file=BEFORE_ONE_AFTER
       name="(ONE|TWO)"
      
       if [[ $file = @(BEFORE_"${name}"_AFTER) ]] ; then
           echo "ohde!"
       fi
      

      $./test.sh  
      ohde!  
      $  
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-04-14
        • 1970-01-01
        • 2015-07-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-04-19
        相关资源
        最近更新 更多