| 1 | #
|
|---|
| 2 | # varenv.sh
|
|---|
| 3 | #
|
|---|
| 4 | # Test the behavior of the shell with respect to variable and environment
|
|---|
| 5 | # assignments
|
|---|
| 6 | #
|
|---|
| 7 | expect()
|
|---|
| 8 | {
|
|---|
| 9 | echo expect "$@"
|
|---|
| 10 | }
|
|---|
| 11 |
|
|---|
| 12 | a=1
|
|---|
| 13 | b=2
|
|---|
| 14 | c=3
|
|---|
| 15 | d=4
|
|---|
| 16 | e=5
|
|---|
| 17 | f=6 g=7 h=8
|
|---|
| 18 |
|
|---|
| 19 | a=3 b=4 $CHMOD $MODE $FN
|
|---|
| 20 |
|
|---|
| 21 | # This should echo "3 4" according to Posix.2
|
|---|
| 22 | expect "3 4"
|
|---|
| 23 | echo $a $b
|
|---|
| 24 |
|
|---|
| 25 | set -k
|
|---|
| 26 |
|
|---|
| 27 | # Assignment statements made when no words are left affect the shell's
|
|---|
| 28 | # environment
|
|---|
| 29 | a=5 b=6 $CHMOD c=7 $MODE d=8 $FN e=9
|
|---|
| 30 |
|
|---|
| 31 | expect "5 6 7 8 9"
|
|---|
| 32 | echo $a $b $c $d $e
|
|---|
| 33 |
|
|---|
| 34 | $CHMOD f=7 $MODE g=8 $FN h=9
|
|---|
| 35 | expect "7 8 9"
|
|---|
| 36 | echo $f $g $h
|
|---|
| 37 |
|
|---|
| 38 | set +k
|
|---|
| 39 |
|
|---|
| 40 | # The temporary environment does not affect variable expansion, only the
|
|---|
| 41 | # environment given to the command
|
|---|
| 42 |
|
|---|
| 43 | export HOME=/usr/chet
|
|---|
| 44 | expect $HOME
|
|---|
| 45 | echo $HOME
|
|---|
| 46 |
|
|---|
| 47 | expect $HOME
|
|---|
| 48 | HOME=/a/b/c /bin/echo $HOME
|
|---|
| 49 |
|
|---|
| 50 | expect $HOME
|
|---|
| 51 | echo $HOME
|
|---|
| 52 |
|
|---|
| 53 | # This should echo /a/b/c
|
|---|
| 54 | expect /a/b/c
|
|---|
| 55 | HOME=/a/b/c printenv HOME
|
|---|
| 56 |
|
|---|
| 57 | set -k
|
|---|
| 58 |
|
|---|
| 59 | # This should echo $HOME 9, NOT /a/b/c 9
|
|---|
| 60 |
|
|---|
| 61 | expect "$HOME"
|
|---|
| 62 | HOME=/a/b/c /bin/echo $HOME c=9
|
|---|
| 63 | expect "$HOME 7"
|
|---|
| 64 | echo $HOME $c
|
|---|
| 65 |
|
|---|
| 66 | # I claim the next two echo calls should give identical output.
|
|---|
| 67 | # ksh agrees, the System V.3 sh does not
|
|---|
| 68 |
|
|---|
| 69 | expect "/a/b/c 9 /a/b/c"
|
|---|
| 70 | HOME=/a/b/c $ECHO a=$HOME c=9
|
|---|
| 71 | echo $HOME $c $a
|
|---|
| 72 |
|
|---|
| 73 | expect "/a/b/c 9 /a/b/c"
|
|---|
| 74 | HOME=/a/b/c a=$HOME c=9
|
|---|
| 75 | echo $HOME $c $a
|
|---|
| 76 | set +k
|
|---|
| 77 |
|
|---|
| 78 | # How do assignment statements affect subsequent assignments on the same
|
|---|
| 79 | # line?
|
|---|
| 80 | expect "/a/b/c /a/b/c"
|
|---|
| 81 | HOME=/a/b/c a=$HOME
|
|---|
| 82 | echo $HOME $a
|
|---|
| 83 |
|
|---|
| 84 | # The system V.3 sh does this wrong; the last echo should output "1 1",
|
|---|
| 85 | # but the system V.3 sh has it output "2 2". Posix.2 says the assignment
|
|---|
| 86 | # statements are processed left-to-right. bash and ksh output the right
|
|---|
| 87 | # thing
|
|---|
| 88 | c=1
|
|---|
| 89 | d=2
|
|---|
| 90 | expect "1 2"
|
|---|
| 91 | echo $c $d
|
|---|
| 92 | d=$c c=$d
|
|---|
| 93 | expect "1 1"
|
|---|
| 94 | echo $c $d
|
|---|
| 95 |
|
|---|
| 96 | # just for completeness
|
|---|
| 97 | unset d c
|
|---|
| 98 | expect unset
|
|---|
| 99 | echo ${d-unset}
|
|---|
| 100 |
|
|---|
| 101 | # no output
|
|---|
| 102 | export a
|
|---|
| 103 | a=bcde
|
|---|
| 104 | export a
|
|---|
| 105 | /bin/true 2>/dev/null
|
|---|
| 106 |
|
|---|
| 107 | func()
|
|---|
| 108 | {
|
|---|
| 109 | local YYZ
|
|---|
| 110 |
|
|---|
| 111 | YYZ="song by rush"
|
|---|
| 112 | echo $YYZ
|
|---|
| 113 | echo $A
|
|---|
| 114 | }
|
|---|
| 115 |
|
|---|
| 116 | YYZ="toronto airport"
|
|---|
| 117 | A="AVAR"
|
|---|
| 118 | echo $YYZ
|
|---|
| 119 | echo $A
|
|---|
| 120 | A=BVAR func
|
|---|
| 121 | echo $YYZ
|
|---|
| 122 | echo $A
|
|---|
| 123 |
|
|---|
| 124 | export A
|
|---|
| 125 | # Make sure expansion doesn't use assignment statements preceding a builtin
|
|---|
| 126 | A=ZVAR echo $A
|
|---|
| 127 |
|
|---|
| 128 | XPATH=/bin:/usr/bin:/usr/local/bin:.
|
|---|
| 129 | func2()
|
|---|
| 130 | {
|
|---|
| 131 | local z=yy
|
|---|
| 132 | local -a avar=( ${XPATH//: } )
|
|---|
| 133 | echo ${avar[@]}
|
|---|
| 134 | local
|
|---|
| 135 | }
|
|---|
| 136 |
|
|---|
| 137 | avar=42
|
|---|
| 138 | echo $avar
|
|---|
| 139 | func2
|
|---|
| 140 | echo $avar
|
|---|
| 141 |
|
|---|
| 142 | # try to set an attribute for an unset variable; make sure it persists
|
|---|
| 143 | # when the variable is assigned a value
|
|---|
| 144 | declare -i ivar
|
|---|
| 145 |
|
|---|
| 146 | ivar=10
|
|---|
| 147 |
|
|---|
| 148 | declare -p ivar
|
|---|
| 149 | unset ivar
|
|---|
| 150 |
|
|---|
| 151 | # export an unset variable, make sure it is not suddenly set, but make
|
|---|
| 152 | # sure the export attribute persists when the variable is assigned a
|
|---|
| 153 | # value
|
|---|
| 154 | export ivar
|
|---|
| 155 | echo ${ivar-unset}
|
|---|
| 156 |
|
|---|
| 157 | ivar=42
|
|---|
| 158 | declare -p ivar
|
|---|
| 159 |
|
|---|
| 160 | # make sure set [-+]o ignoreeof and $IGNOREEOF are reflected
|
|---|
| 161 | unset IGNOREEOF
|
|---|
| 162 | set +o ignoreeof
|
|---|
| 163 | set -o ignoreeof
|
|---|
| 164 | if [ "$IGNOREEOF" -ne 10 ]; then
|
|---|
| 165 | echo "./varenv.sh: set -o ignoreeof is not reflected in IGNOREEOF" >&2
|
|---|
| 166 | fi
|
|---|
| 167 | unset IGNOREEOF
|
|---|
| 168 | set +o ignoreeof
|
|---|
| 169 |
|
|---|
| 170 | # older versions of bash used to not reset RANDOM in subshells correctly
|
|---|
| 171 | [[ $RANDOM -eq $(echo $RANDOM) ]] && echo "RANDOM: problem with subshells"
|
|---|
| 172 |
|
|---|
| 173 | # make sure that shopt -o is reflected in $SHELLOPTS
|
|---|
| 174 | # first, get rid of things that might be set automatically via shell
|
|---|
| 175 | # variables
|
|---|
| 176 | set +o posix
|
|---|
| 177 | set +o ignoreeof
|
|---|
| 178 | set +o monitor
|
|---|
| 179 | echo $-
|
|---|
| 180 | echo ${SHELLOPTS}
|
|---|
| 181 | shopt -so physical
|
|---|
| 182 | echo $-
|
|---|
| 183 | echo ${SHELLOPTS}
|
|---|
| 184 |
|
|---|
| 185 | # and make sure it is readonly
|
|---|
| 186 | readonly -p | grep SHELLOPTS
|
|---|
| 187 |
|
|---|
| 188 | # This was an error in bash versions prior to bash-2.04. The `set -a'
|
|---|
| 189 | # should cause the assignment statement that's an argument to typeset
|
|---|
| 190 | # to create an exported variable
|
|---|
| 191 | unset FOOFOO
|
|---|
| 192 | FOOFOO=bar
|
|---|
| 193 | set -a
|
|---|
| 194 | typeset FOOFOO=abcde
|
|---|
| 195 |
|
|---|
| 196 | printenv FOOFOO
|
|---|
| 197 |
|
|---|
| 198 | # test out export behavior of variable assignments preceding builtins and
|
|---|
| 199 | # functions
|
|---|
| 200 | $THIS_SH ./varenv1.sub
|
|---|
| 201 |
|
|---|
| 202 | # more tests; bugs in bash up to version 2.05a
|
|---|
| 203 | $THIS_SH ./varenv2.sub
|
|---|
| 204 |
|
|---|
| 205 | # make sure variable scoping is done right
|
|---|
| 206 | tt() { typeset a=b;echo a=$a; };a=z;echo a=$a;tt;echo a=$a
|
|---|