From: Dave on
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,

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.

Dave

--
I respectfully request that this message is not archived by companies as
unscrupulous as 'Experts Exchange' . In case you are unaware,
'Experts Exchange' take questions posted on the web and try to find
idiots stupid enough to pay for the answers, which were posted freely
by others. They are leeches.
From: Kaz Kylheku on
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.

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 "/"

path=trailing/slash/path/
base=${path##*/} # yields empty string, should be "path"

Correctly computing a basename with parameter expansion
seems to require something like this:

case "$path" in
/ )
base=/
;;
*/ )
base=${path%/}
base=${path##*/}
;;
* )
base=${path##*/}
;;
esac

Maybe there is a reason why we have a function for this?

Exercise for readers: rewrite this with parameter expansions:

"$(basename "$(dirname "$(dirname "$(dirname "$FOO")")")")"

Solution given in spoiler below.



Solution to exercise:

#!/bin/sh
echo "just say no"

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.
>
> 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.

> path=trailing/slash/path/
> base=${path##*/} # yields empty string, should be "path"

Ditto.

> Correctly computing a basename with parameter expansion
> seems to require something like this:
>
> case "$path" in
> / )
> base=/
> ;;
> */ )
> base=${path%/}
> base=${path##*/}
> ;;
> * )
> base=${path##*/}
> ;;
> esac
>
> Maybe there is a reason why we have a function for this?

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>.

--
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-04, Chris F.A. Johnson wrote:
> 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.
>>
>> 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.
>
>> path=trailing/slash/path/
>> base=${path##*/} # yields empty string, should be "path"
>
> Ditto.
>
>> Correctly computing a basename with parameter expansion
>> seems to require something like this:
>>
>> case "$path" in
>> / )
>> base=/
>> ;;
>> */ )
>> base=${path%/}
>> base=${path##*/}
>> ;;
>> * )
>> base=${path##*/}
>> ;;
>> esac
>>
>> Maybe there is a reason why we have a function for this?
>
> 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>.

Sorry, that's my local copy. It should be:

<http://cfajohnson.com/shell/scripts/basename-sh>.

--
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: Dave on
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.



--
I respectfully request that this message is not archived by companies as
unscrupulous as 'Experts Exchange' . In case you are unaware,
'Experts Exchange' take questions posted on the web and try to find
idiots stupid enough to pay for the answers, which were posted freely
by others. They are leeches.