2010-06-22 91 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中的結構是您應該使用的結構。

相關問題