2013-01-17 75 views
-1

我必須做一個練習:有包含該文本bash腳本演習X/Linux的

迪拉DIRB

加載dirX dirY中

迪拉dirD

每行一個文件有兩個目錄,如果第一個目錄的任何文件包含在第二個目錄中,那麼腳本必須檢查每一行。下面是該腳本:

if [ $# -ne 2 ];then 
echo "Error! Insufficient parameters" 
exit 1 
fi 
if [ ! -f $1 ];then 
echo "$1 is not a file" 
exit 2 
fi 
if [ -f $2 ];then 
echo "$2 already exists" 
exit 3 
fi 

file=$(cat $1) 
file_output=$(touch $2) 
count=0 
dir_a='' 
dir_b='' 

for i in $file ; do 
    if [ $count -eq 0 ];then 
     dir_a=$i 
     let count=$count+1 
     continue 
    fi 
    if [ $count -eq 1 ];then 
     dir_b=$i 
     let count=0 
     for f in $(ls $dir_a) ; do 
      if [ -f $dir_b/$f ];then 
       echo "found" 
      fi 
     done 
    fi 
done 

的問題是,它不檢查的最後幾個,在本例的情況下,上述不會檢查夫婦「DIRA dirD」。 這種奇怪行爲的任何想法?

回答

1

.....什麼?

while read src dest 
do 
    for file in "$src"/* 
    do 
    if [ -e "$dest"/"${file##*/}" ] 
    then 
     echo "Found: $src $dest" 
     break 
    fi 
    done 
done 
+0

感謝這個替代解決方案,但我想了解我的循環出了什麼問題。 – AR89

+0

你這樣做完全錯了。 –