문제

#!/bin/bash

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

When I execute shell script containing above script. I get following error:

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

Can anyone help me why I am getting this error?

도움이 되었습니까?

해결책

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

There are a bunch of errors in your code.

  1. structure of if is wrong
  2. there is a space missing before the closing bracked
  3. I believe the $status thing you are using is wrong. You check the return status of a command using $?

다른 팁

Your script can be simplified into this:

#!/bin/bash

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

Here echo "$line" >> f2.txt will execute only when grep -q returns non-zero status.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top