Looping filename in a shell script

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
FlyingJenny2bucks
Posts: 22
Joined: 2015-03-01T19:40:59-07:00
Authentication code: 6789

Looping filename in a shell script

Post by FlyingJenny2bucks »

Hi, I'm trying to loop all jpg files (i.e. *.jpg) within a directory in a shell script. Below is an example of what I'm attempting to do.

Convert $1 $2 $3 $4 $5 -adjoin abc.pdf

$1=*apple.jpg
$2=*orange.jpg
$3=*banana.jpg
$4=*mango.jpg
$5=*cheese.jpg

How would you go about doing this in a shell script so that it will always search for the selected variables within a directory?

Many thanks for all you geniuses in advance :D
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Looping filename in a shell script

Post by fmw42 »

Not really sure what you are trying to do? Do you want all files in the directory (*.jpg) or only certain ones such a *apple.jpg, etc? Are you trying to loop over a list of directories and create one pdf from all jpgs in that directory?

Please clarify further and identify your version of IM and platform.

untested:

dirlist="folder1 folder2 folder3 folder4"
for dir in $dirlist; do
dirname=`basename $dir`
cd $dir
convert *.jpg -adjoin ${dirname}_abc.pdf
cd
done

However, for output to pdf, your should be able to leave out the -adjoin, but it does no harm to include it.

NOTE: the above will not traverse subdirectories. So you need to put the full path to all folders unless they are all at the same level.
FlyingJenny2bucks
Posts: 22
Joined: 2015-03-01T19:40:59-07:00
Authentication code: 6789

Re: Looping filename in a shell script

Post by FlyingJenny2bucks »

-ImageMagick-6.9.0-7-Q16-x64-dll
-bash script in cygwin

Hi, I want to loop all *.jpg in a directory. $1, $2, $3, $4, $5 will filter the images with specific filenames and then convert will merge them together into pdf. Each pdf should contain one image of apple, orange, etc.

Thanks !
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Looping filename in a shell script

Post by fmw42 »

Sorry I do not fully understand what you are trying to do with apple, orange, etc. First you say you want to loop all *.jpg, but then you say you just want apples, oranges, etc? Please clarify further.
FlyingJenny2bucks
Posts: 22
Joined: 2015-03-01T19:40:59-07:00
Authentication code: 6789

Re: Looping filename in a shell script

Post by FlyingJenny2bucks »

What I meant was I have a folder with many .jpg images inside. I want to merge only those images with the selected filenames per $1 $2 $3 $4 $5 into a pdf. So if there are 3 apple, orange, banana, mango and cheese images (i.e. 010apple.jpg, 110apple.jpg, 001apple.jpg, 010orange.jpg, 110orange.jpg, 001orange.jpg, etc.), there should be 3 pdfs, each containing a total of 5 images with apple, orange, banana, mango and cheese each.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Looping filename in a shell script

Post by fmw42 »

Still confused. You want all images that start with 110*.jpg, such as 110apple.jpg 110orange.jpg, etc merged into one pdf. The same for the other starting numbers. Or do you want all *apple.jpg merged into one pdf, all *orange.jpg merged into another pdf, etc? What is the common set of characters in the filenames that you want merged into a given pdf? Give me an example command line of what images are to be merged into the output pdf.
FlyingJenny2bucks
Posts: 22
Joined: 2015-03-01T19:40:59-07:00
Authentication code: 6789

Re: Looping filename in a shell script

Post by FlyingJenny2bucks »

Here is an example:

Convert 00010apple.jpg 112orange.jpg 1610banana.jpg 1540mango.jpg 20189cheese.jpg -adjoin 001.pdf
Convert 10221apple.jpg 1320orange.jpg 41110banana.jpg 1040mango.jpg 5189cheese.jpg -adjoin 002.pdf

As you can see, the first half of the filenames varies both in length and prefixes. What I want is to generate a shell script that loops through thousands of jpg files in a single directory, filter against the arguments ($1 $2 $3 $4 $5) and then merge them into pdfs.

