12

I've found that it's quite powerful to create long pipelines in bash scripts, but the main drawback that I see is that there doesn't seem to be a 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"
5
  • 1
    This doesn't answer your question but I'm positive you could remove at least 3 of those pipes. Unfortunately, I don't seem to have vncconfig so I cant test it out
    – SiegeX
    Commented Feb 24, 2011 at 5:46
  • @bukzor I'd be happy to if you provide the input to and output of the xargs call to vncconfig. Just off the bat though, whenever you see a grep followed by awk you can always combine them. In your case awk -v user=$USER '$0 ~ user{print $1}'
    – SiegeX
    Commented Feb 24, 2011 at 7:23
  • @SiegeX: I've removed my non-standard tool, so you should be able to run it now. It was just providing a timeout for non-existent sessions. The input is X display names, and the output is the same, but with user name in parens like: myhost:2.0 (mylogin)
    – bukzor
    Commented Feb 24, 2011 at 16:46
  • @bukzor see my answer. I still had to guess because as mentioned I don't have vncconfig
    – SiegeX
    Commented Feb 24, 2011 at 18:00
  • I answered this in January on superuser: superuser.com/questions/238791/…
    – Mikel
    Commented Feb 24, 2011 at 22:47

4 Answers 4

29

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

ls -t $HOME/.vnc/*.pid | #comment here
   xargs -n1 | #another comment 
   ...
1
  • 3
    And observe that if the pipe is after each step in the pipeline, you don't need (most of) the horrid and superfluous backslashes which look like they crept in from a sea-shell script. Commented Feb 24, 2011 at 5:46
10

This works too:

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

based on https://stackoverflow.com/a/5100821/1019205. it comes down to s/|//;s!\!|!.

1

Unless they're spectacularly long pipelines, you don't have to comment inline, just comment at the 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 comments are relatively localised, it's fine. So I wouldn't put them at the top of the file (unless your pipeline was the first thing in the file, of course) or scribbled down on toilet paper and locked in your desk at work.

But the first thing I do when looking at a block is to look for comments immediately preceding the block. Even in C code, I don't comment every line, since the intent of comments is to mostly show the why and a high-level how.

-1
#!/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 you subsequently remove for the call to xdpyinfo. Also, all those pipes take quite a bit of overhead, if you time your script vs mine I think you'll find the performance comparable if not faster.

1
  • The point is to double-check that the VNC session is really mine. It often happens that a machine crashes, kills my VNC, and another user pops on and gets the display that used to be mine. Performance is slower as you're doing it serially, while xargs gives parallelization (-P50). Many of the queries don't return and time out after 30 seconds.
    – bukzor
    Commented Feb 25, 2011 at 3:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.