[X-Unix] Shell Script: How get parent path to invoke a sister tool?

Eric F Crist ecrist at secure-computing.net
Fri May 23 07:37:18 PDT 2008


On May 23, 2008, at 9:28 AM, Eric F Crist wrote:

> On May 23, 2008, at 9:21 AM, Jerry Krinock wrote:
>
>> I want to ship someone a directory containing two files: a shell  
>> script and a tool.  Within the shell script, I'd like to invoke the  
>> tool.  And I want it idiot-proofed to work regardless of where the  
>> user drops my directory.
>>
>> So, I need the path to the tool within the script.  It seems like a  
>> bash shell script should have an environment variable available  
>> giving the path to it's own parent directory, but I don't see any  
>> such thing in printenv output.
>>
>> How can I do this?
>
>
> Jerry,
>
> Simply require that the tool which is called be within the same  
> directory as the script.  From there, you can figure out you current  
> working directory, or simply call the tool with ./tool_name.
>
> Hope this helps.

Sorry for the self-reply, but there's another method, building off  
what I stated above.  You may, if the user calls your utility from  
another directory, need to know that directory information.  $0 is a  
special variable containing the name of the script itself, feeding  
this to the command dirname will get you a valid path to the script.

#!/bin/sh
echo "I live in `dirname $0`"

Simply put this in test.sh within your home directory, and do the  
following to see:

cd /
sh ~/test.sh

Hope this clarifies a bit.

-----
Eric F Crist
Secure Computing Networks




More information about the X-Unix mailing list