开发者

tcsh scripting : regex in if statement

what is the correct way to code this perl statment in tcsh shell script

foreach (@array) { if 开发者_运维问答(/^(pam|pom)/) { dosomething(); } }


Here's one way:

#!/bin/tcsh -f

set array = ( foo pam bar pom baz xpam pamx )
alias dosomething echo

foreach elem ($array:q)
    if ($elem:q =~ {pam,pom}*) then
        dosomething $elem:q
    endif
end

Note that the expression on the right side of the =~ operator is a file matching pattern, not a regular expression, so this solution doesn't generalize to all cases. If you need regular expression matching, you can use the expr command:

expr STRING : REGEXP

or, equivalently:

expr match STRING REGEXP
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