问题
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