From: Chris F.A. Johnson on
On 2009-12-04, Dave wrote:
> Chris F.A. Johnson wrote:
>
>> Basename is not a function; it is an external command.
>>
>> There is a POSIX-compliant basename function at
>> <http://cfaj/cfajohnson.com/shell/scripts/basename-sh>.
>>
>
> Are there any systems which do not have basename? I tried a few, including HP-UX
> 11.11, and all had it.

All POSIX systems should have it.

--
Chris F.A. Johnson, author <http://shell.cfajohnson.com/>
===================================================================
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
Pro Bash Programming: Scripting the GNU/Linux Shell (2009, Apress)
===== My code in this post, if any, assumes the POSIX locale =====
===== and is released under the GNU General Public Licence =====
From: Chris F.A. Johnson on
On 2009-12-03, Kaz Kylheku wrote:
> On 2009-12-03, Chris F.A. Johnson <cfajohnson(a)gmail.com> wrote:
>> On 2009-12-03, Rakesh Sharma wrote:
>>> On Dec 3, 7:16?am, Dave <f...(a)coo.com> wrote:
>>>> The output of a command is this
>>>>
>>>> /opt/kirkby/gcc-4.4.2/lib/libgcc_s.so.1
>>>>
>>>> how can I strip off the path, and so just get the 'libgcc_s.so.1' ?
>>>>
>>>> I guess I need to strip from the first character, to the last '/', but are not
>>>> sure how to do this.
>>>>
>>>
>>> Apart from the command 'basename' which is tailor-made for this task,
>>
>> As is POSIX parameter expansion.
>
> Not sure why you ned to invoke POSIX here; basename is a also a POSIX feature,
> and not a recent addition either.

An external command such as basename is many, many times slower
than the shell's parameter expansion.

--
Chris F.A. Johnson, author <http://shell.cfajohnson.com/>
===================================================================
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
Pro Bash Programming: Scripting the GNU/Linux Shell (2009, Apress)
===== My code in this post, if any, assumes the POSIX locale =====
===== and is released under the GNU General Public Licence =====
From: Kaz Kylheku on
On 2009-12-04, Chris F.A. Johnson <cfajohnson(a)gmail.com> wrote:
>> If parameter ``tailor-made'' for this problem, why do we
>> run into this problem when we apply parameter expansion
>> in the straighforward way, and how do we fix it?
>>
>> path=/
>> base=${path##*/} # yields empty string, should be "/"
>
> No, it should be an empty string as there is no name after the slash.

One tiny problem with that reasoning is that if you
parse "$path" into "$dirname" and "$basename" with this
method, and then:

chdir "$dirname"

and then try to access the empty string "$basename", there is no
directory entry by that in that directory! Oops!

Paths are not (exactly) strings. Blind string manipulation is not
path manipulations.

Paths are syntax which denote a structured name object. The argument can be
made that it needs just a little bit of care in parsing and generation.
From: Jon LaBadie on
Dave wrote:
> Rakesh Sharma wrote:
>> On Dec 3, 7:16 am, Dave <f...(a)coo.com> wrote:
>>> The output of a command is this
>>>
>>> /opt/kirkby/gcc-4.4.2/lib/libgcc_s.so.1
>>>
>>> how can I strip off the path, and so just get the 'libgcc_s.so.1' ?
>>>
>>
>> Apart from the command 'basename' which is tailor-made for this task,
>
> Thank you. I'll use that. It is defined by POSIX, works on HP-UX 11.11
> and a Google shows it exists on AIX 3.1 (which is pretty damm old), so
> it would appear to be quite portable. In the relatively unlikely event
> that IRIX or Tru64 is supported on the system I'm looking at, it may be
> necessary to revisit this, but for now at least, that seems sufficiently
> portable.
>
> Thank you. That is a new unix command I have learned.
>

basename(1) existed over 30 years ago in UNIX V7.
From: Sven Mascheck on
Dave wrote:

> Are there any systems which do not have basename? I tried a few,
> including HP-UX 11.11, and all had it.

As Jon mentioned, 7th ed unix knew basename(1), and also had
the suffix feature implemented already.
V7 is virtually _the_ ancestor concerning traditional utilities.
Thus you really should find it in every toolkit (and if not, you
will almost certainly have a whole bunch of different problems).