Thus, if the first image that $1 finds is 00010apple.jpg, it will merge it with the first image that $2 finds '112orange.jpg' and so on. There should be 5 images in each pdf and luckily the order of images does not matter as long as there is one apple, orange, banana, mango and cheese in each pdf.

I hope this helps. Thanks !
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Looping filename in a shell script

Post by fmw42 »

I think you will need to make arrays for all $1, then all $2, etc. Then loop over the arrays taking the first array element [0] from each array as input to a separate convert. If you cannot figure this out, I will try to work on it this afternoon.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Looping filename in a shell script

Post by fmw42 »

If I understand what you want, it would be something like (untested):

Code: Select all

cd somedirectory
appleArr=(`ls *apple.jpg`)
orangeArr=(`ls *orange.jpg`)
bananaArr=(`ls *banana.jpg`)
mangoArr=(`ls *mango.jpg`)
cheeseArr=(`ls *cheese.jpg`)
applenum=${#appleArr[*]}
orangenum=${#orangeArr[*]}
banananum=${#bananaArr[*]}
mangonum=${#mangoArr[*]}
cheesenum=${#cheeseArr[*]}
minnum=`convert xc: -format "%[fx:min(min(min(min($applenum,$orangenum),$banananum),$mangonum),$cheesenum)]" info:`
for ((i=0; i<minnum; i++)) do
index=`printf "%03d" "$i"`
convert ${appleArr[$i]} ${orangeArr[$i]} ${bananaArr[$i]} ${mangoArr[$i]} ${cheeseArr[$i]} -adjoin ${index}.pdf
done
cd
FlyingJenny2bucks
Posts: 22
Joined: 2015-03-01T19:40:59-07:00
Authentication code: 6789

Re: Looping filename in a shell script

Post by FlyingJenny2bucks »

Oh my....below are my guesses:

appleArr=(`ls *apple.jpg`) - read the list of apple files and store them in 'appleArr'

applenum=${#appleArr[*]} - numerically arrange the files in 'appleArr'

minnum=`convert xc: -format "%[fx:min(min(min(min($applenum,$orangenum),$banananum),$mangonum),$cheesenum)]" info:`
- my brain is boiling

for ((i=0; i<minnum; i++)) do
- a loop with defined parameters but I don't understand the parameters.

index=`printf "%03d" "$i"`
- again function unknown to me

convert ${appleArr[$i]} ${orangeArr[$i]} ${bananaArr[$i]} ${mangoArr[$i]} ${cheeseArr[$i]} -adjoin ${index}.pdf
- hmmm... join the images with the lowest numerical prefixes into a pdf (but what does {index} do) ??

Thank you very much for the help.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Looping filename in a shell script

Post by fmw42 »

Code: Select all

cd somedirectory    change to the desired directory

appleArr=(`ls *apple.jpg`)  gets all file names that match *apple.jpg

applenum is the number of found file names for the appleArr

minnum is the smallest number of found files from each of the arrays (the arrays may not all have the same number of files) and if an index is larger than the array, you will get an error. So I limit the outputs so that all arrays will not exceed the minimum number of files from the smallest array length.

for ((i=0; i<minnum; i++)) do  loop over every array parameter i from 0 to minnum

index=`printf "%03d" "$i"` change the loop parameter $i from a simple number to one with leading zeros

convert ${appleArr[$i]} ${orangeArr[$i]} ${bananaArr[$i]} ${mangoArr[$i]} ${cheeseArr[$i]} -adjoin ${index}.pdf  create each pdf output for a given array parameter $i from each of your 5 arrays

done  end the looping

cd   change back to the home directory
You need to do some reading on bash for loops and arrays and printf, which you can find by a google search
FlyingJenny2bucks
Posts: 22
Joined: 2015-03-01T19:40:59-07:00
Authentication code: 6789

Re: Looping filename in a shell script

Post by FlyingJenny2bucks »

Much appreciated.
Post Reply