Re: What directory is script in??? Bad description
From: Bill Seivert (seivert_at_pcisys.net)
Date: 04/29/05
- Next message: David Schwartz: "Re: memory usage by a process"
- Previous message: Niraj123: "memory usage by a process"
- In reply to: Måns Rullgård: "Re: What directory is script in??? Bad description"
- Next in thread: Henry Townsend: "Re: What directory is script in??? Bad description"
- Reply: Henry Townsend: "Re: What directory is script in??? Bad description"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Thu, 28 Apr 2005 23:19:07 -0600
Måns Rullgård wrote:
> phil_gg04@treefic.com writes:
>
>
>>>What [...] to recognize the physical
>>>directory the [currently executing shell] script is stored in.
>>
>>This isn't easy. I generally do this:
>>
>>case $0 in
>>/*) SCRIPT=$0 ;;
>>./*) SCRIPT="`pwd`/$0" ;;
>>*) SCRIPT=`which $0` ;;
>>esac
>>
>>SCRIPT_DIR=`dirname $SCRIPT`
>>
>>$0 contains the name of the script when it starts. The case considers
>>three possibilites: it could be an absolute pathname, e.g.
>>"/usr/bin/foo", or a relative pathname ("./foo"), or a bare name in
>>which case you can look it up in the path.
>>
>>I think there are some problem cases, but I don't remember them now.
>>Can anyone spot anything obvious?
>
>
> A relative path doesn't necessarily start with "./". It could just as
> well be something like "foo/bar/script", or "../foo/script". A better
> (and simpler) solution is to always use "which":
>
> SCRIPT="`which \"$0\"`"
> SCRIPT_DIR="`dirname \"$SCRIPT\"`"
>
> Be sure to quote everything properly, or you might be in for a
> surprise. If your shell supports it, the $(command) construct makes
> quoting a bit less messy.
>
There may be a problem with 'which'. It typically sources the user's
.cshrc and any aliases, and then determines where the program or script
is. The problem is that if the user has changed PATH, this change is
not recognized by which. Better to use type "name".
Bill Seivert
- Next message: David Schwartz: "Re: memory usage by a process"
- Previous message: Niraj123: "memory usage by a process"
- In reply to: Måns Rullgård: "Re: What directory is script in??? Bad description"
- Next in thread: Henry Townsend: "Re: What directory is script in??? Bad description"
- Reply: Henry Townsend: "Re: What directory is script in??? Bad description"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|