[ale] How to expand to full-path name?

Ken Alexander kenry at alexandertech.biz
Thu Aug 9 14:49:54 EDT 2007


Jeff is right. Everything there should work in recent versions of bash,
ksh, or sh. If you have problems on Solaris, use an interpreter
from /usr/xpg4/bin.

-KA

On Thu, 2007-08-09 at 14:28 -0400, Jeff Lightner wrote:

> Solaris has korn shell (ksh) in addition to bourne shell.  Much of what
> runs in bash will run in ksh.   You'd have to change the interpreter
> line (#!/bin/ksh or wherever it is).
> 
> -----Original Message-----
> From: ale-bounces at ale.org [mailto:ale-bounces at ale.org] On Behalf Of Tim
> Watts
> Sent: Thursday, August 09, 2007 2:26 PM
> To: Atlanta Linux Enthusias
> Subject: Re: [ale] How to expand to full-path name?
> 
> Thanks Ken. That looks promising. I'll give it a try. 
> 
> BTW, this has to be strictly-Bourne shell since it will have to run on
> Solaris 
> too. Are there any bash-isms hidden in there?
> 
> On Thursday 09 August 2007 11:58 am, Ken Alexander wrote:
> > On Thu, 2007-08-09 at 11:54 -0400, Ken Alexander wrote:
> > > On Thu, 2007-08-09 at 10:57 -0400, Tim Watts wrote:
> > > > Hi,
> > > >
> > > > Suppose I have MyScript in /home/me/bin which does this:
> > > >
> > > > 	#!/bin/sh
> > > > 	SCRIPT_HOME=`dirname $0`
> > > > 	echo home=$SCRIPT_HOME
> > > >
> > > > Now I run it from /home/me/bin which produces:
> > > >
> > > > 	home=.
> > > >
> > > > How can I expand SCRIPT_HOME into the full path name (i.e.
> > > > /home/me/bin)? _______________________________________________
> > > > Ale mailing list
> > > > Ale at ale.org
> > > > http://www.ale.org/mailman/listinfo/ale
> > >
> > > $0 is whatever you typed in at the command line. So you can call the
> > > script with the absolute path. Instead of "./script" use
> > > "/full/path/to/script".
> > >
> > > You can use `pwd` as someone already said, but that's going to give
> > > you whichever directory you are in when the script is run. Not
> > > necessarily the directory the script resides in.  So, if you need
> this
> > > to work from anywhere, you can maybe do an if statement to check
> > > whether $0 gives you an absolute path.
> > >
> > > quick and dirty ....
> > >
> > > script_home=$(dirname $0)
> > > if [ ${script_home:0:1} != '/' ]
> > > then
> > >    cd ${script_home}
> > >    script_home=`pwd` # corrected line
> > >    cd - >  /dev/null
> > > fi
> > > echo home=${script_home}
> > >
> > > -KA
> > >
> > > _______________________________________________
> > > Ale mailing list
> > > Ale at ale.org
> > > http://www.ale.org/mailman/listinfo/ale
> >
> > oops, typo. see above
> _______________________________________________
> Ale mailing list
> Ale at ale.org
> http://www.ale.org/mailman/listinfo/ale
> _______________________________________________
> Ale mailing list
> Ale at ale.org
> http://www.ale.org/mailman/listinfo/ale
-------------- next part --------------
An HTML attachment was scrubbed...




More information about the Ale mailing list