2010-06-22 95 views
4

嗨,我是一个bash编程的新手,需要一些帮助。我正在建立一个图像处理管道。我想能够在一个文件夹中将png图像传递给clusterImage.pl,然后将输出的文件传递给seperateObjects.pl,输出的文件具有相同的名称,但具有kmeansOutput。 all.matrix附加到最后。以下是我到目前为止,但它不起作用。 任何帮助将不胜感激。谢谢如何使用Bash获取一个文件夹中的.png文件数组

#!/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 
+0

尝试使用'-x'选项来[调试你的bash脚本](http://tldp.org/LDP/Bash-Beginners-Guide/html/sect_02_03.html)。 – jschmier 2010-06-22 18:11:18

+0

“不工作”不提供任何信息。请发布错误消息或预期行为的特定偏差。 – 2010-06-22 18:47:46

回答

4

我看到你的代码的几个问题(或潜在的改进):

  1. 你不需要循环for i in $list因为你从不在脚本中使用$i - 这会导致一遍又一遍地执行相同的操作(与目录中.png文件的数目相同)
  2. 您不需要使用Bash数组,因为Bash可以迭代列表中的不同文件名,如*.png
  3. 我怀疑你的意思是在目录中的每个.png文件上运行perl clusterImage.pl ...还是你?这很难说。编辑你的问题,更清楚地解释你的意思,我可以相应地编辑我的答案。
  4. 可以使用短路,因为他们称呼它,而不是if声明:[ -f file.png ] && echo "file exists"短于

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

如果我理解你正在试图做什么(我我不确定我是否),我认为这可能适合你。对于目录中的每个图像,这将运行perl clusterImage.pl <name_of_image.png>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 
+0

谢谢大卫 – Alos 2010-06-23 14:18:10

+0

嗨戴夫这样做!非常感谢你。 – Alos 2010-06-23 14:30:39

7

这应该工作:

for file in *.png; do 
    # do stuff with your file: 
    perl clusterImage.pl "$file"; 
    # … 
done 
+0

谢谢你knittl – Alos 2010-06-23 14:22:04

1

如果你真的想有一个数组,它是可能的:Advanced Bash-Scripting Guide: Arrays

但也许它会更好(或至少简单)要么修改你的Perl脚本来处理的文件列表,或分别处理每个图像。

1

您通常不希望在作业左侧的变量名上有美元符号。

可能做一个这样的数组:image_array=($(ls *.png))但如果文件名中有空格,则失败。

Don't parse ls,但至少因为这个原因。

Don't use backticks,改为使用$()

您嵌套了看起来相互冲突的循环。 knittl's answer中的结构是您应该使用的结构。

相关问题