Live data from Hacker News

Help Message for Shell Scripts

samizdat.dev

1–10 of 129 posts

Re: Help Message for Shell Scripts

#2
This seems like a neat sed trick, but I'm not sure that it's useful for this particular case?

When I write a shell script, I often write a help function if it's not a totally trivial script, but there's no need for this cryptic sed expression, right? You can just call `echo` a few times and do it the obvious way. That works better for maintainability and if you put it at the top of the file then it's immediately visible when opening or using `head` on it.

Neat trick though - sed is super useful for all kinds of things. I had a co-worker who bound a keybinding to a sed one-liner that would automagically reconfigure some files that needed to be changed regularly. I ended up using that trick to allow for changing my terminal console colorscheme with a single command.

Re: Help Message for Shell Scripts

#3

This seems like a neat sed trick, but I'm not sure that it's useful for this particular case? When I write a shell script, I often write a help function if it's not a totally trivial script, but there's no need for this cryptic sed expression, right? You can just call `echo` a few times and do it the obvious way. That works better for maintainability and if you put it at the top of the file then it's immediately visi…

It's a little sad that standard shell here documents only support elliding leading tabs (it wouldn't be so sad if the record separator hadn't been thrown under the bus - having a character for indentation distinct from space is good... In theory).

But at any rate my typical usage() is generally along these lines (warning watch out for expansions):

  usage()
  {
    cat -  [-o|--optional-param] 

       Etc. Possibly referencing
       default value: ${DEFAULT_VALUE}
    EOF
  }

Re: Help Message for Shell Scripts

#4
Hmm:

% sed -rn 's/^### ?//;T;p' testfile

sed: 1: "s/^### ?//;T;p": invalid command code T

Looks like it might need GNU Sed or something. But honestly if I want to read the top of the file less works just as well.

Re: Help Message for Shell Scripts

#5
You can also use a "here document"

    help() {
      cat  
    
    Options:
         Input file to read.
        Output file to write. Use '-' for stdout.
      -h        Show this message.
    EOH
    }
If the indentation bugs you, you can use a simpler sed trick to remove leading space so that you can indent it as desired:

    help() {
      sed -e 's/    //'  
        
        Options:
             Input file to read.
            Output file to write. Use '-' for stdout.
          -h        Show this message.
    EOH
    }

Re: Help Message for Shell Scripts

#6
post #4

Hmm: % sed -rn 's/^### ?//;T;p' testfile sed: 1: "s/^### ?//;T;p": invalid command code T Looks like it might need GNU Sed or something. But honestly if I want to read the top of the file less works just as well.

Yeah, "man sed" on my machine says, "This is a GNU extension."

You could do the same thing with awk instead:

    awk '{ if (sub("^### ?", "")) { print; } else { exit; } }'

Re: Help Message for Shell Scripts

#7
Handling of arguments is one of the reasons I reach for Python or Powershell instead of a bash script when writing my own stuff.

https://docs.python.org/3/library/argparse.html is great.

Powershell has the Param keyword that functions like argparse in Python

https://docs.microsoft.com/en-us/powershell/module/microsoft...

Re: Help Message for Shell Scripts

#8

You can also use a "here document" help() { cat Options: Input file to read. Output file to write. Use '-' for stdout. -h Show this message. EOH } If the indentation bugs you, you can use a simpler sed trick to remove leading space so that you can indent it as desired: help() { sed -e 's/ //' Options: Input file to read. Output file to write. Use '-' for stdout. -h Show this message. EOH }

You can also use add a hyphen ( https://linuxhint.com/bash-heredoc-tutorial/

Re: Help Message for Shell Scripts

#9
post #4

Hmm: % sed -rn 's/^### ?//;T;p' testfile sed: 1: "s/^### ?//;T;p": invalid command code T Looks like it might need GNU Sed or something. But honestly if I want to read the top of the file less works just as well.

Yeah, "man sed" on my machine says, "This is a GNU extension." You could do the same thing with awk instead: awk '{ if (sub("^### ?", "")) { print; } else { exit; } }'

Or

    sed -rn 's/^### ?//p'

Re: Help Message for Shell Scripts

#10
post #7

Handling of arguments is one of the reasons I reach for Python or Powershell instead of a bash script when writing my own stuff. https://docs.python.org/3/library/argparse.html is great. Powershell has the Param keyword that functions like argparse in Python https://docs.microsoft.com/en-us/powershell/module/microsoft...

My rule for a long time has been that any time you have more than one screen's worth of code and/or are using any of arrays, functions, or more than simple positional arguments you'll save time by rewriting it in Python. shellcheck has helped soften that a bit but the usability difference is pretty noticeable even with multiple decades writing shell scripts.
Post reply on HN