Unix Shell - Using basename to strip suffix

This is Interesting: Free IT Magazines  
Home > Archive > Unix Shell > May 2007 > Using basename to strip suffix





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author Using basename to strip suffix
Dave Kelly

2007-05-28, 1:19 am

I am trying to strip the suffix 'jpg' from a list of images. I keep
getting this error.

../writeimage: line 23: and this is what you catch.jpg: command not found
../writeimage: line 23: dave-damon-2006-3.jpg: command not found
../writeimage: line 23: Double exposure.jpg: command not found

Here is a snippet containing the error producing code.

for filename in *.jpg; ##list images in directory

do
if [ -f "$filename" ]
then

CAPTION=basename "$filename" .jpg

imageline="
<td align=\"center\"> <p style=\"margin-top: 0; margin-bottom: 0\"> <img
border=\"0\" src=\"$filename\"></a></p> <p style=\"margin-top: 0;
margin-bottom: 0\">"$CAPTION"</p> </td>"

This is a maintance tool for me. I run it on my debian based machine.

TIA
Dave

--
A little rum in the morning coffee. Just to clear the cobwebs, ya know.
Janis Papanagnou

2007-05-28, 1:19 am

Dave Kelly wrote:
> I am trying to strip the suffix 'jpg' from a list of images. I keep
> getting this error.
>
> ./writeimage: line 23: and this is what you catch.jpg: command not found
> ./writeimage: line 23: dave-damon-2006-3.jpg: command not found
> ./writeimage: line 23: Double exposure.jpg: command not found
>
> Here is a snippet containing the error producing code.
>
> for filename in *.jpg; ##list images in directory
>
> do
> if [ -f "$filename" ]
> then
>
> CAPTION=basename "$filename" .jpg


CAPTION=$( basename "$filename" .jpg )

or using shell builtins instead of costly external processes...

CAPTION=${filename%.jpg}


Janis

>
> imageline="
> <td align=\"center\"> <p style=\"margin-top: 0; margin-bottom: 0\"> <img
> border=\"0\" src=\"$filename\"></a></p> <p style=\"margin-top: 0;
> margin-bottom: 0\">"$CAPTION"</p> </td>"
>
> This is a maintance tool for me. I run it on my debian based machine.
>
> TIA
> Dave
>

Dave Kelly

2007-05-28, 1:19 am

Janis Papanagnou wrote:

> CAPTION=$( basename "$filename" .jpg )
>
> or using shell builtins instead of costly external processes...
>
> CAPTION=${filename%.jpg}
>
>
> Janis


Worked like a charm. Thanks.
Where would I go to read/learn about some of this stuff. A google search
did not turn up any interesting leads.
Is there a better place than 'info coreutilities'? Does this cover builtins?

Thanks again.
Dave


--
A little rum in the morning coffee. Just to clear the cobwebs, ya know.
Janis Papanagnou

2007-05-28, 7:19 am

Dave Kelly wrote:
> Janis Papanagnou wrote:
>
>
>
> Worked like a charm. Thanks.
> Where would I go to read/learn about some of this stuff. A google search
> did not turn up any interesting leads.
> Is there a better place than 'info coreutilities'? Does this cover
> builtins?


The builtins depend on the used shell, for Kornshell for example you can
find the information in the manpage online
http://www.cs.princeton.edu/~jlk/ko.../doc/man93.html
or for bash with man bash. Though careful about portability; even most
modern shells have a large common subset you may occasionally want
to restrict to the POSIX subset. (The above ${...%...} is POSIX.) Surely
the man pages are a good source to get an overview of the shell features.

Janis

>
> Thanks again.
> Dave
>
>

Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2009 webservertalk.com