## 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 compileHelp Message for Shell Scripts
81–90 of 129 posts
Re: Help Message for Shell Scripts
#82I 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…
Re: Help Message for Shell Scripts
#83Re: Help Message for Shell Scripts
#84I 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"
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
#85Even 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.
Re: Help Message for Shell Scripts
#86Handling 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
#87Earlier 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
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
#88Earlier 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'
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
#89Re: Help Message for Shell Scripts
#90Even 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.