Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/email/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
bash:注释一条长管道_Bash_Comments_Pipeline_Sh - Fatal编程技术网

bash:注释一条长管道

bash:注释一条长管道,bash,comments,pipeline,sh,Bash,Comments,Pipeline,Sh,我发现在bash脚本中创建长管道功能非常强大,但我看到的主要缺点是似乎没有插入注释的方法 例如,有没有一种好的方法可以向脚本添加注释 #find all my VNC sessions ls -t $HOME/.vnc/*.pid \ | xargs -n1 \ | sed 's|\.pid$||; s|^.*\.vnc/||g' \ | xargs -P50 --replace vncc

我发现在bash脚本中创建长管道功能非常强大,但我看到的主要缺点是似乎没有插入注释的方法

例如,有没有一种好的方法可以向脚本添加注释

#find all my VNC sessions
ls -t $HOME/.vnc/*.pid                  \
    | xargs -n1                         \
    | sed 's|\.pid$||; s|^.*\.vnc/||g'  \
    | xargs -P50 --replace vncconfig -display {} -get desktop \
    | grep "($USER)"                    \
    | awk '{print $1}'                  \
    | xargs -n1 xdpyinfo -display       \
    | egrep "^name|dimensions|depths"

让管道成为每行的最后一个字符,并使用
\
而不是
\
,如下所示:

ls -t $HOME/.vnc/*.pid | #comment here
   xargs -n1 | #another comment 
   ...
除非它们是非常长的管道,否则您不必内联注释,只需在顶部注释即可:

# Find all my VNC sessions.
#   xargs does something.
#   sed does something else
#   the second xargs destroys the universe.
#   :
#   and so on.

ls -t $HOME/.vnc/*.pid                  \
    | xargs -n1                         \
    | sed 's|\.pid$||; s|^.*\.vnc/||g'  \
    | xargs -P50 --replace /opt/tools/bin/restrict_resources -T1 \
            -- vncconfig -display {} -get desktop 2>/dev/null \
    | grep "($USER)"                    \
    | awk '{print $1}'                  \
    | xargs -n1 xdpyinfo -display       \
    | egrep "^name|dimensions|depths"
只要评论相对本地化,就可以了。所以我不会把它们放在文件的顶部(当然,除非你的管道是文件中的第一件东西),也不会在卫生纸上乱写,工作时锁在桌子上

但我在查看块时要做的第一件事是查找块前面的注释。即使在C代码中,我也不会注释每一行,因为注释的目的主要是显示
为什么
和高级
如何

#!/bin/bash

for pid in $HOME/.vnc/*.pid; do
    tmp=${pid##*/}
    disp=${tmp%.*}
    xdpyinfo -display "$disp" | # commment here
    egrep "^name|dimensions|depths"
done
我不理解
vnconfig
的必要性,如果它所做的只是附加“(用户)”,您随后会删除它以调用
xdpyinfo
。此外,所有这些管道都会占用相当大的开销,如果你
时间
你的脚本与我的脚本相比,我认为你会发现性能相当,如果不是更快的话。

这也有效:

# comment here
ls -t $HOME/.vnc/*.pid |
 #comment here
 xargs -n1 |
 #another comment
 ...
基于。
它归结为
s/|/;s!\!|

并注意,如果管道在管道中的每一步之后,您不需要(大部分)可怕和多余的反斜杠,它们看起来像是从sea shell脚本中爬进来的。这并不能回答您的问题,但我肯定您可以移除至少3个管道。不幸的是,我似乎没有
vnconfig
,所以我无法测试它out@bukzor如果您提供
xargs
调用
vnconfig
的输入和输出,我很乐意为您提供帮助。不过,只要你看到
grep
后面跟着
awk
,你就可以随时将它们组合起来。在你的例子中,
awk-v user=$user'$0~ user{print$1}
@SiegeX:我已经删除了我的非标准工具,所以你现在应该可以运行它了。它只是为不存在的会话提供了一个超时。输入是X个显示名称,输出是相同的,但是用户名在参数中是:myhost:2.0(mylogin)@bukzor请看我的答案。我仍然不得不猜测,因为正如前面提到的,我没有
vnconfig
我在一月份的superuser上回答了这个问题:重点是要再次检查VNC会话是否真的是我的。经常发生的情况是,一台机器崩溃,杀死了我的VNC,另一个用户突然出现,得到了以前属于我的显示器。串行执行时性能较慢,而xargs提供并行化(-P50)。许多查询在30秒后不会返回并超时。