2013-08-07 44 views
-1

这个脚本检查一个目录是另一个父目录。如何工作这个脚本?什么是=〜在下面的脚本

 T() { 
      if [[ "$2" =~ ${1}['/'?] ]] ; then 
       echo "$2 is child of $1" 
       return 0 
      else 
       echo "$2 is NOT child of $1 ($?)" 
       return 1 
      fi 
     } 
+3

阅读['bash'手册页](http://linux.die.net/man/1/bash)。 –

+1

你的问题含糊不清。你想知道这个脚本是如何工作的 - 特别是'〜='在做什么,或者如何使用这个脚本?请澄清一下。 –

+0

为了避免混淆这里发生的事情,[我们自由地对这个问题做了一些实验](http://meta.stackexchange.com/questions/192242/you-made-too-few-changes-to-在-后对提高-它)。 – Bobby

回答

1

从手册:

An additional binary operator, ‘=~’, is available, with the same precedence as ‘==’ and ‘!=’. When 
it is used, the string to the right of the operator is considered an extended regular expression 
and matched accordingly (as in regex3)). The return value is 0 if the string matches the pattern, 
and 1 otherwise. 
2

这是一个正则表达式匹配,一个将字符串与模式匹配的有用工具。 This link可能会帮助你。

2

它是一个正则表达式模式匹配运算符。请参阅Conditional constructs下的bash参考手册 - 并查找[[ ... ]]结构。