Linux watch命令无法在脚本中运行

时间:2013-09-28 02:30:28

标签: linux bash shell ubuntu debian

大家好。

我正在编写一个脚本,用于定期监视与端口的连接(在本例中为80)。

我写了这个简短的剧本。

echo '=================================';a=`sudo lsof -i :80`;echo $a | awk '{print $1," ",$2," ",$3," ",$8}'; b=`echo $a | wc -l`; b=$(($b - 1));echo Total SSH Connections: $b;echo '================================='

输出结果为:

=================================  
COMMAND   PID   USER   NODE  
acwebseca   90   root   TCP  
acwebseca   90   root   TCP  
acwebseca   90   root   TCP  
acwebseca   90   root   TCP  
acwebseca   90   root   TCP  
acwebseca   90   root   TCP  
acwebseca   90   root   TCP  
acwebseca   90   root   TCP  
acwebseca   90   root   TCP  
acwebseca   90   root   TCP  
acwebseca   90   root   TCP  
acwebseca   90   root   TCP  
acwebseca   90   root   TCP  
acwebseca   90   root   TCP  
acwebseca   90   root   TCP  
acwebseca   90   root   TCP  
acwebseca   90   root   TCP  
acwebseca   90   root   TCP  
acwebseca   90   root   TCP  
Total SSH Connections: 19  
=================================  

但是当尝试使用watch命令时会抛出错误,当我取消命令时看不到输出我看到这样的错误:

sh: PID: command not found
                          sh: -c: line 1: syntax error near unexpected token `('
                                                                                sh: -c: line 1: `acwebseca  90 root   37u  IPv4 0x81ae738f91e7bed9      0t0  TCP 192.168.0.11:49915->108.160.163.33:http (ESTABLISHED)'

我该如何解决这个问题。

watch -n 2 "echo '=================================';a=`sudo lsof -i :80`;echo $a | awk '{print $1," ",$2," ",$3," ",$8}'; b=`echo $a | wc -l`; b=$(($b - 1));echo Total SSH Connections: $b;echo '================================='"

3 个答案:

答案 0 :(得分:3)

如果您将脚本编写到文件中并执行它,它就可以工作。然后它不一定是一个可怕的单行,但看起来像这样:

echo '================================='
a=`sudo lsof -i :80`
echo $a | awk '{print $1," ",$2," ",$3," ",$8}'
b=`echo $a | wc -l`
b=$(($b - 1))
echo Total SSH Connections: $b
echo '================================='

只需将其放入文件中,然后运行watch my-script.sh即可。这解决了问题,并使代码可以同时读取。

编辑:如果你真的想要一个单行,这是一个坏主意,你可以试试这个:

watch 'echo =================================;a=`lsof -i :80`;echo $a | awk "{print \$1, \$2, \$3, \$8}"; b=`echo $a | wc -l`; b=$(($b - 1));echo Total SSH Connections: $b;echo ================================='

基本上我调整了引用以使其正常运行;我可能稍微搞砸了awk格式,但我确信如果需要的话你可以将它恢复原状。

答案 1 :(得分:2)

这不仅仅是一个评论,而是一个答案,因为我不打算将命令传递给watch。但格式化评论很难。您可以通过在awk中执行更多操作来大大简化命令:

 sudo lsof -i :80 | awk '
      BEGIN { d="================================="; print d}
      {print $1," ",$2," ",$3," ",$8}'
      END { print "Total SSH Connections:", NR-1; print d}'

答案 2 :(得分:1)

引自man watch

Note that command is given to "sh -c" which means that you may need
to use extra quoting to get the desired effect.  You can disable this
with the -x or --exec option, which passes the command to exec(2) instead.

Note that POSIX option processing is used (i.e., option processing stops
at the first non-option argument).  This means that
flags after command don't get interpreted by watch itself.