How do I run a series of commands in terminal on a mac?
July 5, 2015 11:27 AM   Subscribe

I have a command in terminal (included below) that contains the same number in two different locations. I need to run that command 500 times counting down from 500 to 0. Is there an easy way to automate/batch process this?

I'm using ImageMagick to add text to an image template. I'm doing this with six lines of code in the mac terminal. The only change is the number that is being used twice for the text and the name of the file (bolded below). Is there an easy way to automate the process of running this 500 times?

This is the code:

convert -size 1024x512 xc:none -gravity NorthWest \
-stroke white -strokewidth 0 -annotate 0 ‘’ \
-background none +repage \
-stroke '#000C' -strokewidth 1 -pointsize 200 -annotate 0 496 \
blanktemplate.png +swap -gravity NorthWest -geometry +90+20 \
-composite 496.jpg
posted by andoatnp to Computers & Internet (5 answers total) 2 users marked this as a favorite
 
I think there is indeed!
for filenum in $(seq 1 500)
do

convert -size 1024x512 xc:none -gravity NorthWest \
-stroke white -strokewidth 0 -annotate 0 ‘’ \
-background none +repage \
-stroke '#000C' -strokewidth 1 -pointsize 200 -annotate 0 $filenum \
blanktemplate.png +swap -gravity NorthWest -geometry +90+20 \
-composite $filenum.jpg

done
You can put that in a generateimages.bash and then run it with bash generateimages.bash. If you want to test what it will do first put an echo in front of the convert command and it will just print out what it will run.
posted by foxfirefey at 11:41 AM on July 5, 2015 [4 favorites]


That looks like just one command split across lines, not six separate commands. It's also not totally clear to me why you'd need to count down from 500 instead of up from 0. But anyway 'seq' will let you do this pretty easily. Something like this, maybe. Note I just copied and pasted your original command, I didn't really pay attention to whether that part is all correct and makes sense. And it looks like some of the quotation marks got 'curlified' so be sure those are all right before running with it.

for imgnum in `seq 500 0`
do
convert -size 1024x512 xc:none -gravity NorthWest \
-stroke white -strokewidth 0 -annotate 0 ‘’ \
-background none +repage \
-stroke '#000C' -strokewidth 1 -pointsize 200 -annotate 0 $imgnum \
blanktemplate.png +swap -gravity NorthWest -geometry +90+20 \
-composite ${imgnum}.jpg
done

This is assuming your image 0 is "0.jpg" not "000.jpg" (if you need "000" you can add '-w' to the seq command).
posted by primethyme at 11:42 AM on July 5, 2015


Assuming you're running bash shell, the following may work:

for mynumber in {500..1}
do
yourcommand
done

Substituting $mynumber for both hard coded values in yourcommand.
posted by SquidLips at 11:44 AM on July 5, 2015


One weird quirk you should know about with Bash on OSX is that Apple ships a very old release that doesn't properly support padding in brace expansion: where on a current release

{01..10} expands as 01 02 03 ... 10

OSX's Bash gives you 1 2 3 ... 10

This is why most of the solutions above use seq instead. Since brace expansion is easier to write, you may want to consider using Homebrew or Macports to install a newer version of Bash if you're going to be writing a lot of these scripts in the future.
posted by fifthrider at 11:56 AM on July 5, 2015 [5 favorites]


I tried the first solution from foxfirefey and it worked. Thanks!
posted by andoatnp at 12:17 PM on July 5, 2015


« Older I ate cheesecake and now I'm sick.   |   How to buy the empty lot next door to extend my... Newer »
This thread is closed to new comments.