Date: Wed, 14 Nov 2007 15:20:40 +0000 From: Vince <jhary@unsane.co.uk> To: Martin McCormick <martin@dc.cis.okstate.edu> Cc: freebsd-questions@freebsd.org Subject: Re: /bin/sh Can one Easily Strip Path Name from $0? Message-ID: <473B1248.3000502@unsane.co.uk> In-Reply-To: <200711141508.lAEF8veZ083725@m.it.okstate.edu> References: <200711141508.lAEF8veZ083725@m.it.okstate.edu>
next in thread | previous in thread | raw e-mail | index | archive | help
Martin McCormick wrote:
> I am ashamed to admit that I have been writing shell
> scripts for about 15 years but this problem has me stumped. $0
> is the shell variable which contains the script name or at least
> what name is linked to the script. The string in $0 may or may
> not contain a path, depending upon how the script was called. It
> is easy to strip off the path if it is always there
>
> #! /bin/sh
> PROGNAME=`echo $0 |awk 'BEGIN{FS="/"}{print $NF}'`
> echo $PROGNAME
>
> That beautifully isolates the script name but if you happen to
> call the script without prepending a path name such as when the
> script is in the execution path, you get an error because there
> are no slashes in the string so awk gets confused.
>
> Is there a better way to always end up with only the script name and
> nothing else no matter whether the path was prepended or not?
>
basename should do it.
> Thank you.
>
> Martin McCormick
> _______________________________________________
> freebsd-questions@freebsd.org mailing list
> http://lists.freebsd.org/mailman/listinfo/freebsd-questions
> To unsubscribe, send any mail to "freebsd-questions-unsubscribe@freebsd.org"
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?473B1248.3000502>
