Bash: comment on the long pipeline

I found it quite powerful to create long pipelines in bash scripts, but the main drawback that I see is that there seems to be no way to insert comments.

As an example, is there a good way to add comments to this script?

#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" 
+4
source share
4 answers

This also works:

 # comment here ls -t $HOME/.vnc/*.pid | #comment here xargs -n1 | #another comment ... 

based on fooobar.com/questions/1341087 / .... it boils down to s/|//;s!\!|! .

+3
source

Let the pipe be the last character of each line and use # instead of \ , for example:

 ls -t $HOME/.vnc/*.pid | #comment here xargs -n1 | #another comment ... 
+20
source

If they are not spectacularly long pipelines, you do not need to comment on the embedded text, just comment on top:

 # 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" 

As long as the comments are relatively localized, this is normal. Therefore, I would not put them at the top of the file (unless, of course, your pipeline was the first thing in the file) or scratched toilet paper and locked yourself on your desk at work.

But the first thing I do when I look at the block is to look for comments immediately preceding the block. Even in C code, I donโ€™t comment on every line, as the purpose of the comments is to basically display why and high-level how .

0
source
 #!/bin/bash for pid in $HOME/.vnc/*.pid; do tmp=${pid##*/} disp=${tmp%.*} xdpyinfo -display "$disp" | # commment here egrep "^name|dimensions|depths" done 

I donโ€™t understand the need for vncconfig if all it does is append '(user)', which is subsequently removed to call xdpyinfo . In addition, all these channels take up quite a bit of overhead, if you time your script against mine, I think you will find comparable performance, if not faster.

0
source

Source: https://habr.com/ru/post/1341087/


All Articles