| 1 | #!/bin/bash |
| 2 | # Functions and parameters |
| 3 | |
| 4 | DEFAULT=default # Default param value. |
| 5 | |
| 6 | func2 () { |
| 7 | if [ -z "$1" ] # Is parameter #1 zero length? |
| 8 | then |
| 9 | echo "-Parameter #1 is zero length.-" # Or no parameter passed. |
| 10 | else |
| 11 | echo "-Parameter #1 is \"$1\".-" |
| 12 | fi |
| 13 | |
| 14 | variable=${1-$DEFAULT} # What does |
| 15 | echo "variable = $variable" #+ parameter substitution show? |
| 16 | # --------------------------- |
| 17 | # It distinguishes between |
| 18 | #+ no param and a null param. |
| 19 | |
| 20 | if [ "$2" ] |
| 21 | then |
| 22 | echo "-Parameter #2 is \"$2\".-" |
| 23 | fi |
| 24 | |
| 25 | return 0 |
| 26 | } |
| 27 | |
| 28 | echo |
| 29 | |
| 30 | echo "Nothing passed." |
| 31 | func2 # Called with no params |
| 32 | echo |
| 33 | |
| 34 | |
| 35 | echo "Zero-length parameter passed." |
| 36 | func2 "" # Called with zero-length param |
| 37 | echo |
| 38 | |
| 39 | echo "Null parameter passed." |
| 40 | func2 "$uninitialized_param" # Called with uninitialized param |
| 41 | echo |
| 42 | |
| 43 | echo "One parameter passed." |
| 44 | func2 first # Called with one param |
| 45 | echo |
| 46 | |
| 47 | echo "Two parameters passed." |
| 48 | func2 first second # Called with two params |
| 49 | echo |
| 50 | |
| 51 | echo "\"\" \"second\" passed." |
| 52 | func2 "" second # Called with zero-length first parameter |
| 53 | echo # and ASCII string as a second one. |
| 54 | |
| 55 | exit 0 |