Bash错误回显命令

时间:2013-07-16 00:24:39

标签: bash scripting echo

我有问题。我需要显示一段时间的回声,我使用两个回声第一个工作,但第二个它给出错误。

#!/bin/bash
conexiuni="/tmp/conexiuni"
if [ "$1" != "" ]; then
netstat -tuan | grep $1 | grep ESTAB | awk '{print $5}' | cut -d: -f1 | sort | uniq -c | sort -n > $conexiuni
else
netstat -tuan  | grep ESTAB | awk '{print $5}' | cut -d: -f1 | sort | uniq -c | sort -n > $conexiuni
fi

cat $conexiuni | while read line
do
  con=`echo ''$line'' | awk '{print $1}'`
  ip=`echo ''$line'' | awk '{print $2}'`
  if [ "$con" -gt "4" ]; then
       `echo -e "$ip" >> /var/log/drop_sc_ip`
       `echo -e "$ip"`
  fi
done
if [  -f "$conexiuni" ];
then
    `rm -rf $conexiuni`
fi

错误是:

./show_conn: line 15: 8.97.80.2: command not found
./show_conn: line 15: 8.76.109.13: command not found
./show_conn: line 15: 8.33.15.2: command not found
./show_conn: line 15: 9.118.226.3: command not found

1 个答案:

答案 0 :(得分:1)

你可以在没有反引号的情况下编写这个部分:

if [ "$con" -gt "4" ]; then
       echo -e "$ip" >> /var/log/drop_sc_ip
       echo -e "$ip"
fi

在这一部分也是如此:

rm -rf $conexiuni

使用反引号,它首先执行反引号内部的操作,然后尝试执行反引号的输出。

并更改循环:

while read con ip
do
    if [ "$con" -gt "4" ]; then
       echo -e "$ip" >> /var/log/drop_sc_ip
       echo -e "$ip"
    fi
done < $conexiuni