tags:

views:

60

answers:

4

Hi I am a newbe to bash programming and need some help. I am building a pipeline for image processing. I would like to be able to take the png images in a folder and pass them to clusterImage.pl once that is done I would like to then pass the outputted file to seperateObjects.pl the outputted file is of the same name but has kmeansOutput.all.matrix attached to the end. Below is what I have so far, but it is not working. Any help would be greatly appreciated. Thank you

#!/bin/bash
#This script will take in an image and a matrix file.
#The output will be an image and a matrix file.

list=`ls *.png`
for i in $list
do
$file="./$list"
$image_array = $list
echo $file
#Cheching to see if the file exists.
for((j=0;j<=i;j++))
do
if [ -e image_array[j] ]; then
echo $file
echo "Begining processing"
#Take in an image and create a matrix from it.
perl clusterImage.pl SampleImage.png
#Take in a matrix and draw a picture showing the centers of all
#of the colonies.
perl seperateObjects.pl SampleImage.png.kmeansOutput.all.matrix
echo "Ending processing"
else
echo "There is an issue"
fi
done
done
+5  A: 

this should work:

for file in *.png; do
    # do stuff with your file:
    perl clusterImage.pl "$file";
    # …
done
knittl
Thank you knittl
Alos
+2  A: 

I see a few problems (or potential improvements) with your code:

  1. You don't need the loop for i in $list because you never use $i in the script - that results in just doing the same thing over and over again (the same number of times as the number of .png files in the directory)
  2. You don't need to use a Bash array, since Bash can iterate over the different filenames in a list like *.png.
  3. I suspect you mean to run perl clusterImage.pl on each .png file in the directory... or did you? It's kind of hard to tell. Edit your question to explain more clearly what you mean to do, and I can edit my answer accordingly.
  4. You can use short-circuiting, as they call it, instead of an if statement: [ -f file.png ] && echo "file exists" is shorter than

    if [ -f file.png ]; then
        echo "file exists"
    fi
    

If I understand what you're trying to do (and I'm not sure I do), I think this might work for you. For each image in the directory, this will run perl clusterImage.pl <name_of_image.png> and perl separateObjects.pl <name_of_image.png>.kmeansOutput.all.matrix.

for image in *.png
do
  [[ -f $image ]] && perl clusterImage.pl $image && perl separateObjects.pl $image.kmeansOutput.all.matrix
done
David Zaslavsky
Thank you David
Alos
Hi Dave that did it! Thank you very much.
Alos
+1  A: 

If you really want to have an array, it is possible: Advanced Bash-Scripting Guide: Arrays

But perhaps it would be better (or at least simpler) to either modify your Perl script to handle a list of files, or handle each image separately.

nikc
Awesome thank you nikc
Alos
+1  A: 

You don't normally want to have dollar signs on variable names on the left hand side of an assignment.

You could make an array like this: image_array=($(ls *.png)) but that fails if filenames have spaces in them.

Don't parse ls, though, for that reason at least.

Don't use backticks, use $() instead.

You have nested for loops that seem to be in conflict with each other. The structure in knittl's answer is the one you should use.

Dennis Williamson
Thank you Dennis
Alos