Bash - 如何根据正则表达式的匹配/不匹配结果运行命令

时间:2013-05-09 04:01:01

标签: regex bash shell command-line

我正在~/.bashrc编写Linux bash函数,为我自动执行某些操作

#!/bin/bash
......
......

function cog102start
{
    LD_LIBRARY_PATH=/opt/ibm/cognos/c102_6/cgi-bin
    httpd64start
    DB2START=$(sudo -u db2inst1 /home/db2inst1/sqllib/adm/db2start)

    ________need something here_________
}

DB2START的输出将包含

SQL1036N DB2START process was successful

SQL1026N The database manager is already active

如何测试if语句以决定下一步做什么?

我正在尝试

if [ [ $DB2START =~ ^(SQL1036N|SQL1026N)$ ] ]; then dosomething; fi

给我一​​个语法错误......我不知道还有什么可以尝试。

2 个答案:

答案 0 :(得分:3)

正确的语法是

if [[ $DB2START =~ ^(SQL1036N|SQL1026N)$ ]]; then dosomething; fi

[[]]是单一令牌;两者之间不允许有空格。

答案 1 :(得分:2)

if [ [ $DB2START =~ ^(SQL1036N|SQL1026N)$ ] ]; then dosomething; fi
    ^                                      ^
     \                                    /
      --- no space here        or here ---