From within the above named shell script, I want to know the full path of the above named shell script. Even if I execute the shell script from a directory other than the one it resides in, I want to know the directory where the script resides, not the path where the script was executed from.
For example:
I'm in: /home/myhome
and I put the following on the command line: /junk/junk1/junk2/junk3.sh
I want the following stored to a variable inside the script: /junk/junk1/junk2
I don't want the following stored to a variable inside the script: /home/myhome
That works fine by itself, now I want to pull out the "environment" (development, stage or production) I am in by examining the path where the script resides. The "environment" may not be in the same place in the path for various scripts on various machines, therefore I can't use cut to parse the pwd. If the path to the script doesn't contain devl, stage or prod, then the CURR_ENV variable should contain "Invalid.".
$CURR_ENV will contain the string "devl" or "stage" or "prod" if the shell script is in a directory that contains one of those words.
$CURR_ENV will contain the string "Invalid." if the shell script is NOT in a directory that contains "devl" or "stage" or "prod".
In response to the first question posted:
I use "type" or "whence" to determine where the script/pgm/command/etc is executing from (On some systems type is an alias for whence -v)
whence scriptname will return the absolute path + script file.
I have frequently done the following inside a script:
cd $(dirname $(whence $0)) to change to the directory where the script is running.
(I greatly prefer $() notation to `` backquotes)
whence/type id's functions, aliases, shell builtins etc.