[ale] stripping filename extension in bash

David S. Jackson deepbsd at earthlink.net
Sat Nov 30 18:16:40 EST 2002


On Sat, Nov 30, 2002 at 03:45:08PM -0500 Christopher Bergeron <christopher at bergeron.com> wrote:
> Does anyone know how I can strip the extension from a filename in a bash 
> script?
> 
> Here's my script:
> 
> #!/bin/bash
> echo USE bergeron
> for i in `\ls *.jpg`
> do
> echo "INSERT INTO pictures VALUES ('','$i','','','');"

Or you could just use ${i%.*} instead of $i.  (See man bash and
look for "${parameter%word}")

Or, if you know the suffix will be .jpg and never, say, .JPG, you
could use a command substitution like `basename $i .jpg` or
$(basename $i .jpg) or somesuch.

Another thing to watch out for is that files you import from
Windoze users (as on Gnutella or filesharing networks) sometimes
have spaces or reserved characters in the filenames, and a for
loop will interpret those spaces as a separator.  So, you might
try a while/read loop, unless you're certain all the filenames
are polite and respectable.  Something like 

ls *.jpg|while read file; do
echo "INSERT INTO pictures VALUES ('','${file%.*}','','','');"
blah blah
done

should work.  I would sanitize the filenames first, if they need
it, and a while/read loop along with sed works pretty well for
that as well.  So, you're making an sql database of your jpegs?
Cool.

-- 
David S. Jackson                        dsj at dsj.net
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
	"Are you sure you're not an encyclopedia
	salesman?"  No, Ma'am.	Just a burglar,
	come to ransack the flat." -- Monty Python
_______________________________________________
Ale mailing list
Ale at ale.org
http://www.ale.org/mailman/listinfo/ale






More information about the Ale mailing list