MLUG: Re: [MLUG] shell script - find shell script parent directory?
Re: [MLUG] shell script - find shell script parent directory?
Email address obfuscation in effect -- please click here to turn it off.

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
On 3/3/08, Mike Miller <EMAIL:PROTECTED> wrote:
> On Mon, 3 Mar 2008, Mike Miller wrote:
>
> > On Mon, 3 Mar 2008, Robert Citek wrote:
> >
> >> On Sat, Mar 1, 2008 at 12:16 PM, Mike Miller <EMAIL:PROTECTED>
> >> wrote:
> >>
> >>> We had a fairly long discussion about this on TCLUG (Twin Cities) and
> >>> I thought you guys also would find it interesting and informative.
> >>> It started with this question about bash scripting:
> >>>
> >>>     I'm trying to find a variable or a command that will tell me where
> >>>     the currently executing script lives.
> >>
> >> I'll take a stab:
> >>
> >> $cat foo.sh
> >> #!/bin/bash
> >> echo $0
>
>
> Robert wrote to me off-list because he wanted to give the rest of you a
> chance to come up with something.  I haven't heard anything, so maybe the
> interest isn't there, or maybe you were waiting to see how Robert would
> respond to my comments.  In case it was the latter, this is what he came
> up with next:
>
> #!/bin/bash
> ( cd "${0%/*}" ; pwd -P )
>
> That is very cool and it is similar to what we came up with on TCLUG.
> There are several little tricks worth adding though.  What happens if the
> filename or directory name begins with a minus sign?

I guess you could escape it...

> How can we be sure
> that the bash builtin versions of cd and pwd are used?

Can you over-ride that from happening? In any case, a horrible hack
would be to parse the output of "which", which should report that
these are built-ins. Note: I don't have a appropriate test bed here at
work for any of this.

jking

_______________________________________________
members mailing list
EMAIL:PROTECTED
http://mlug.missouri.edu/mailman/listinfo/members