2 Replies Latest reply on Jun 23, 2014 7:30 PM by Paul M.

    Get initial directories in bash script

    user5108636

      I have a variable TEST in a bash script

      TEST=/ABCService/ZeroComposite/deploy/sca_XYZ.jar

       

      I want to parse this TEST variable to have a new variable COMPOSITE_DIR with value "/ABCService/ZeroComposite". Looks pretty simple but I am on it for about 3 hours now. Exhausted all option with regex, dirname, basename etc. There has to be a simple way to do this.

       

      Thanks

        • 1. Re: Get initial directories in bash script
          Dude!

          Giving up after only 3 hours already?

           

          How about the following:

           

          var=$(echo $TEST | rev | cut -d'/' -f3- | rev)

          echo $var

          /ABCService/ZeroComposite

           

          The cut command allows to specify a character delimiter that acts like a field separator. The parameter -f3- would normally extract the string starting at field number 3 and all remaining fields. The trick is to reverse the order of the string so that the cut command will cut from the end to the beginning, instead from the beginning to the end, and then reverse the string again to get the result.

          • 2. Re: Get initial directories in bash script
            Paul M.

            I have a variable TEST in a bash script

            TEST=/ABCService/ZeroComposite/deploy/sca_XYZ.jar

             

            I want to parse this TEST variable to have a new variable COMPOSITE_DIR with value "/ABCService/ZeroComposite".

            # TEST=/ABCService/ZeroComposite/deploy/sca_XYZ.jar

            # dirname `dirname $TEST`

            /ABCService/ZeroComposite

            #