4

When I have long multi-line piped commands in my scripts I would like to comment on what each line does, but I haven't found a way of doing so.

Given this snippet:

git branch -r --merged \
| grep "  $remote"  \
| egrep -v "HEAD ->"  \
| util.esed -n 's/  \w*\/(.*)/\1/p' \
| egrep -v \
    "$(skipped $skip | util.esed -e 's/,/|/g' -e 's/(\w+)/^\1$/g' )" \
| paste -s

Is it possible to insert comments in between the lines? It seems that using the backslash to escape the newline prevents me from adding comments at the end of the line, and I can't add the comment before the backslash, as that would hide the escaping.

Pseudo-code of what I would like the above script to look like

It seems I was unclear (?) of what I wanted by the above section, so to have a clue on what I am looking for, it should be in the similar vein of this:

git branch -r --merged \ # list merged remote branches
| grep "  $remote"  \    # filter out the ones for $remote
| egrep -v "HEAD ->"  \  # remove some garbage

#strip some whitespace:
| util.esed -n 's/  \w*\/(.*)/\1/p' \     

#  remove the skipped branches:
| egrep -v \
    "$(skipped $skip | util.esed -e 's/,/|/g' -e 's/(\w+)/^\1$/g' )" \
| paste -s               # something else

It doesn't have to be exactly like this (obviously, it's not valid syntax), but something similar. If it's not possible directly, due to syntactical restrictions, perhaps it's possible to write self-modifying code that will have comments that are removed before executing it?

oligofren
  • 20,744
  • 16
  • 93
  • 180

1 Answers1

3

You can try something like that:

git branch --remote | # some comment
    grep origin | # another comment
    tr a-z A-Z
Casual Coder
  • 1,492
  • 2
  • 14
  • 15
  • Thanks for this. It's not a general solution for commenting, but when it comes to piped commands only, which is what the question was about, it is perfect. You can see me employing it here: https://github.com/fatso83/dotfiles/commit/945f5b4deda7189459a0ed486fba132ac4b574a2#diff-5213268af30c91452c53ccc19056297dR77 – oligofren Apr 26 '18 at 13:13
  • I am glad I could help. Definitely, after a change, one can much easier comprehend what is going on with those multiple pipes. – Casual Coder Apr 26 '18 at 13:50