-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
         }
4

3 回答 3

2

这是一个正则表达式匹配,是一个将字符串与模式匹配的有用工具。 此链接可能会对您有所帮助。

于 2013-08-07T07:52:09.157 回答
2

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

于 2013-08-07T07:52:15.633 回答
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.
于 2013-08-07T07:54:18.913 回答