Created
October 19, 2023 00:49
-
-
Save kunalb/545b76fdbf021970d8efbd67e40b9045 to your computer and use it in GitHub Desktop.
Testing out argument handling
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
./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