Skip to content

Instantly share code, notes, and snippets.

@kunalb
Created October 19, 2023 00:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kunalb/545b76fdbf021970d8efbd67e40b9045 to your computer and use it in GitHub Desktop.
Save kunalb/545b76fdbf021970d8efbd67e40b9045 to your computer and use it in GitHub Desktop.
Testing out argument handling
#!/bin/bash
set -u
suffixes=( "-" ":-" "+" ":+" "+x" ":+x" )
function header {
printf "%10s | " "\$#"
printf "%10s | " "\$*"
for predicate in "-n" "-z"; do
for suffix in "${suffixes[@]}"; do
header=$(printf '%s ${1%s%s}' $predicate $suffix)
printf "%10s | " "$header"
done
done
echo
}
function test {
printf "%10s | " "\`$#\`"
printf "%10s | " "\`$*\`"
for predicate in "-n" "-z"; do
for suffix in "${suffixes[@]}"; do
str="if [[ $predicate "\${1${suffix}}" ]]; then printf '%10s | ' 'T'; else printf '%10s | ' 'F'; fi"
eval $str
done
done
echo
}
header
test
test ""
test "arg"
./args_table.sh
$# | $* | -n ${1-} | -n ${1:-} | -n ${1+} | -n ${1:+} | -n ${1+x} | -n ${1:+x} | -z ${1-} | -z ${1:-} | -z ${1+} | -z ${1:+} | -z ${1+x} | -z ${1:+x} |
`0` | `` | F | F | F | F | F | F | T | T | T | T | T | T |
`1` | `` | F | F | F | F | T | F | T | T | T | T | F | T |
`1` | `arg` | T | T | F | F | T | T | F | F | T | T | F | F |
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment