tcsh scripting : regex in if statement

萝らか妹 提交于 2019-12-12 04:59:51

问题


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

foreach (@array) { if (/^(pam|pom)/) { dosomething(); } }


回答1:


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


来源:https://stackoverflow.com/questions/4521799/tcsh-scripting-regex-in-if-statement

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!