bashresty

Please help me de-cipher this line of bash scripting code


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


Solution

  • 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.

    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
    )