2012-02-20 107 views
1

我正在寻找匹配Bash case语句中的文件扩展名模式。Bash - case语句中的模式匹配文件扩展名

到目前为止,我已经尝试/(\.).{3}/:

case ${myArray[count]} in 

*CODE*) $codeFound[count]=${myArray[count]};; 
/(\.).{3}/) $extensionFound[count]=${myArray[count]};; 

esac 

代码的模式匹配工作,但是我有文件扩展名的格局麻烦。 上述引发错误:邻近意外的标记

语法错误`(”

如果我在包裹@正则表达式()例如@(/(\.).{3}/)图案也不会被匹配

由于

回答

2

case语句中的模式匹配不使用正则表达式。从手册页:

A case command first expands word, and tries to match it against
each pattern in turn, using the same matching rules as for path‐
name expansion (see Pathname Expansion below).

您会将eith呃需要使用if语句块,或者根据case语句中的松散glob进行进一步的正则表达式检查。

case ${myArray[count]} in 

*CODE*) 
    $codeFound[count]=${myArray[count]};; 
*.*) 
    if [[ ${myArray[count]} =~ \..{3} ]]; then 
     $extensionFound[count]=${myArray[count]} 
    fi;; 

esac 
+4

你可能会使用'。???)'作为路径名扩展 – user123444555621 2012-02-20 22:24:08

+0

非常感谢! – ctdeveloper 2012-02-21 00:06:21

+1

@ Pumbaa80对于case语句+1,这将是更好的模式匹配。 – jordanm 2012-02-21 14:56:00

相关问题