| 1 | #!/usr/bin/env bash |
| 2 | # |
| 3 | # Test set flags, sh flags. |
| 4 | |
| 5 | ### $- with -c |
| 6 | # dash's behavior seems most sensible here? |
| 7 | $SH -o nounset -c 'echo $-' |
| 8 | # stdout: u |
| 9 | # OK bash stdout: huBc |
| 10 | # OK mksh stdout: uhc |
| 11 | # status: 0 |
| 12 | |
| 13 | ### $- with pipefail |
| 14 | set -o pipefail -o nounset |
| 15 | echo $- |
| 16 | # stdout: u |
| 17 | # status: 0 |
| 18 | # OK bash stdout: huB |
| 19 | # OK mksh stdout: ush |
| 20 | # N-I dash stdout-json: "" |
| 21 | # N-I dash status: 2 |
| 22 | |
| 23 | ### sh -c |
| 24 | $SH -c 'echo hi' |
| 25 | # stdout: hi |
| 26 | # status: 0 |
| 27 | |
| 28 | ### empty -c input |
| 29 | # had a bug here |
| 30 | $SH -c '' |
| 31 | # stdout-json: "" |
| 32 | # status: 0 |
| 33 | |
| 34 | ### empty stdin |
| 35 | # had a bug here |
| 36 | echo -n '' | $SH |
| 37 | # stdout-json: "" |
| 38 | # status: 0 |
| 39 | |
| 40 | ### args are passed |
| 41 | $SH -c 'argv.py "$@"' dummy a b |
| 42 | # stdout: ['a', 'b'] |
| 43 | |
| 44 | ### args that look like flags are passed after script |
| 45 | script=$TMP/sh1.sh |
| 46 | echo 'argv.py "$@"' > $script |
| 47 | chmod +x $script |
| 48 | $SH $script --help --help -h |
| 49 | # stdout: ['--help', '--help', '-h'] |
| 50 | |
| 51 | ### args that look like flags are passed after -c |
| 52 | $SH -c 'argv.py "$@"' --help --help -h |
| 53 | # stdout: ['--help', '-h'] |
| 54 | |
| 55 | ### pass short options on command line |
| 56 | $SH -e -c 'false; echo status=$?' |
| 57 | # stdout-json: "" |
| 58 | # status: 1 |
| 59 | |
| 60 | ### pass long options on command line |
| 61 | $SH -o errexit -c 'false; echo status=$?' |
| 62 | # stdout-json: "" |
| 63 | # status: 1 |
| 64 | |
| 65 | ### can continue after unknown option |
| 66 | # dash and mksh make this a fatal error no matter what. |
| 67 | set -o errexit |
| 68 | set -o STRICT || true # unknown option |
| 69 | echo hello |
| 70 | # stdout: hello |
| 71 | # status: 0 |
| 72 | # BUG dash/mksh stdout-json: "" |
| 73 | # BUG dash status: 2 |
| 74 | # BUG mksh status: 1 |
| 75 | |
| 76 | ### set with both options and argv |
| 77 | set -o errexit a b c |
| 78 | echo "$@" |
| 79 | false |
| 80 | echo done |
| 81 | # stdout: a b c |
| 82 | # status: 1 |
| 83 | |
| 84 | ### nounset |
| 85 | echo "[$unset]" |
| 86 | set -o nounset |
| 87 | echo "[$unset]" |
| 88 | echo end # never reached |
| 89 | # stdout: [] |
| 90 | # status: 1 |
| 91 | # OK dash status: 2 |
| 92 | |
| 93 | ### -u is nounset |
| 94 | echo "[$unset]" |
| 95 | set -u |
| 96 | echo "[$unset]" |
| 97 | echo end # never reached |
| 98 | # stdout: [] |
| 99 | # status: 1 |
| 100 | # OK dash status: 2 |
| 101 | |
| 102 | ### nounset with "$@" |
| 103 | set a b c |
| 104 | set -u # shouldn't touch argv |
| 105 | echo "$@" |
| 106 | # stdout: a b c |
| 107 | |
| 108 | ### set -u -- clears argv |
| 109 | set a b c |
| 110 | set -u -- # shouldn't touch argv |
| 111 | echo "$@" |
| 112 | # stdout: |
| 113 | |
| 114 | ### set -u -- x y z |
| 115 | set a b c |
| 116 | set -u -- x y z |
| 117 | echo "$@" |
| 118 | # stdout: x y z |
| 119 | |
| 120 | ### reset option with long flag |
| 121 | set -o errexit |
| 122 | set +o errexit |
| 123 | echo "[$unset]" |
| 124 | # stdout: [] |
| 125 | # status: 0 |
| 126 | |
| 127 | ### reset option with short flag |
| 128 | set -u |
| 129 | set +u |
| 130 | echo "[$unset]" |
| 131 | # stdout: [] |
| 132 | # status: 0 |
| 133 | |
| 134 | ### set -eu (flag parsing) |
| 135 | set -eu |
| 136 | echo "[$unset]" |
| 137 | echo status=$? |
| 138 | # stdout-json: "" |
| 139 | # status: 1 |
| 140 | # OK dash status: 2 |
| 141 | |
| 142 | ### -n for no execution (useful with --ast-output) |
| 143 | # NOTE: set +n doesn't work because nothing is executed! |
| 144 | echo 1 |
| 145 | set -n |
| 146 | echo 2 |
| 147 | set +n |
| 148 | echo 3 |
| 149 | # stdout-json: "1\n" |
| 150 | # status: 0 |
| 151 | |
| 152 | ### xtrace |
| 153 | echo 1 |
| 154 | set -o xtrace |
| 155 | echo 2 |
| 156 | # stdout-json: "1\n2\n" |
| 157 | # stderr: + echo 2 |
| 158 | |
| 159 | ### errexit aborts early |
| 160 | set -o errexit |
| 161 | false |
| 162 | echo done |
| 163 | # stdout-json: "" |
| 164 | # status: 1 |
| 165 | |
| 166 | ### errexit for nonexistent command |
| 167 | set -o errexit |
| 168 | nonexistent__ZZ |
| 169 | echo done |
| 170 | # stdout-json: "" |
| 171 | # status: 127 |
| 172 | |
| 173 | ### errexit aborts early on pipeline |
| 174 | set -o errexit |
| 175 | echo hi | grep nonexistent |
| 176 | echo two |
| 177 | # stdout-json: "" |
| 178 | # status: 1 |
| 179 | |
| 180 | ### errexit with { } |
| 181 | # This aborts because it's not part of an if statement. |
| 182 | set -o errexit |
| 183 | { echo one; false; echo two; } |
| 184 | # stdout: one |
| 185 | # status: 1 |
| 186 | |
| 187 | ### errexit with if and { } |
| 188 | set -o errexit |
| 189 | if { echo one; false; echo two; }; then |
| 190 | echo three |
| 191 | fi |
| 192 | echo four |
| 193 | # stdout-json: "one\ntwo\nthree\nfour\n" |
| 194 | # status: 0 |
| 195 | |
| 196 | ### errexit with || |
| 197 | set -o errexit |
| 198 | echo hi | grep nonexistent || echo ok |
| 199 | # stdout: ok |
| 200 | # status: 0 |
| 201 | |
| 202 | ### errexit with && |
| 203 | set -o errexit |
| 204 | echo ok && echo hi | grep nonexistent |
| 205 | # stdout: ok |
| 206 | # status: 1 |
| 207 | |
| 208 | ### errexit with ! |
| 209 | set -o errexit |
| 210 | echo one |
| 211 | ! true |
| 212 | echo two |
| 213 | ! false |
| 214 | echo three |
| 215 | # stdout-json: "one\ntwo\nthree\n" |
| 216 | # status: 0 |
| 217 | |
| 218 | ### errexit with ! and ; |
| 219 | # AST has extra Sentence nodes; there was a REGRESSION here. |
| 220 | set -o errexit; echo one; ! true; echo two; ! false; echo three |
| 221 | # stdout-json: "one\ntwo\nthree\n" |
| 222 | # status: 0 |
| 223 | |
| 224 | ### errexit with while/until |
| 225 | set -o errexit |
| 226 | while false; do |
| 227 | echo ok |
| 228 | done |
| 229 | until false; do |
| 230 | echo ok # do this once then exit loop |
| 231 | break |
| 232 | done |
| 233 | # stdout: ok |
| 234 | # status: 0 |
| 235 | |
| 236 | ### errexit with (( )) |
| 237 | # from http://mywiki.wooledge.org/BashFAQ/105, this changed between verisons. |
| 238 | set -o errexit |
| 239 | i=0 |
| 240 | (( i++ )) |
| 241 | echo done |
| 242 | # stdout-json: "" |
| 243 | # status: 1 |
| 244 | # N-I dash status: 127 |
| 245 | # N-I dash stdout-json: "" |
| 246 | |
| 247 | ### errexit with subshell |
| 248 | set -o errexit |
| 249 | ( echo one; false; echo two; ) |
| 250 | # stdout: one |
| 251 | # status: 1 |
| 252 | |
| 253 | ### errexit with command sub |
| 254 | # This is the bash-specific bug here: |
| 255 | # https://blogs.janestreet.com/when-bash-scripts-bite/ |
| 256 | set -o errexit |
| 257 | s=$(echo one; false; echo two;) |
| 258 | echo "$s" |
| 259 | # stdout-json: "" |
| 260 | # status: 1 |
| 261 | # BUG bash status: 0 |
| 262 | # BUG bash stdout-json: "one\ntwo\n" |
| 263 | |
| 264 | ### errexit with local |
| 265 | # I've run into this problem a lot. |
| 266 | # https://blogs.janestreet.com/when-bash-scripts-bite/ |
| 267 | set -o errexit |
| 268 | f() { |
| 269 | echo good |
| 270 | local x=$(echo bad; false) |
| 271 | echo $x |
| 272 | } |
| 273 | f |
| 274 | # stdout-json: "good\n" |
| 275 | # status: 1 |
| 276 | # BUG bash/dash/mksh stdout-json: "good\nbad\n" |
| 277 | # BUG bash/dash/mksh status: 0 |
| 278 | |
| 279 | ### setting errexit while it's being ignored |
| 280 | # ignored and then set again |
| 281 | set -o errexit |
| 282 | # osh aborts early here |
| 283 | if { echo 1; false; echo 2; set -o errexit; echo 3; false; echo 4; }; then |
| 284 | echo 5; |
| 285 | fi |
| 286 | echo 6 |
| 287 | false # this is the one that makes other shells fail |
| 288 | echo 7 |
| 289 | # status: 1 |
| 290 | # stdout-json: "1\n2\n" |
| 291 | # OK dash/bash/mksh stdout-json: "1\n2\n3\n4\n5\n6\n" |
| 292 | |
| 293 | ### setting errexit in a subshell works but doesn't affect parent shell |
| 294 | ( echo 1; false; echo 2; set -o errexit; echo 3; false; echo 4; ) |
| 295 | echo 5 |
| 296 | false |
| 297 | echo 6 |
| 298 | # stdout-json: "1\n2\n3\n5\n6\n" |
| 299 | # status: 0 |
| 300 | |
| 301 | ### setting errexit while it's being ignored in a subshell |
| 302 | set -o errexit |
| 303 | if ( echo 1; false; echo 2; set -o errexit; echo 3; false; echo 4 ); then |
| 304 | echo 5; |
| 305 | fi |
| 306 | echo 6 # This is executed because the subshell just returns false |
| 307 | false |
| 308 | echo 7 |
| 309 | # status: 1 |
| 310 | # stdout-json: "1\n2\n6\n" |
| 311 | # OK dash/bash/mksh stdout-json: "1\n2\n3\n4\n5\n6\n" |
| 312 | |
| 313 | ### errexit double quard |
| 314 | # OSH bug fix. ErrExit needs a counter, not a boolean. |
| 315 | set -o errexit |
| 316 | if { ! false; false; true; } then |
| 317 | echo true |
| 318 | fi |
| 319 | false |
| 320 | echo done |
| 321 | # status: 1 |
| 322 | # stdout-json: "true\n" |
| 323 | |
| 324 | ### pipefail |
| 325 | # NOTE: the sleeps are because osh can fail non-deterministically because of a |
| 326 | # bug. Same problem as PIPESTATUS. |
| 327 | { sleep 0.01; exit 9; } | { sleep 0.02; exit 2; } | { sleep 0.03; exit 0; } |
| 328 | echo $? |
| 329 | set -o pipefail |
| 330 | { sleep 0.01; exit 9; } | { sleep 0.02; exit 2; } | { sleep 0.03; exit 0; } |
| 331 | echo $? |
| 332 | # stdout-json: "0\n2\n" |
| 333 | # status: 0 |
| 334 | # N-I dash stdout-json: "0\n" |
| 335 | # N-I dash status: 2 |