PDA

View Full Version : Stupid Shell Scipt problem (regexp related)



tomcheng76
July 29th, 2007, 08:08 AM
hello, i think i should post it here
i am totally newbie to shell scipt (although i have learnt them b4...)
here is the problem


$ cat /usr/local/bin/2jpg
#!/bin/bash
for ext in bmp gif png
do
for file in *.$ext
do
if [ -r $file ]
then
convert "$file" "$file".jpg
fi
done
done

it ends up with Picture.bmp.jpg (it is a jpg) which is able to predict
so, my question is
how to use regexp to remember "Picture" this name ?
i only know using (*) and \1 , but i already forget how to use them
any help would be appreciated, thanks in advance

PaulFr
July 29th, 2007, 09:03 AM
Would this work ?
#!/bin/bash

for ext in bmp gif png
do
for file in *.$ext
do
if [ -r $file ]
then
base=${file/\.[a-z][a-z][a-z]/}
convert "$file" "${base}.jpg"
fi
done
done Comment: If your directory has sample.bmp and sample.png, then you will have only one sample.jpg.

tomcheng76
July 29th, 2007, 09:11 AM
it works like a charm
thanks paulFr
how to do auto renaming in case the directory has sample.bmp and sample.png?
also, i want recursive search (subdirectory)
any help would be appreciated

PaulFr
July 29th, 2007, 05:53 PM
#!/bin/bash
if test $# -ge 1; then
cd $1
echo "Processing $1"
fi
for ext in bmp gif png
do
for file in *.$ext
do
if [ -r $file ]
then
base=${file/\.[a-z][a-z][a-z]/}
new="${base}.jpg"
idx=0; export idx
while :
do
if [ -f "$new" ]
then
idx=`expr $idx + 1`
new="${base}-${idx}.jpg"
else
convert "$file" "$new"
break
fi
done
fi
done
done
exit 0This will handle duplicates in a single directory. The easy way to do subdirectories is IMHO:
1. Put the above in a file, say ~/change2jpg
2. Then go to the top directory where you want to convert the files and run:
find . -type d -exec ~/change2jpg {} \;

tomcheng76
July 30th, 2007, 06:42 AM
thanks PaulFr!
it works perfectly
i just realized that i can use find LOL
btw, here is only one question for the scipt
when there is no test for the while loop
i must add a ":" there?

PaulFr
July 30th, 2007, 07:35 AM
The while : is an infinite loop construct (like while (1) { <do something> } in the C language).