Original Path

I've got one I haven't been able to figure out.

  1. A shell script exists- /junk/junk1/junk2/junk3.sh
  2. 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

How do I figure this out?

TIA
Paul

#!/bin/bash

Home=`pwd`
Base=`basename $0`
ScriptDir=`dirname $0`
# or ScriptDir=`echo $0 | sed -e "s/$Base$//"`

echo "ScriptDirectory: $ScriptDir, CalledFrom: $Home, ScriptName: $Base"

HTH

[Edited by mib on 05-17-2001 at 06:32 AM]

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.".

Here is what I have in my "junk.sh" script:

#! /bin/sh
pwd
CURR_ENV=`pwd | xargs -i expr {} : '\(.*devl\)' \| {} : '\(.*stage\)' \| {} : '\(.*prod\)' \| "Invalid." : '\(.*Invalid.\)' | xargs basename`
echo "Hi this is $CURR_ENV"

Here is the path where the script resides:

/sftwr/devl/proj1/bin/

Here is the pwd I am in when I run the script:

/home/myhome/

Here is the command I execute:

/sftwr/devl/proj1/bin/junk.sh

What I see is:

/home/myhome/
Hi this is Invalid.

What I expect to see is:

/sftwr/devl/proj1/bin/
Hi this is devl

What am I missing?

I got it. Thanx.

Here is the command.

CURR_ENV=`echo $0 | xargs -i expr {} : '\(.*devl\)' \| {} : '\(.*stage\)' \| {} : '\(.*prod\)' \| "Invalid." : '\(.*Invalid.\)' | xargs basename`

$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".

Actually, this one is much better.

CURR_ENV=`echo $0 | xargs -i expr {} : '\(.*devl\)' \| {} : '\(.*stage\)' \| {} : '\(.*prod\)' \| \`pwd\` | xargs -i expr {} : '\(.*devl\)' \| {} : '\(.*stage\)' \| {} : '\(.*prod\)' \| "Invalid." : '\(.*Invalid.\)' | xargs basename`

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.