用尾部-n0 -F

时间:2019-09-19 11:38:18

标签: sed grep tail

我正在监视星号日志文件中是否有脱机的同级。 该if部分工作正常,但sed命令在else部分中未执行,尽管echo命令有效。我需要更改什么

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

1 个答案:

答案 0 :(得分:2)

您遇到引号问题-当字符串包含shell变量时,您已使用单引号:

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

尝试使用双引号代替

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