Search code examples
sedgreptail

problems while reading log file with tail -n0 -F


i am monitoring the asterisk log file for peers that get offline. the if part is working correct, but the sed command is not executed in the else part, although the echo command works. What do i need to change

tail -n0 -F /var/log/asterisk/messages | \
while read LINE
do
if echo "$LINE" | /bin/grep -q "is now UNREACHABLE!"
then
EXTEN=$(echo $LINE | /bin/grep -o -P "(?<=\').*(?=\')")
echo "$EXTEN is now UNREACHABLE!"
CALLERID=$(/bin/sed -n '/^\['"$EXTEN"'\]/,/^\[.*\]/{/^callerid*/p}' "$SIP" | /usr/bin/awk -F'=' '{ print $2 }')
if .......
then
  .......
fi
elif echo "$LINE" | /bin/grep -q "is now REACHABLE!"
then
EXTEN=$(echo $LINE | /bin/grep -o -P "(?<=\').*(?=\')")
echo "$EXTEN is now REACHABLE!"
if /bin/grep -qi "^$EXTEN;" $OFFLINE; then
  /bin/sed -i '/^$EXTEN;/d' $OFFLINE
fi
fi
done

Solution

  • You have a quoting problem - you've used single quotes when the string includes a shell variable:

    if /bin/grep -qi "^$EXTEN;" $OFFLINE; then
      /bin/sed -i '/^$EXTEN;/d' $OFFLINE
    fi
    

    Try using double quotes instead:

    if /bin/grep -qi "^$EXTEN;" $OFFLINE; then
      /bin/sed -i "/^$EXTEN;/d" $OFFLINE
    fi