I'm processing a csv file in awk. During the script, I need to exit awk for a little while, go and do some ImageMagick processing in the bash shell based on the information awk processed in the csv file. I'm trying to do this image processing in the shell because I loop through all the jpg files in the nominated directory. My question is twofold:
- How can I pass variables from awk to bash so that I can do this? (this is
$imageDirectoryand$productRefin the code below) - I'm trying to avoid lots of
system(someImageMagicCommand)type of code, because it looks like I'm trying to use awk to do something it's not designed to do. Is there a better approach?
Here's the pseudocode sample that illustrates what I'm trying to achieve:
#blah blah blah awk code
#leave awk interpreter, go into bash
'
resizesize="200x200";
#concatenate with *.jpg suffix for listing all the jpg files in the imageDirectory
imageDirectoryWithSuffix="$imageDirectory/*.jpg";
for i in `ls $imageDirectoryWithSuffix`
do
#imageMagick converts large images to thumbnails
convert $i $resizeSize -otherFlagsEtc assets/$productRef/thumbs/$i
done
'#back into awk, more csv processing now...
Clarifying context: I have a csv file full of product information and associated file paths (1 product per line). I'm trying to automate the creation of webpages about the products. Part of this involves resizing a directory of images, the location of which is given as a field in the csv file. Hence, part way through the script, I'm trying to leave the awk interpreter, invoke ImageMagick to resize, and then return to the awk interpreter at the same record in the csv file and keep outputting HTML files.
There's about 100 (long) lines of script code before I get to the ImageMagick part and another hundred afterwards, so based on @Bushmills answer I think the best thing would be to write the awk variables I'll need in bash to a small temp file, then exit awk and read in the temp file from bash. However, how do I reinvoke awk and get it to start reading at the same record where it left off? Or do I just have to stay in awk and use a system() call? It doesn't seem sensible to wrap an entire bash for loop in an awk system() function, but I can't think of a more elegant way of calling ImageMagick on an entire directory of files.