Operating System - HP-UX
1753404 Members
7115 Online
108793 Solutions
New Discussion

Re: variable directory-path for shell script

 
support_billa
Valued Contributor

variable directory-path for shell script

hello,

 

we have a specified structure where shell scripts are located for production and test server   :

test  server : filesystem /test and applicationdirs like applidir1  example : /test/applidir1

prod server: filesystem /prod

test source server : filesystem /testfs/central

 

i want to determine in a shell script variabel the path where the application directories are located .

the shell scripts can start with absolute or relative path

 

example : test 1:  /test/applidir1/test.sh => the directory-path   is "/test"

                   test 2: cd /test/applidir1; ./test.sh   => the directory-path   is "/test"

 

this is my idea , with specified "case"

 

SCRIPT_EXEDIR=$( dirname $0 )

case "${SCRIPT_EXEDIR}" in
  "/testfs/central/applidir1" ) DIREXE="/testfs/central" ;;
  "/test/applidir1" ) DIREXE="/test" ;;
  "/prod/applidir1" ) DIREXE="/prod"           ;;
  "."                ) case "$( pwd -P )" in
                        "/test/applidir1" ) DIREXE="/test" ;;
                        "/prod/applidir1" ) DIREXE="/prod" ;;
                        "/testfs/central/applidir1" ) DIREXE="/testfs/central" ;;
                       esac
                      ;;
esac

 

regards

2 REPLIES 2
Dennis Handly
Acclaimed Contributor

Re: variable directory-path for shell script

It looks like that will work.  You may want to handle some error cases with:

*)  error here? ;;

 

You should remove the "" from your case patterns:

case "${SCRIPT_EXEDIR}" in
/testfs/central/applidir1) DIREXE="/testfs/central" ;;
/test/applidir1)                 DIREXE="/test" ;;

support_billa
Valued Contributor

Re: variable directory-path for shell script

hello,

this is my final solution, i think a little bit difficult , but it handle all options of starting and

define a variable DIREXE ( it should include the first directory )

- relative path    (cd /prod  ; ./test.sh)                DIREXE=/prod
- absolute path  (/prod/test/test.sh)                 DIREXE=/prod


DIRNAME_SCRIPT=$( dirname $0 )
case "${DIRNAME_SCRIPT}" in
  "." | ".."  ) DIR_PWD=$( pwd -P )
              DIRNAME_EX_APPLICATION=${DIR_PWD%/*}
                case "${DIRNAME_EX_APPLICATION}" in
                  "/test" | "/prod" | "/testfs/central" ) DIREXE="${DIRNAME_EX_APPLICATION}"
                                ;;
                  * )          DIREXE="/prod"
                                ;;
                esac
                ;;
  *           ) DIRNAME_EX_APPLICATION=${DIRNAME_SCRIPT%/*}
                case "${DIRNAME_EX_APPLICATION}" in
                  "/test")   DIREXE="${DIRNAME_EX_APPLICATION}"
                             ;;
                  "/prod" )  DIREXE="${DIRNAME_EX_APPLICATION}"
                             ;;
                   "/testfs/central") DIREXE="${DIRNAME_EX_APPLICATION}"
                             ;;
                  ".."  )    DIR_PWD=$( pwd -P )
                           DIRNAME_EX_APPLICATION=${DIR_PWD%/*}
                             case "${DIRNAME_EX_APPLICATION}" in
                              "/test")   DIREXE="${DIRNAME_EX_APPLICATION}"
                                         ;;
                              "/prod" )  DIREXE="${DIRNAME_EX_APPLICATION}"
                                         ;;
                              "/testfs/central")      DIREXE="${DIRNAME_EX_APPLICATION}"
                                         ;;
                              * )        DIREXE="/prod"
                                         ;;
                             esac
                             ;;
                  * )        DIREXE="/prod"
                     ;;
                esac
                ;;
esac


regards,