2014-05-11 105 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返回非零状态。