-1

this script check that a directory is a parent of another. how to work this script?

     T() {
          if [[ "$2" =~ ${1}['/'?] ]] ; then
              echo "$2 is child of $1"
              return 0
          else
              echo "$2 is NOT child of $1 ($?)"
              return 1
          fi
         }
Bobby
  • 11,419
  • 5
  • 44
  • 69
amir nami
  • 11
  • 3
  • 3
    Read the [`bash` manual page](http://linux.die.net/man/1/bash). – Some programmer dude Aug 07 '13 at 07:52
  • 1
    Your question is phrased ambiguously. Do you want to know how this script is working - in particular what the `~=` is doing, or how to use this script? Please clarify it. – Chen Levy Aug 07 '13 at 08:06
  • To avoid confusion about what's going on here, [we took the liberty to do some experiments with this questions](http://meta.stackexchange.com/questions/192242/you-made-too-few-changes-to-the-post-to-improve-it). – Bobby Aug 07 '13 at 08:13
  • @Bobby - please can you can stop now. – ChrisF Aug 07 '13 at 08:28

3 Answers3

2

It's a regular expression match, a helpful tool to match a string to a pattern. This link may help you.

Miguel Prz
  • 13,718
  • 29
  • 42
2

It is a regular expression pattern match operator. See the bash reference manual under Conditional constructs — and look for the [[ ... ]] construct.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
1

From manual:

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.
Matthias
  • 8,018
  • 2
  • 27
  • 53