Live data from Hacker News

Help Message for Shell Scripts

samizdat.dev

81–90 of 129 posts

Re: Help Message for Shell Scripts

#81
We do this for Makefile entries - looking for '##' that we put before each make command.

  ## help: prints this help message
  help:
     @echo "Usage: \n"
     @egrep -h "^## [a-zA-Z0-9\-]*:" ${MAKEFILE_LIST} | sed -e 's/##//' | column -t -s ':' |  sed -e 's/^/ /'

  ## build: builds JAR with dependencies
  build:
     mvn compile

Re: Help Message for Shell Scripts

#82
post #64

I learnt the same trick some years ago, from an article called Shell Scripts Matter : https://dev.to/thiht/shell-scripts-matter So I took some of the advice and tips offered in there, and wrote a template file to be used as a baseline when writing scripts for any project that might need one: https://github.com/j1elo/shell-snippets/blob/master/template... Other resources that I link in the readme of that repo, because…

Thank you for this really helpful comment. It's like an encyclopedia's worth of bash information in one go--much appreciated.

Re: Help Message for Shell Scripts

#83
I'm particular about: If I run a command and its arguments is wrong, it should output error and help messages to STDERR. But if I run a command with --help argument, it should output help messages to STDOUT.

Re: Help Message for Shell Scripts

#84
post #15

I also posted to the github gist, this the sed command here is not cross-plataform friendly. You can accomplish the same thing with an awk command though: awk '/^###/' "$0"

The bit at the end isn't POSIX sh, either. Fixed version:

if [ $# -eq 0 ] || [ "$1" = "-h" ]; then help exit 1 fi

Relevant spec bits:

https://pubs.opengroup.org/onlinepubs/9699919799/utilities/t...

Re: Help Message for Shell Scripts

#85

Even better: Don’t use bash. I started using Python instead of bash. It’s way better to read and more maintainable. If I need the performance of native-Unix commands, I can still use them using subprocess.

Python is surprisingly bad at managing subprocesses, though. Read the subprocess docs closely, it's quite easy to get deadlocks if you try to compose operations the way you can in bash.

Re: Help Message for Shell Scripts

#86
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...

The original argparse is available in every Unix shell: https://pubs.opengroup.org/onlinepubs/9699919799/utilities/g...

Re: Help Message for Shell Scripts

#87
post #48
post #20

Earlier quoted context omitted.

But handling args isn't that bad in bash. while [[ $# -gt 0 ]]; do case "$1" in -h|--help) do_help exit ;; -v|--version) do_version exit ;; -d|--debug) debug=true shift ;; -a|--arg) arg_value=$2 shift 2 ;; esac done

To expand on that pattern: while (( $# )); do case "$1" in -h|--help) usage exit ;; -v|--version) do_version exit ;; -d|--debug) debug=true ;; -a|--arg) arg_value="$2" shift ;; *) if [[ ! -v pos1 ]]; then pos1="$1" elif [[ ! -v pos2 ]]; then pos2="$1" else >&2 printf "%s: unrecognized argument\n" "$1" >&2 usage exit 1 fi esac shift done

The one downside of this is that it doesn't handle squeezing flags as in

    foo -da bar
whereas getopts does. On the other hand, with (the Bash built-in) getopts you're limited to single character flags.

Re: Help Message for Shell Scripts

#88
post #23
post #9

Earlier quoted context omitted.

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

Doesn't appear anyone has tried addressing before replacement - ie the simplest sed work-a-like - if you don't mind the leading ### is just: sed -n '/^### /p' I believe? (equivalent to grep). Then eg: sed -nr '/^### /s/^.{4}(.*)/\1/p' (or without the redundant addressing, just:) sed -nr 's/^### (.*)/\1/p'

You can simplify

    sed -nr 's/^.{4}(.*)/\1/'
to

    sed -nr 's/^.{4}//
And if you use a pattern for the address, you can repeat it in the substitution by using an empty pattern, so

    sed -nr '/^### /s/^.{4}(.*)/\1/p'
is the same as

    sed -nr '/^### /s/^.{4}//p'
is the same as

    sed -nr '/^### /s///p'
at which point I prefer just the substitution:

    sed -nr 's/^### //p'

Re: Help Message for Shell Scripts

#90
post #85

Even better: Don’t use bash. I started using Python instead of bash. It’s way better to read and more maintainable. If I need the performance of native-Unix commands, I can still use them using subprocess.

Python is surprisingly bad at managing subprocesses, though. Read the subprocess docs closely, it's quite easy to get deadlocks if you try to compose operations the way you can in bash.

There are libraries that fix that: https://pypi.org/project/sh/ if you are in position to use external libraries.
Post reply on HN