2

What does this mean?

  h2t=$((exec 2>&-; (which lynx >/dev/null && echo lynx -stdin -dump) \
            || which html2text || which cat) |tail -n 1)

Ok, h2t=... means it's setting a variable.

I thought double-parens were for arithmetic operations. If that's the case I don't see any arithmetic there, and I'm thoroughly baffled.

Of course, there is a 15-line comment block above that line of code, which explains the intent. Unfortunately the comment is in unicode version of ancient sumerian, and I cannot interpret it.

**Only kidding! There is no comment.

Addendum: This is from https://github.com/micha/resty/blob/master/resty

Cheeso
  • 189,189
  • 101
  • 473
  • 713

2 Answers2

2

twalberg in a comment to my answer spotted it. Turns out the outer $() assigns a command line, depending on the availability of various tools that might be able to convert HTML to text.

Therefore h2t contains either the lynx -stdin -dump command line, or failing that (i.e. lynx not available), html2text or as a last resort cat. The commands for the latter two come from the which invocations, the one for the former from the echo.


It converts HTML to text from stdin.

Let's split it up.

  • exec 2>&- sets up a redirect in the subshell (shuts up stderr, IIRC)
  • the next sub-subshell tries to see whether lynx is installed and runs it, taking input from stdin.
  • the other parts after the || make not much sense because they only evaluate whether html2text and cat are installed, but don't run them
  • then we fetch the last line from that first subshell

Scratch that. Since it's an echo it doesn't do anything. Looks like prototyping to me.

Taking it apart to make more readable:

$(
    exec 2>&-
      (
        which lynx >/dev/null &&
        echo lynx -stdin -dump
      ) ||
    which html2text ||
    which cat
  ) |
  tail -n 1
)
Community
  • 1
  • 1
0xC0000022L
  • 20,597
  • 9
  • 86
  • 152
  • 2
    Actually, it only assigns to a variable a more-or-less suitable command line whose intent is to convert from html to text... If either `lynx` or `html2text` are available, the variable will contain an appropriate command line featuring that tool, otherwise, it falls back to a default of `cat`. – twalberg Mar 11 '13 at 21:18
  • @twalberg: good catch, you're right. Edited it into my answer. Leaving the rest intact. Let me know if you decide to write up your own, so I'll delete mine. – 0xC0000022L Mar 11 '13 at 21:24
  • ok, so it's choosing an html2text program to run, hence the name of the var `h2t`. It isn't running it. It's just finding it, and failing lynx or html2text, chooses cat. If you actually put this in your answer I'll accept it! – Cheeso Mar 11 '13 at 21:57
  • Thanks, @0xC0000022L, I've accepted it. "The application failed to initialize properly"? – Cheeso Mar 12 '13 at 21:32
  • @Cheeso: you mean `0xC0000022L`? -> `STATUS_ACCESS_DENIED` ... most people shortened this to `SAD` which annoyed me mightily ;) – 0xC0000022L Mar 12 '13 at 21:48
  • ah - I see. I don't have my Windows machine anymore and cannot grep winerr.h. I resorted to the google and it failed me. anyway, cool username. – Cheeso Mar 13 '13 at 01:22
2

I don't remember the exact rule for disambiguating the syntax, but it should be parsed like this:

h2t=$(
       (exec 2>&-; (which lynx >/dev/null && echo lynx -stdin -dump) ||
         which html2text ||
         which cat) |
       tail -n 1
     )

In other words, a command substitution consisting of a subshell piped into tail. The subshell is used to provide a "scope" for the exec command, after which another subshell starts a list whose 3 commands are separated by the || operator.

chepner
  • 497,756
  • 71
  • 530
  • 681