/bin/sh Can one Easily Strip Path Name from $0?

2007-11-14 Thread Martin McCormick
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?

Thank you.

Martin McCormick
___
freebsd-questions@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to [EMAIL PROTECTED]


Re: /bin/sh Can one Easily Strip Path Name from $0?

2007-11-14 Thread Erik Trulsson
On Wed, Nov 14, 2007 at 09:08:57AM -0600, 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?
 

The basename(1) command seems to do what you want.



-- 
Insert your favourite quote here.
Erik Trulsson
[EMAIL PROTECTED]
___
freebsd-questions@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to [EMAIL PROTECTED]


Re: /bin/sh Can one Easily Strip Path Name from $0?

2007-11-14 Thread Bill Moran
In response to Martin McCormick [EMAIL PROTECTED]:

   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 $0

-- 
Bill Moran
http://www.potentialtech.com
___
freebsd-questions@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to [EMAIL PROTECTED]


Re: /bin/sh Can one Easily Strip Path Name from $0?

2007-11-14 Thread Vince
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 [EMAIL PROTECTED]

___
freebsd-questions@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to [EMAIL PROTECTED]


Re: /bin/sh Can one Easily Strip Path Name from $0?

2007-11-14 Thread Martin McCormick
The basename utility does the trick. Thanks to all of you
who answered.

Martin
___
freebsd-questions@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to [EMAIL PROTECTED]


Re: /bin/sh Can one Easily Strip Path Name from $0?

2007-11-14 Thread Michaël Grünewald
Martin McCormick [EMAIL PROTECTED] writes:

   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

As said by others, you can use `basename`. Apart from this, you can
fix your old habit by prepending a '/' before '$0', like this:

#! /bin/sh
PROGNAME=`echo /$0 |awk 'BEGIN{FS=/}{print $NF}'`
echo $PROGNAME

Last, you can also use variable expansions mechanisms by saying:

PROGNAME=${0##*/}

The main difference with `basename` way is that the latter do not call
a subprogram.

(If you are sure there is no space in your name, you can remove the
quotes, but are you sure?)

See `Parameter Expansion' in sh(1).
-- 
Best wishes,
Michaël
___
freebsd-questions@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to [EMAIL PROTECTED]