Forums » Off-Topic

Help me make a bash script!

12»
Oct 10, 2004 Pyro link
Anywho... I'm trying to make a script that'll crop a massive amount of bitmaps, stored in different directories, some of which have subdirectories (they're all in the same main directories, though). For example:

/2/:
1.bmp
2.bmp
3.bmp
3/

/2/3/:
1.bmp
2.bmp
3.bmp

/3/:
...
...

You get the idea. Anyways, I'm using ImageMagick to crop them, so the syntax is:
convert <inputfile> <outputfile>

Is there a way to make the script run that command on every picture in every directory? The command I've been using is:
for img in `ls *.bmp`
do
convert $img crop-$img
done

(yay for IBM developerWorks)
However, that doesn't seem to work recursively. Help? :P

Edit: Ooops, forgot to mention... I'm using Cygwin...
Oct 10, 2004 mr_spuck link
if you have find:

for img in `find -name '*.bmp'`
do
convert $img crop-$img
done

But avoid spaces in the paths atleast on my system they didn't get passed over correctly.
Oct 10, 2004 Pyro link
Tyrdium@hal /cygdrive/c/documents and settings/tyrdium/desktop/Images
$ for img in `find -name '*.bmp'`; do convert -crop 120x60+20+20 $img crop-$img; done
convert: unable to open image `crop-./1/clp1.bmp': No such file or directory.
convert: unable to open image `crop-./1/clp2.bmp': No such file or directory.
convert: unable to open image `crop-./1/clp3.bmp': No such file or directory.
convert: unable to open image `crop-./1/clp4.bmp': No such file or directory.
convert: unable to open image `crop-./1/clp5.bmp': No such file or directory.
convert: unable to open image `crop-./1/clp6.bmp': No such file or directory.
convert: unable to open image `crop-./1/clp7.bmp': No such file or directory.
convert: unable to open image `crop-./1/clp8.bmp': No such file or directory.
convert: unable to open image `crop-./1/clp9.bmp': No such file or directory.
convert: unable to open image `crop-./2/clp10.bmp': No such file or directory.
convert: unable to open image `crop-./2/clp11.bmp': No such file or directory.
convert: unable to open image `crop-./2/clp12.bmp': No such file or directory.
convert: unable to open image `crop-./2/clp13.bmp': No such file or directory.
convert: unable to open image `crop-./2/clp14.bmp': No such file or directory.
convert: unable to open image `crop-./2/clp15.bmp': No such file or directory.

Blah... >.<
Oct 10, 2004 mr_spuck link
ouch forgot about the renaming. :P
Hmm I'm not that much of a shell expert .. this might help:
http://studio.imagemagick.org/pipermail/magick-users/2002-July/003883.html
Although it doesn't do any renaming either. Can't you just copy directory tree somewhere else before cropping?
Oct 10, 2004 Pyro link
I suppose that could work. I'll probably be working with a few dozen gigs worth, but I could split up the jobs or something. :P

Edit: Oooo, I just realized I have an 80 gig Firewire drive I can use for backing it up... W00t!
Oct 10, 2004 red cactus link
How about Perl?

#!/usr/bin/perl

@images = `find -name *.bmp`;
foreach $image (@images) {
 $newname = ($image =~ /.+\.bmp$/i);
 print "Converting $image...\n";
 `convert $image $newname`;
}

Does this work? I'm not on a Linux box to test, and I don't have a directory structure like that to test, either.
Oct 10, 2004 roguelazer link
I found your error... Doing a crop-$img would give you "crop-images/01.bmp". Not ideal. Solution forthcoming... I will go make a gawk script now I think. Cut is too unwieldy.

AND... gawk is too unwieldy too. Maybe using sed on a file containing a list of the directories? This would be -so- much easier if it didn't have to recurse through multiple levels...
Oct 10, 2004 mr_spuck link
hah found a way .. this should work:

for img in `find -name '*.jpg'`
do
echo $img | convert -crop 120x60+20+20 $img `sed s/.jpg/-cropped.jpg/`
done

Though it still doesn't handle spaces. And the resulting filenames will be input-cropped.jpg not crop-input.jpg.
I can't figure out how to make sed eat directory seperators.

EDIT: extreme syncrone posting :P
Oct 10, 2004 roguelazer link
Perfectorized. It handles files between 0.bmp and 9999.bmp

#!/bin/bash

for i in `find -name *.bmp`
do
DIRECTORY=`echo $i | sed "s/^\.\\///" | sed "s/[0-9].bmp$//"`
FILENAME=`echo $i | grep -o "[0-9]\{0,3\}[0-9].bmp$"`
convert -crop 120x60+20+20 ${DIRECTORY}${FILENAME} ${DIRECTORY}crop-${FILENAME}
done
Oct 10, 2004 Pyro link
Wheee, lotsa scripts. :P

Anywho, new one! Now that I've got them all cut down, I want to rename them. The format is currently clp1.bmp, clp2.bmp, etc. Now, what I want to do is change that to img001.bmp, img002.bmp, etc. Next script! :P
Oct 10, 2004 MonkRX link
*Automated Voice Respose*

We are sorry, P-Y-R-O, but your free S-C-R-I-P-T-S trial has ended. Please press three to pay me 200 dollars and continue to recive help from this MMORPG Forum.
Oct 11, 2004 Pyro link
*enters Gates' credit card number*
Oct 11, 2004 Pyro link
New one! :P

I've got the stuff sorted into directories according to size. For example:

/a/b/1
/a/b/2
/a/b/3
/a/c/1
/a/c/2
/d/1
/d/2
/d/3

(The letters are just directory names). Now, how do I get it to run a different command for all the files in the directory, depending on what directory they're in? For example, run mogrify -format png on all the files in the 1 directories, mogrify -format gif on all the files in the 2 directories, etc.
Oct 11, 2004 roguelazer link
#!/bin/bash

CURRENTDIR=`pwd`

for i in `find [a-z] -type d -maxdepth 1`
do
DIRECTORY=`echo $i; sed "s/\.\\///"`
cd ${DIRECTORY}
if [ -d a ]
then
for n in `find [a-z] -type d -maxdepth 1`
do
2DIR=`echo $n; sed "s/\.\\///"`
cd ${2DIR}
for o in `find [0-9] -type d -maxdepth 1`
do
if [ $o == 1 ]
mogrify -format png *
fi
if [ $o == 2 ]
mogrify -format gif *
fi
done
cd ${DIRECTORY}
done
else
then
for n in `find [0-9] -type d -maxdepth 1`
do
if [ $n == 1 ]
mogrify -format png *
fi
if [ $n == 2 ]
mogrify -format gif *
fi
done
fi

cd ${CURRENTDIR}
done

Oct 11, 2004 Pyro link
Blah, here's what I get when I try to run the script... :-/

Tyrdium@hal /cygdrive/c/documents and settings/tyrdium/desktop/images
$ sortedcrop.sh
bash: sortedcrop.sh: command not found

Tyrdium@hal /cygdrive/c/documents and settings/tyrdium/desktop/images
$ sh sortedcrop.sh
sortedcrop.sh: 14: Syntax error: Bad substitution

-----------------------------------------------------------------

#!/bin/bash

CURRENTDIR=`pwd`

for i in `find [a-z] -type d -maxdepth 1`
do
DIRECTORY=`echo $i; sed "s/\.\\///"`
cd ${DIRECTORY}
if [ -d a ]
then
for n in `find [a-z] -type d -maxdepth 1`
do
2DIR=`echo $n; sed "s/\.\\///"`
cd ${2DIR}
for o in `find [0-9] -type d -maxdepth 1`
do
if [ $o == 3 ]
mogrify -crop 366x56+0+0 *
fi
if [ $o == 4 ]
mogrify -crop 366x72+0+0 *
fi
if [ $o == 5 ]
mogrify -crop 366x88+0+0 *
fi
if [ $o == 6 ]
mogrify -crop 366x104+0+0 *
fi
if [ $o == 7 ]
mogrify -crop 366x120+0+0 *
fi
done
cd ${DIRECTORY}
done
else
then
for n in `find [0-9] -type d -maxdepth 1`
do
if [ $o == 3 ]
mogrify -crop 366x56+0+0 *
fi
if [ $o == 4 ]
mogrify -crop 366x72+0+0 *
fi
if [ $o == 5 ]
mogrify -crop 366x88+0+0 *
fi
if [ $o == 6 ]
mogrify -crop 366x104+0+0 *
fi
if [ $o == 7 ]
mogrify -crop 366x120+0+0 *
fi
done
fi

cd ${CURRENTDIR}
done
Oct 11, 2004 roguelazer link
Try chmod +x the file and just running it. IE:

$ chmod +x sortedcrop.sh
$ ./sortedcrop.sh

usually, sh behaves just like bash. But I dunno about in cygwin.
Oct 11, 2004 Pyro link
Tyrdium@hal /cygdrive/c/documents and settings/tyrdium/desktop/images
$ ./sortedcrop.sh
./sortedcrop.sh: line 19: syntax error near unexpected token `fi'
./sortedcrop.sh: line 19: `fi'
Oct 11, 2004 roguelazer link
You forgot that if has to be followed by "then".

---

#!/bin/bash

CURRENTDIR=`pwd`

for i in `find [a-z] -type d -maxdepth 1`
do
DIRECTORY=`echo $i; sed "s/\.\\///"`
cd ${DIRECTORY}
if [ -d a ]
then
for n in `find [a-z] -type d -maxdepth 1`
do
2DIR=`echo $n; sed "s/\.\\///"`
cd ${2DIR}
for o in `find [0-9] -type d -maxdepth 1`
do
if [ $o == 3 ]
then mogrify -crop 366x56+0+0 *
fi
if [ $o == 4 ]
then mogrify -crop 366x72+0+0 *
fi
if [ $o == 5 ]
then mogrify -crop 366x88+0+0 *
fi
if [ $o == 6 ]
then mogrify -crop 366x104+0+0 *
fi
if [ $o == 7 ]
then mogrify -crop 366x120+0+0 *
fi
done
cd ${DIRECTORY}
done
else
then
for n in `find [0-9] -type d -maxdepth 1`
do
if [ $o == 3 ]
then mogrify -crop 366x56+0+0 *
fi
if [ $o == 4 ]
then mogrify -crop 366x72+0+0 *
fi
if [ $o == 5 ]
then mogrify -crop 366x88+0+0 *
fi
if [ $o == 6 ]
then mogrify -crop 366x104+0+0 *
fi
if [ $o == 7 ]
then mogrify -crop 366x120+0+0 *
fi
done
fi

cd ${CURRENTDIR}
done
Oct 11, 2004 Pyro link
Tyrdium@hal /cygdrive/c/documents and settings/tyrdium/desktop/weapons2
$ ./sortedcrop.sh
./sortedcrop.sh: line 36: syntax error near unexpected token `then'
./sortedcrop.sh: line 36: `then'
Oct 11, 2004 roguelazer link