1# These are the tests for various style of parameter expansion modifiers supported by ksh93 2 3# Check ${parameter:-word} style of parameter expansion. ':' is optional and is used to check for null (empty string). 4unset foo 5[[ ${foo:-bar} == bar ]] || log_error '${foo:-bar} not bar when foo is not set' 6 7unset foo 8[[ ${foo-bar} == bar ]] || log_error '${foo-bar} not bar when foo is not set' 9 10foo="" 11[[ ${foo-bar} == "" ]] || log_error '${foo-bar} not "" when foo is null' 12 13# Check ${parameter:=word} style of parameter expansion. ':' is optional and is used to check for null (empty string). 14unset foo 15[[ ${foo:=bar} == bar ]] || log_error '${foo:=bar} not bar when foo is not set' 16 17unset foo 18[[ ${foo=bar} == bar ]] || log_error '${foo=bar} not bar when foo is not set' 19 20foo="" 21[[ ${foo=bar} == "" ]] || log_error '${foo=bar} not "" when foo is null' 22 23# Check ${parameter:?word} style of parameter expansion. ':' is optional and is used to check for null (empty string). 24expect="bar not set" 25actual=$( $SHELL -c 'unset foo; print ${foo:?bar not set}' 2>&1 ) 26[[ $actual =~ $expect ]] || log_error '${foo:?bar} does not display error if foo not set' 27 28actual=$( $SHELL -c 'unset foo; print ${foo?bar not set}' 2>&1 ) 29[[ $actual =~ $expect ]] || log_error '${foo?bar} does not display error if foo is not set' 30 31[[ $( $SHELL -c 'foo=""; print ${foo?bar}' 2>&1 ) == "" ]] || log_error '${foo=bar} not "" when foo is null' 32 33# When nothing is specified after :?, a default error message is printed 34unset foo 35[[ $( (print ${foo:?}) 2>&1) =~ "parameter not set" ]] || log_error 'Incorrect error message with ${foo:?} when foo is not set' 36 37foo= 38[[ $( (print ${foo:?}) 2>&1) =~ "parameter null" ]] || log_error 'Incorrect error message with ${foo:?} when foo is null' 39 40# Check ${parameter:+word} style of parameter expansion. ':' is optional and is used to check for null (empty string). 41unset foo 42[[ ${foo:+bar} == "" ]] || log_error '${foo:+bar} not null when foo is not set' 43 44unset foo 45[[ ${foo+bar} == "" ]] || log_error '${foo+bar} not null when foo is not set' 46 47foo="non-null value" 48[[ ${foo:+bar} == "bar" ]] || log_error '${foo:+bar} not bar when foo is not null' 49[[ ${foo+bar} == "bar" ]] || log_error '${foo+bar} not bar when foo is not null' 50 51# Check for regressions on issue #475 where parens after `-', `+', and `=' were causing syntax 52# errors. We check both the unset variable case and the set variable case for each set of symbols. 53unset -v foo 54for op in - :- = := 55do 56 for word in '(word)' 'w(or)d' '(wor)d' 'w(ord)' 'w(ord' 'wor)d' 57 do 58 if [[ $(eval "echo \${foo${op}${word}}") != "${word}" ]] 59 then 60 log_error "\${foo${op}${word}} not ${word} when foo is not set" 61 fi 62 done 63done 64 65foo="non-null value" 66for op in - :- = := 67do 68 for word in '(word)' 'w(or)d' '(wor)d' 'w(ord)' 'w(ord' 'wor)d' 69 do 70 if [[ $(eval "echo \${foo${op}${word}}") != "${foo}" ]] 71 then 72 log_error "\${foo${op}${word}} not ${foo} when foo is set" 73 fi 74 done 75done 76 77unset -v foo 78for op in + :+ 79do 80 for word in '(word)' 'w(or)d' '(wor)d' 'w(ord)' 'w(ord' 'wor)d' 81 do 82 if [[ $(eval "echo \${foo${op}${word}}") != "" ]] 83 then 84 log_error "\${foo${op}${word}} not null when foo is not set" 85 fi 86 done 87done 88 89foo="non-null value" 90for op in + :+ 91do 92 for word in '(word)' 'w(or)d' '(wor)d' 'w(ord)' 'w(ord' 'wor)d' 93 do 94 if [[ $(eval "echo \${foo${op}${word}}") != "${word}" ]] 95 then 96 log_error "\${foo${op}${word}} not ${word} when foo is set" 97 fi 98 done 99done 100 101# ========== 102# https://github.com/att/ast/issues/70 103cat > $TEST_DIR/modifier-in-loop.sh <<EOF 104unset -v var 105for i in 1 2 3 4 5; do 106 case \${var+s} in 107 ( s ) echo set; unset -v var;; 108 ( '' ) echo unset; var=;; 109 esac 110done 111EOF 112 113chmod u+x $TEST_DIR/modifier-in-loop.sh 114 115actual=$($TEST_DIR/modifier-in-loop.sh) 116expect=$'unset\nset\nunset\nset\nunset' 117 118[[ "$actual" = "$expect" ]] || log_error '${var+s} expansion fails in loops' "$expect" "$actual" 119