2014-05-11 101 views
0
#!/bin/bash 

while read line; do 
    grep "$line" file1.txt 
    if [ $status -eq 1] 
    echo "$line" >> f2.txt 
done < f3.txt 

當我執行包含上述腳本的shell腳本時。我得到以下錯誤:從使用shell腳本的文件逐行讀取

./test.sh: line 7: syntax error near unexpected token `done' 
./test.sh: line 7: `done < f3.txt' 

任何人都可以幫助我,爲什麼我得到這個錯誤?

+0

在問這裏之前,請先查詢http://www.shellcheck.net/以查找語法錯誤! – oberlies

回答

2
#!/bin/bash 
while read line; do 
    grep "$line" file1.txt 
    if [ $? -eq 1 ]; then 
     echo "$line" >> f2.txt 
    fi 
done < f3.txt 

你的代碼有一堆錯誤。如果是錯誤的

  • 沒有結束前缺少空間

    1. 結構bracked
    2. 我相信你正在使用$狀態的事情是錯誤的。您使用$檢查命令的返回狀態?
  • 2

    腳本可以被簡化成這樣:

    #!/bin/bash 
    
    while read -r line; do 
        grep -q "$line" file1.txt || echo "$line" >> f2.txt 
    done < f3.txt 
    

    這裏echo "$line" >> f2.txt將執行只有當grep -q返回非零狀態。