【问题标题】:Linux watch command not working in scriptLinux watch 命令在脚本中不起作用
【发布时间】:2013-10-04 10:45:51
【问题描述】:

大家好。

我正在编写一个脚本来定期监视与端口的连接(在本例中为 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 '================================='"

【问题讨论】:

    标签: linux bash shell ubuntu debian


    【解决方案1】:

    如果您将脚本写入文件并执行它,它就会起作用。那么它不必是一个可怕的单线,但可以看起来像这样:

    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 格式,但我相信您可以根据需要将其恢复原状。

    【讨论】:

    • 我知道。但是有没有办法让它在“一个”行中。 ?
    • 您为什么要这样做?根据我的更好判断,我在答案中添加了一个示例。
    • 是的,不管代码的可读性如何,我都需要它工作 :)
    【解决方案2】:

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

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

    【讨论】:

    • 谢谢@William_Pursell,我在找那个。我知道这可以以更好的方式实现,就像你的那样,但它是用于测试带有引号的 watch 命令。
    【解决方案3】:

    引用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.
    

    【讨论】:

    • 我应该在哪里应用这个?桌面手表用法:手表 [-dhntv] [--differences[=cumulative]] [--help] [--interval=] [--no-title] [--version]
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-06-05
    • 2014-05-23
    • 1970-01-01
    • 2023-02-05
    • 1970-01-01
    • 2020-10-31
    • 2017-03-05
    相关资源
    最近更新 更多