Re: What directory is script in??? Bad description

From: Bill Seivert (seivert_at_pcisys.net)
Date: 04/29/05


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



Relevant Pages

  • Re: [opensuse] alias failure
    ... So it's just an accident that the following aliases all work as I ... It's uncommon for interactive shells to have any positional parameters ... Unlike the very limited capabilities of aliases, shell procedures ... I made a script with nothing but that in it, ...
    (SuSE)
  • Re: [PS] - Help with becoming better
    ... If I give it ps for input, then I get back Get-Process which is of course ... I think this does pretty much the same as your script. ... I write an actual script however, I would rather avoid the use of aliases as ...
    (microsoft.public.windows.server.scripting)
  • Re: [PS] - Help with becoming better
    ... I didn't take it to handling parameters, ... I think this does pretty much the same as your script. ... I write an actual script however, I would rather avoid the use of aliases as ...
    (microsoft.public.windows.server.scripting)
  • Re: [PS] - Help with becoming better
    ... If I give it ps for input, then I get back Get-Process which is of course ... I think this does pretty much the same as your script. ... I write an actual script however, I would rather avoid the use of aliases as ...
    (microsoft.public.windows.server.scripting)
  • Re: alias within bash-script
    ... > The script prints nothing. ... > How to invoke alias within script? ... aliases are not passed to subshells, ... Then within your .bashrc file, ...
    (comp.unix.shell)