From owner-freebsd-questions@FreeBSD.ORG Wed Nov 14 15:20:52 2007 Return-Path: Delivered-To: freebsd-questions@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id D437A16A468 for ; Wed, 14 Nov 2007 15:20:52 +0000 (UTC) (envelope-from jhary@unsane.co.uk) Received: from unsane.co.uk (unsane-pt.tunnel.tserv2.fmt.ipv6.he.net [IPv6:2001:470:1f03:1f1::2]) by mx1.freebsd.org (Postfix) with ESMTP id 5842B13C4BB for ; Wed, 14 Nov 2007 15:20:52 +0000 (UTC) (envelope-from jhary@unsane.co.uk) Received: from prawn.unsane.co.uk (150.117-84-212.staticip.namesco.net [212.84.117.150]) (authenticated bits=0) by unsane.co.uk (8.14.0/8.14.0) with ESMTP id lAEFKZOD037153 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO); Wed, 14 Nov 2007 15:20:36 GMT (envelope-from jhary@unsane.co.uk) Message-ID: <473B1248.3000502@unsane.co.uk> Date: Wed, 14 Nov 2007 15:20:40 +0000 From: Vince User-Agent: Thunderbird 2.0.0.6 (X11/20071101) MIME-Version: 1.0 To: Martin McCormick References: <200711141508.lAEF8veZ083725@m.it.okstate.edu> In-Reply-To: <200711141508.lAEF8veZ083725@m.it.okstate.edu> X-Enigmail-Version: 0.95.2 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Cc: freebsd-questions@freebsd.org Subject: Re: /bin/sh Can one Easily Strip Path Name from $0? X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 14 Nov 2007 15:20:52 -0000 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"