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

Tim Watts timtw at earthlink.net
Thu Aug 9 14:25:38 EDT 2007


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



More information about the Ale mailing list