Web Hosting Talk







View Full Version : Simple Shell Scripting


WoodShedd
07-04-2002, 12:36 AM
I have a need to learn how to write shell scripts (among others)
on my own. I'm getting there.

At the moment I have the need for a renaming one, but I cant seem to get it right. If anyone could help me, that would be great.

What I need to do is rename file001.foo.bar to file001.bar, and do this for all the files in the directory.

can I do this with basename?

I suppose It could be done in perl too.

help = good. :)

thanks

ScottD
07-04-2002, 12:58 AM
Assuming the pattern never changes:basename file | sed 's/.foo//'Should do the trick with the example provided.

WoodShedd
07-04-2002, 12:59 AM
which pattern?

ScottD
07-04-2002, 01:05 AM
If the file is always called "*.foo.*"

Is that variable? From the example you provide it seems you want to remove "foo", the little snipped above does just that.

WoodShedd
07-04-2002, 01:08 AM
Yes, that's the case.

basename file | sed 's/.foo//'

what do I put in for file? anything?

I'm aiming to remove .foo on all files in the directory.

ScottD
07-04-2002, 01:08 AM
If the file is always called "*.foo.*"

Is that variable? From the example you provide it seems you want to remove "foo", the little snipped above does just that.

<<edit>>
Oops, the above should have read:basename $file | sed 's/.foo//'Where $file contains the actual file name, so something like:ls -1 | while read file; do
mv $file `basename $file | sed 's/.foo//'`
donewould be the whole thing. Basename is actually unnecessary if you are executing ls -1 on the current directory.

BTW, make sure you run this in a test environment first. Moving files is dangerous work, a simple type and poof.

WoodShedd
07-04-2002, 01:14 AM
Thanks.

I ran it in a test env with no result. I wonder what I could have done wrong...

WoodShedd
07-04-2002, 01:19 AM
THanks, Scott. I got it to work. (silly error on my part)

ScottD
07-04-2002, 01:19 AM
Good question, here are my results:[sc@goofy]$ ls -1
a.foo.bar
b.foo.bar
c.foo.bar
d.foo.bar
e.foo.bar
[sc@goofy]$ ls -1 | while read file; do
> mv $file `echo $file | sed 's/.foo//'`
> done
[sc@goofy]$ ls -1
a.bar
b.bar
c.bar
d.bar
e.bar
[sc@goofy$ Note, I didn't use basename but the results should have been the same. Are you sure you backticked the part surrounding the basename call?

<< edit >> :D
Great! :)

WoodShedd
07-04-2002, 01:41 AM
One last question:

one last question:

I did up a little script for running the binaries that I use to scale images.


for f in `ls`
do
echo "Converting $f from jpg to pnm...."
jpegtopnm *.jpg > $f.pnm
echo "Scaling $f.pnm"
pnmscale -xysize 100 100 $f.pnm > $f_thumb.pnm
echo "Converting $f from pnm to jpg"
pnmtojpeg --quality 75 $f_thumb.pnm > $f_thumb.jpg
echo "Done $f"
done

This is where my original problem of having *.foo.bar files
(in this case *.jpg.pnm) stems from.

instead of *.jpg > $f.pnm resulting in the original file becoming a pnm file the original file becomes *.jpg.pnm

how might I either
a. fix this or
b. incorporate ls -1 | while read file; do
mv $file `basename $file | sed 's/.foo//'`
done in my original mess so I dont have to run two different scripts.


thanks again. I'm learning!