| 1 | ## oils_failures_allowed: 2
|
| 2 | ## compare_shells: dash bash mksh zsh ash
|
| 3 |
|
| 4 |
|
| 5 | # echo, read, mapfile
|
| 6 | # TODO mapfile options: -c, -C, -u, etc.
|
| 7 |
|
| 8 | #### echo dashes
|
| 9 | echo -
|
| 10 | echo --
|
| 11 | echo ---
|
| 12 | ## stdout-json: "-\n--\n---\n"
|
| 13 | ## BUG zsh stdout-json: "\n--\n---\n"
|
| 14 |
|
| 15 | #### echo backslashes
|
| 16 | echo \\
|
| 17 | echo '\'
|
| 18 | echo '\\'
|
| 19 | echo "\\"
|
| 20 | ## STDOUT:
|
| 21 | \
|
| 22 | \
|
| 23 | \\
|
| 24 | \
|
| 25 | ## BUG dash/mksh/zsh STDOUT:
|
| 26 | \
|
| 27 | \
|
| 28 | \
|
| 29 | \
|
| 30 | ## END
|
| 31 |
|
| 32 | #### echo -e backslashes
|
| 33 | echo -e \\
|
| 34 | echo -e '\'
|
| 35 | echo -e '\\'
|
| 36 | echo -e "\\"
|
| 37 | echo
|
| 38 |
|
| 39 | # backslash at end of line
|
| 40 | echo -e '\
|
| 41 | line2'
|
| 42 | ## STDOUT:
|
| 43 | \
|
| 44 | \
|
| 45 | \
|
| 46 | \
|
| 47 |
|
| 48 | \
|
| 49 | line2
|
| 50 | ## N-I dash STDOUT:
|
| 51 | -e \
|
| 52 | -e \
|
| 53 | -e \
|
| 54 | -e \
|
| 55 |
|
| 56 | -e \
|
| 57 | line2
|
| 58 | ## END
|
| 59 |
|
| 60 | #### echo builtin should disallow typed args - literal
|
| 61 | echo (42)
|
| 62 | ## status: 2
|
| 63 | ## OK mksh/zsh status: 1
|
| 64 | ## STDOUT:
|
| 65 | ## END
|
| 66 |
|
| 67 | #### echo builtin should disallow typed args - variable
|
| 68 | var x = 43
|
| 69 | echo (x)
|
| 70 | ## status: 2
|
| 71 | ## OK mksh/zsh status: 1
|
| 72 | ## STDOUT:
|
| 73 | ## END
|
| 74 |
|
| 75 | #### echo -en
|
| 76 | echo -en 'abc\ndef\n'
|
| 77 | ## stdout-json: "abc\ndef\n"
|
| 78 | ## N-I dash stdout-json: "-en abc\ndef\n\n"
|
| 79 |
|
| 80 | #### echo -ez (invalid flag)
|
| 81 | # bash differs from the other three shells, but its behavior is possibly more
|
| 82 | # sensible, if you're going to ignore the error. It doesn't make sense for
|
| 83 | # the 'e' to mean 2 different things simultaneously: flag and literal to be
|
| 84 | # printed.
|
| 85 | echo -ez 'abc\n'
|
| 86 | ## stdout-json: "-ez abc\\n\n"
|
| 87 | ## OK dash/mksh/zsh stdout-json: "-ez abc\n\n"
|
| 88 |
|
| 89 | #### echo -e with embedded newline
|
| 90 | flags='-e'
|
| 91 | case $SH in dash) flags='' ;; esac
|
| 92 |
|
| 93 | echo $flags 'foo
|
| 94 | bar'
|
| 95 | ## STDOUT:
|
| 96 | foo
|
| 97 | bar
|
| 98 | ## END
|
| 99 |
|
| 100 | #### echo -e line continuation
|
| 101 | flags='-e'
|
| 102 | case $SH in dash) flags='' ;; esac
|
| 103 |
|
| 104 | echo $flags 'foo\
|
| 105 | bar'
|
| 106 | ## STDOUT:
|
| 107 | foo\
|
| 108 | bar
|
| 109 | ## END
|
| 110 |
|
| 111 | #### echo -e with C escapes
|
| 112 | # https://www.gnu.org/software/bash/manual/bashref.html#Bourne-Shell-Builtins
|
| 113 | # not sure why \c is like NUL?
|
| 114 | # zsh doesn't allow \E for some reason.
|
| 115 | echo -e '\a\b\d\e\f'
|
| 116 | ## stdout-json: "\u0007\u0008\\d\u001b\u000c\n"
|
| 117 | ## N-I dash stdout-json: "-e \u0007\u0008\\d\\e\u000c\n"
|
| 118 |
|
| 119 | #### echo -e with whitespace C escapes
|
| 120 | echo -e '\n\r\t\v'
|
| 121 | ## stdout-json: "\n\r\t\u000b\n"
|
| 122 | ## N-I dash stdout-json: "-e \n\r\t\u000b\n"
|
| 123 |
|
| 124 | #### \0
|
| 125 | echo -e 'ab\0cd'
|
| 126 | ## stdout-json: "ab\u0000cd\n"
|
| 127 | ## N-I dash stdout-json: "-e ab\u0000cd\n"
|
| 128 |
|
| 129 | #### \c stops processing input
|
| 130 | flags='-e'
|
| 131 | case $SH in dash) flags='' ;; esac
|
| 132 |
|
| 133 | echo $flags xy 'ab\cde' 'zzz'
|
| 134 | ## stdout-json: "xy ab"
|
| 135 | ## N-I mksh stdout-json: "xy abde zzz"
|
| 136 |
|
| 137 | #### echo -e with hex escape
|
| 138 | echo -e 'abcd\x65f'
|
| 139 | ## stdout-json: "abcdef\n"
|
| 140 | ## N-I dash stdout-json: "-e abcd\\x65f\n"
|
| 141 |
|
| 142 | #### echo -e with octal escape
|
| 143 | flags='-e'
|
| 144 | case $SH in dash) flags='' ;; esac
|
| 145 |
|
| 146 | echo $flags 'abcd\044e'
|
| 147 | ## stdout-json: "abcd$e\n"
|
| 148 |
|
| 149 | #### echo -e with 4 digit unicode escape
|
| 150 | flags='-e'
|
| 151 | case $SH in dash) flags='' ;; esac
|
| 152 |
|
| 153 | echo $flags 'abcd\u0065f'
|
| 154 | ## STDOUT:
|
| 155 | abcdef
|
| 156 | ## END
|
| 157 | ## N-I dash/ash stdout-json: "abcd\\u0065f\n"
|
| 158 |
|
| 159 | #### echo -e with 8 digit unicode escape
|
| 160 | flags='-e'
|
| 161 | case $SH in dash) flags='' ;; esac
|
| 162 |
|
| 163 | echo $flags 'abcd\U00000065f'
|
| 164 | ## STDOUT:
|
| 165 | abcdef
|
| 166 | ## END
|
| 167 | ## N-I dash/ash stdout-json: "abcd\\U00000065f\n"
|
| 168 |
|
| 169 | #### \0377 is the highest octal byte
|
| 170 | echo -en '\03777' | od -A n -t x1 | sed 's/ \+/ /g'
|
| 171 | ## stdout-json: " ff 37\n"
|
| 172 | ## N-I dash stdout-json: " 2d 65 6e 20 ff 37 0a\n"
|
| 173 |
|
| 174 | #### \0400 is one more than the highest octal byte
|
| 175 | # It is 256 % 256 which gets interpreted as a NUL byte.
|
| 176 | echo -en '\04000' | od -A n -t x1 | sed 's/ \+/ /g'
|
| 177 | ## stdout-json: " 00 30\n"
|
| 178 | ## BUG ash stdout-json: " 20 30 30\n"
|
| 179 | ## N-I dash stdout-json: " 2d 65 6e 20 00 30 0a\n"
|
| 180 |
|
| 181 | #### \0777 is out of range
|
| 182 | flags='-en'
|
| 183 | case $SH in dash) flags='-n' ;; esac
|
| 184 |
|
| 185 | echo $flags '\0777' | od -A n -t x1 | sed 's/ \+/ /g'
|
| 186 | ## stdout-json: " ff\n"
|
| 187 | ## BUG mksh stdout-json: " c3 bf\n"
|
| 188 | ## BUG ash stdout-json: " 3f 37\n"
|
| 189 |
|
| 190 | #### incomplete hex escape
|
| 191 | echo -en 'abcd\x6' | od -A n -c | sed 's/ \+/ /g'
|
| 192 | ## stdout-json: " a b c d 006\n"
|
| 193 | ## N-I dash stdout-json: " - e n a b c d \\ x 6 \\n\n"
|
| 194 |
|
| 195 | #### \x
|
| 196 | # I consider mksh and zsh a bug because \x is not an escape
|
| 197 | echo -e '\x' '\xg' | od -A n -c | sed 's/ \+/ /g'
|
| 198 | ## stdout-json: " \\ x \\ x g \\n\n"
|
| 199 | ## N-I dash stdout-json: " - e \\ x \\ x g \\n\n"
|
| 200 | ## BUG mksh/zsh stdout-json: " \\0 \\0 g \\n\n"
|
| 201 |
|
| 202 | #### incomplete octal escape
|
| 203 | flags='-en'
|
| 204 | case $SH in dash) flags='-n' ;; esac
|
| 205 |
|
| 206 | echo $flags 'abcd\04' | od -A n -c | sed 's/ \+/ /g'
|
| 207 | ## stdout-json: " a b c d 004\n"
|
| 208 |
|
| 209 | #### incomplete unicode escape
|
| 210 | echo -en 'abcd\u006' | od -A n -c | sed 's/ \+/ /g'
|
| 211 | ## stdout-json: " a b c d 006\n"
|
| 212 | ## N-I dash stdout-json: " - e n a b c d \\ u 0 0 6 \\n\n"
|
| 213 | ## BUG ash stdout-json: " a b c d \\ u 0 0 6\n"
|
| 214 |
|
| 215 | #### \u6
|
| 216 | flags='-en'
|
| 217 | case $SH in dash) flags='-n' ;; esac
|
| 218 |
|
| 219 | echo $flags '\u6' | od -A n -c | sed 's/ \+/ /g'
|
| 220 | ## stdout-json: " 006\n"
|
| 221 | ## N-I dash/ash stdout-json: " \\ u 6\n"
|
| 222 |
|
| 223 | #### \0 \1 \8
|
| 224 | # \0 is special, but \1 isn't in bash
|
| 225 | # \1 is special in dash! geez
|
| 226 | flags='-en'
|
| 227 | case $SH in dash) flags='-n' ;; esac
|
| 228 |
|
| 229 | echo $flags '\0' '\1' '\8' | od -A n -c | sed 's/ \+/ /g'
|
| 230 | ## stdout-json: " \\0 \\ 1 \\ 8\n"
|
| 231 | ## BUG dash/ash stdout-json: " \\0 001 \\ 8\n"
|
| 232 |
|
| 233 | #### Read builtin
|
| 234 | # NOTE: there are TABS below
|
| 235 | read x <<EOF
|
| 236 | A B C D E
|
| 237 | FG
|
| 238 | EOF
|
| 239 | echo "[$x]"
|
| 240 | ## stdout: [A B C D E]
|
| 241 | ## status: 0
|
| 242 |
|
| 243 | #### Read from empty file
|
| 244 | echo -n '' > $TMP/empty.txt
|
| 245 | read x < $TMP/empty.txt
|
| 246 | argv.py "status=$?" "$x"
|
| 247 |
|
| 248 | # No variable name, behaves the same
|
| 249 | read < $TMP/empty.txt
|
| 250 | argv.py "status=$?" "$REPLY"
|
| 251 |
|
| 252 | ## STDOUT:
|
| 253 | ['status=1', '']
|
| 254 | ['status=1', '']
|
| 255 | ## END
|
| 256 | ## OK dash STDOUT:
|
| 257 | ['status=1', '']
|
| 258 | ['status=2', '']
|
| 259 | ## END
|
| 260 | ## status: 0
|
| 261 |
|
| 262 | #### read /dev/null
|
| 263 | read -n 1 </dev/null
|
| 264 | echo $?
|
| 265 | ## STDOUT:
|
| 266 | 1
|
| 267 | ## END
|
| 268 | ## OK dash stdout: 2
|
| 269 |
|
| 270 | #### read with zero args
|
| 271 | echo | read
|
| 272 | echo status=$?
|
| 273 | ## STDOUT:
|
| 274 | status=0
|
| 275 | ## END
|
| 276 | ## BUG dash STDOUT:
|
| 277 | status=2
|
| 278 | ## END
|
| 279 |
|
| 280 | #### Read builtin with no newline.
|
| 281 | # This is odd because the variable is populated successfully. OSH/Oil might
|
| 282 | # need a separate put reading feature that doesn't use IFS.
|
| 283 | echo -n ZZZ | { read x; echo $?; echo $x; }
|
| 284 | ## stdout-json: "1\nZZZ\n"
|
| 285 | ## status: 0
|
| 286 |
|
| 287 | #### Read builtin with multiple variables
|
| 288 | # NOTE: there are TABS below
|
| 289 | read x y z <<EOF
|
| 290 | A B C D E
|
| 291 | FG
|
| 292 | EOF
|
| 293 | echo "[$x/$y/$z]"
|
| 294 | ## stdout: [A/B/C D E]
|
| 295 | ## status: 0
|
| 296 |
|
| 297 | #### Read builtin with not enough variables
|
| 298 | set -o errexit
|
| 299 | set -o nounset # hm this doesn't change it
|
| 300 | read x y z <<EOF
|
| 301 | A B
|
| 302 | EOF
|
| 303 | echo /$x/$y/$z/
|
| 304 | ## stdout: /A/B//
|
| 305 | ## status: 0
|
| 306 |
|
| 307 | #### Read -n (with $REPLY)
|
| 308 | echo 12345 > $TMP/readn.txt
|
| 309 | read -n 4 x < $TMP/readn.txt
|
| 310 | read -n 2 < $TMP/readn.txt # Do it again with no variable
|
| 311 | argv.py $x $REPLY
|
| 312 | ## stdout: ['1234', '12']
|
| 313 | ## N-I dash/zsh stdout: []
|
| 314 |
|
| 315 | #### IFS= read -n (OSH regression: value saved in tempenv)
|
| 316 | echo XYZ > "$TMP/readn.txt"
|
| 317 | IFS= TMOUT= read -n 1 char < "$TMP/readn.txt"
|
| 318 | argv.py "$char"
|
| 319 | ## stdout: ['X']
|
| 320 | ## N-I dash/zsh stdout: ['']
|
| 321 |
|
| 322 | #### read -n with invalid arg
|
| 323 | read -n not_a_number
|
| 324 | echo status=$?
|
| 325 | ## stdout: status=2
|
| 326 | ## OK bash stdout: status=1
|
| 327 | ## N-I zsh stdout-json: ""
|
| 328 |
|
| 329 | #### read -n from pipe
|
| 330 | case $SH in (dash|ash|zsh) exit ;; esac
|
| 331 |
|
| 332 | echo abcxyz | { read -n 3; echo reply=$REPLY; }
|
| 333 | ## status: 0
|
| 334 | ## stdout: reply=abc
|
| 335 | ## N-I dash/ash/zsh stdout-json: ""
|
| 336 |
|
| 337 | # zsh appears to hang with -k
|
| 338 | ## N-I zsh stdout-json: ""
|
| 339 |
|
| 340 | #### Read uses $REPLY (without -n)
|
| 341 | echo 123 > $TMP/readreply.txt
|
| 342 | read < $TMP/readreply.txt
|
| 343 | echo $REPLY
|
| 344 | ## stdout: 123
|
| 345 | ## N-I dash stdout:
|
| 346 |
|
| 347 | #### read -n vs. -N
|
| 348 | # dash, ash and zsh do not implement read -N
|
| 349 | # mksh treats -N exactly the same as -n
|
| 350 | case $SH in (dash|ash|zsh) exit ;; esac
|
| 351 |
|
| 352 | # bash docs: https://www.gnu.org/software/bash/manual/html_node/Bash-Builtins.html
|
| 353 |
|
| 354 | echo 'a b c' > $TMP/readn.txt
|
| 355 |
|
| 356 | echo 'read -n'
|
| 357 | read -n 5 A B C < $TMP/readn.txt; echo "'$A' '$B' '$C'"
|
| 358 | read -n 4 A B C < $TMP/readn.txt; echo "'$A' '$B' '$C'"
|
| 359 | echo
|
| 360 |
|
| 361 | echo 'read -N'
|
| 362 | read -N 5 A B C < $TMP/readn.txt; echo "'$A' '$B' '$C'"
|
| 363 | read -N 4 A B C < $TMP/readn.txt; echo "'$A' '$B' '$C'"
|
| 364 | ## STDOUT:
|
| 365 | read -n
|
| 366 | 'a' 'b' 'c'
|
| 367 | 'a' 'b' ''
|
| 368 |
|
| 369 | read -N
|
| 370 | 'a b c' '' ''
|
| 371 | 'a b ' '' ''
|
| 372 | ## END
|
| 373 | ## N-I dash/ash/zsh stdout-json: ""
|
| 374 | ## BUG mksh STDOUT:
|
| 375 | read -n
|
| 376 | 'a' 'b' 'c'
|
| 377 | 'a' 'b' ''
|
| 378 |
|
| 379 | read -N
|
| 380 | 'a' 'b' 'c'
|
| 381 | 'a' 'b' ''
|
| 382 | ## END
|
| 383 |
|
| 384 | #### read -N ignores delimiters
|
| 385 | case $SH in (dash|ash|zsh) exit ;; esac
|
| 386 |
|
| 387 | echo $'a\nb\nc' > $TMP/read-lines.txt
|
| 388 |
|
| 389 | read -N 3 out < $TMP/read-lines.txt
|
| 390 | echo "$out"
|
| 391 | ## STDOUT:
|
| 392 | a
|
| 393 | b
|
| 394 | ## END
|
| 395 | ## N-I dash/ash/zsh stdout-json: ""
|
| 396 |
|
| 397 | #### read will unset extranous vars
|
| 398 |
|
| 399 | echo 'a b' > $TMP/read-few.txt
|
| 400 |
|
| 401 | c='some value'
|
| 402 | read a b c < $TMP/read-few.txt
|
| 403 | echo "'$a' '$b' '$c'"
|
| 404 |
|
| 405 | case $SH in (dash) exit ;; esac # dash does not implement -n
|
| 406 |
|
| 407 | c='some value'
|
| 408 | read -n 3 a b c < $TMP/read-few.txt
|
| 409 | echo "'$a' '$b' '$c'"
|
| 410 | ## STDOUT:
|
| 411 | 'a' 'b' ''
|
| 412 | 'a' 'b' ''
|
| 413 | ## END
|
| 414 | ## N-I dash STDOUT:
|
| 415 | 'a' 'b' ''
|
| 416 | ## END
|
| 417 | ## BUG zsh STDOUT:
|
| 418 | 'a' 'b' ''
|
| 419 | 'b' '' ''
|
| 420 | ## END
|
| 421 |
|
| 422 | #### read -r ignores backslashes
|
| 423 | echo 'one\ two' > $TMP/readr.txt
|
| 424 | read escaped < $TMP/readr.txt
|
| 425 | read -r raw < $TMP/readr.txt
|
| 426 | argv.py "$escaped" "$raw"
|
| 427 | ## stdout: ['one two', 'one\\ two']
|
| 428 |
|
| 429 | #### read -r with other backslash escapes
|
| 430 | echo 'one\ two\x65three' > $TMP/readr.txt
|
| 431 | read escaped < $TMP/readr.txt
|
| 432 | read -r raw < $TMP/readr.txt
|
| 433 | argv.py "$escaped" "$raw"
|
| 434 | # mksh respects the hex escapes here, but other shells don't!
|
| 435 | ## stdout: ['one twox65three', 'one\\ two\\x65three']
|
| 436 | ## BUG mksh/zsh stdout: ['one twoethree', 'one\\ twoethree']
|
| 437 |
|
| 438 | #### read with line continuation reads multiple physical lines
|
| 439 | # NOTE: osh failing because of file descriptor issue. stdin has to be closed!
|
| 440 | tmp=$TMP/$(basename $SH)-readr.txt
|
| 441 | echo -e 'one\\\ntwo\n' > $tmp
|
| 442 | read escaped < $tmp
|
| 443 | read -r raw < $tmp
|
| 444 | argv.py "$escaped" "$raw"
|
| 445 | ## stdout: ['onetwo', 'one\\']
|
| 446 | ## N-I dash stdout: ['-e onetwo', '-e one\\']
|
| 447 |
|
| 448 | #### read multiple vars spanning many lines
|
| 449 | read x y << 'EOF'
|
| 450 | one-\
|
| 451 | two three-\
|
| 452 | four five-\
|
| 453 | six
|
| 454 | EOF
|
| 455 | argv.py "$x" "$y" "$z"
|
| 456 | ## stdout: ['one-two', 'three-four five-six', '']
|
| 457 |
|
| 458 | #### read -r with \n
|
| 459 | echo '\nline' > $TMP/readr.txt
|
| 460 | read escaped < $TMP/readr.txt
|
| 461 | read -r raw < $TMP/readr.txt
|
| 462 | argv.py "$escaped" "$raw"
|
| 463 | # dash/mksh/zsh are bugs because at least the raw mode should let you read a
|
| 464 | # literal \n.
|
| 465 | ## stdout: ['nline', '\\nline']
|
| 466 | ## BUG dash/mksh/zsh stdout: ['', '']
|
| 467 |
|
| 468 | #### read -s from pipe, not a terminal
|
| 469 | case $SH in (dash|zsh) exit ;; esac
|
| 470 |
|
| 471 | # It's hard to really test this because it requires a terminal. We hit a
|
| 472 | # different code path when reading through a pipe. There can be bugs there
|
| 473 | # too!
|
| 474 |
|
| 475 | echo foo | { read -s; echo $REPLY; }
|
| 476 | echo bar | { read -n 2 -s; echo $REPLY; }
|
| 477 |
|
| 478 | # Hm no exit 1 here? Weird
|
| 479 | echo b | { read -n 2 -s; echo $?; echo $REPLY; }
|
| 480 | ## STDOUT:
|
| 481 | foo
|
| 482 | ba
|
| 483 | 0
|
| 484 | b
|
| 485 | ## END
|
| 486 | ## N-I dash/zsh stdout-json: ""
|
| 487 |
|
| 488 | #### Read with IFS=$'\n'
|
| 489 | # The leading spaces are stripped if they appear in IFS.
|
| 490 | IFS=$(echo -e '\n')
|
| 491 | read var <<EOF
|
| 492 | a b c
|
| 493 | d e f
|
| 494 | EOF
|
| 495 | echo "[$var]"
|
| 496 | ## stdout: [ a b c]
|
| 497 | ## N-I dash stdout: [a b c]
|
| 498 |
|
| 499 | #### Read multiple lines with IFS=:
|
| 500 | # The leading spaces are stripped if they appear in IFS.
|
| 501 | # IFS chars are escaped with :.
|
| 502 | tmp=$TMP/$(basename $SH)-read-ifs.txt
|
| 503 | IFS=:
|
| 504 | cat >$tmp <<'EOF'
|
| 505 | \\a :b\: c:d\
|
| 506 | e
|
| 507 | EOF
|
| 508 | read a b c d < $tmp
|
| 509 | # Use printf because echo in dash/mksh interprets escapes, while it doesn't in
|
| 510 | # bash.
|
| 511 | printf "%s\n" "[$a|$b|$c|$d]"
|
| 512 | ## stdout: [ \a |b: c|d e|]
|
| 513 |
|
| 514 | #### Read with IFS=''
|
| 515 | IFS=''
|
| 516 | read x y <<EOF
|
| 517 | a b c d
|
| 518 | EOF
|
| 519 | echo "[$x|$y]"
|
| 520 | ## stdout: [ a b c d|]
|
| 521 |
|
| 522 | #### Read should not respect C escapes.
|
| 523 | # bash doesn't respect these, but other shells do. Gah! I think bash
|
| 524 | # behavior makes more sense. It only escapes IFS.
|
| 525 | echo '\a \b \c \d \e \f \g \h \x65 \145 \i' > $TMP/read-c.txt
|
| 526 | read line < $TMP/read-c.txt
|
| 527 | echo $line
|
| 528 | ## stdout-json: "a b c d e f g h x65 145 i\n"
|
| 529 | ## BUG ash stdout-json: "abcdefghx65 145 i\n"
|
| 530 | ## BUG dash/zsh stdout-json: "\u0007 \u0008\n"
|
| 531 | ## BUG mksh stdout-json: "\u0007 \u0008 d \u001b \u000c g h e 145 i\n"
|
| 532 |
|
| 533 | #### Read builtin uses dynamic scope
|
| 534 | f() {
|
| 535 | read head << EOF
|
| 536 | ref: refs/heads/dev/andy
|
| 537 | EOF
|
| 538 | }
|
| 539 | f
|
| 540 | echo $head
|
| 541 | ## STDOUT:
|
| 542 | ref: refs/heads/dev/andy
|
| 543 | ## END
|
| 544 |
|
| 545 | #### read -a reads into array
|
| 546 |
|
| 547 | # read -a is used in bash-completion
|
| 548 | # none of these shells implement it
|
| 549 | case $SH in
|
| 550 | *mksh|*dash|*zsh|*/ash)
|
| 551 | exit 2;
|
| 552 | ;;
|
| 553 | esac
|
| 554 |
|
| 555 | read -a myarray <<'EOF'
|
| 556 | a b c\ d
|
| 557 | EOF
|
| 558 | argv.py "${myarray[@]}"
|
| 559 |
|
| 560 | # arguments are ignored here
|
| 561 | read -r -a array2 extra arguments <<'EOF'
|
| 562 | a b c\ d
|
| 563 | EOF
|
| 564 | argv.py "${array2[@]}"
|
| 565 | argv.py "${extra[@]}"
|
| 566 | argv.py "${arguments[@]}"
|
| 567 | ## status: 0
|
| 568 | ## STDOUT:
|
| 569 | ['a', 'b', 'c d']
|
| 570 | ['a', 'b', 'c\\', 'd']
|
| 571 | []
|
| 572 | []
|
| 573 | ## END
|
| 574 | ## N-I dash/mksh/zsh/ash status: 2
|
| 575 | ## N-I dash/mksh/zsh/ash stdout-json: ""
|
| 576 |
|
| 577 | #### read -d : (colon-separated records)
|
| 578 | printf a,b,c:d,e,f:g,h,i | {
|
| 579 | IFS=,
|
| 580 | read -d : v1
|
| 581 | echo "v1=$v1"
|
| 582 | read -d : v1 v2
|
| 583 | echo "v1=$v1 v2=$v2"
|
| 584 | read -d : v1 v2 v3
|
| 585 | echo "v1=$v1 v2=$v2 v3=$v3"
|
| 586 | }
|
| 587 | ## STDOUT:
|
| 588 | v1=a,b,c
|
| 589 | v1=d v2=e,f
|
| 590 | v1=g v2=h v3=i
|
| 591 | ## END
|
| 592 | ## N-I dash STDOUT:
|
| 593 | v1=
|
| 594 | v1= v2=
|
| 595 | v1= v2= v3=
|
| 596 | ## END
|
| 597 |
|
| 598 | #### read -d '' (null-separated records)
|
| 599 | printf 'a,b,c\0d,e,f\0g,h,i' | {
|
| 600 | IFS=,
|
| 601 | read -d '' v1
|
| 602 | echo "v1=$v1"
|
| 603 | read -d '' v1 v2
|
| 604 | echo "v1=$v1 v2=$v2"
|
| 605 | read -d '' v1 v2 v3
|
| 606 | echo "v1=$v1 v2=$v2 v3=$v3"
|
| 607 | }
|
| 608 | ## STDOUT:
|
| 609 | v1=a,b,c
|
| 610 | v1=d v2=e,f
|
| 611 | v1=g v2=h v3=i
|
| 612 | ## END
|
| 613 | ## N-I dash STDOUT:
|
| 614 | v1=
|
| 615 | v1= v2=
|
| 616 | v1= v2= v3=
|
| 617 | ## END
|
| 618 |
|
| 619 | #### read -rd
|
| 620 | read -rd '' var <<EOF
|
| 621 | foo
|
| 622 | bar
|
| 623 | EOF
|
| 624 | echo "$var"
|
| 625 | ## STDOUT:
|
| 626 | foo
|
| 627 | bar
|
| 628 | ## END
|
| 629 | ## N-I dash stdout-json: "\n"
|
| 630 |
|
| 631 | #### read -d when there's no delimiter
|
| 632 | { read -d : part
|
| 633 | echo $part $?
|
| 634 | read -d : part
|
| 635 | echo $part $?
|
| 636 | } <<EOF
|
| 637 | foo:bar
|
| 638 | EOF
|
| 639 | ## STDOUT:
|
| 640 | foo 0
|
| 641 | bar 1
|
| 642 | ## END
|
| 643 | ## N-I dash STDOUT:
|
| 644 | 2
|
| 645 | 2
|
| 646 | ## END
|
| 647 |
|
| 648 | #### read -t 0 tests if input is available
|
| 649 | case $SH in (dash|zsh|mksh) exit ;; esac
|
| 650 |
|
| 651 | # is there input available?
|
| 652 | read -t 0 < /dev/null
|
| 653 | echo $?
|
| 654 |
|
| 655 | # floating point
|
| 656 | read -t 0.0 < /dev/null
|
| 657 | echo $?
|
| 658 |
|
| 659 | # floating point
|
| 660 | echo foo | { read -t 0; echo reply=$REPLY; }
|
| 661 | echo $?
|
| 662 |
|
| 663 | ## STDOUT:
|
| 664 | 0
|
| 665 | 0
|
| 666 | reply=
|
| 667 | 0
|
| 668 | ## END
|
| 669 | ## N-I dash/zsh/mksh stdout-json: ""
|
| 670 |
|
| 671 | #### read -t 0.5
|
| 672 | case $SH in (dash) exit ;; esac
|
| 673 |
|
| 674 | read -t 0.5 < /dev/null
|
| 675 | echo $?
|
| 676 |
|
| 677 | ## STDOUT:
|
| 678 | 1
|
| 679 | ## END
|
| 680 | ## BUG zsh/mksh STDOUT:
|
| 681 | 1
|
| 682 | ## END
|
| 683 | ## N-I dash stdout-json: ""
|
| 684 |
|
| 685 | #### read -t -0.5 is invalid
|
| 686 | # bash appears to just take the absolute value?
|
| 687 |
|
| 688 | read -t -0.5 < /dev/null
|
| 689 | echo $?
|
| 690 |
|
| 691 | ## STDOUT:
|
| 692 | 2
|
| 693 | ## END
|
| 694 | ## BUG bash STDOUT:
|
| 695 | 1
|
| 696 | ## END
|
| 697 | ## BUG zsh stdout-json: ""
|
| 698 | ## BUG zsh status: 1
|
| 699 |
|
| 700 | #### read -u
|
| 701 | case $SH in (dash|mksh) exit ;; esac
|
| 702 |
|
| 703 | # file descriptor
|
| 704 | read -u 3 3<<EOF
|
| 705 | hi
|
| 706 | EOF
|
| 707 | echo reply=$REPLY
|
| 708 | ## STDOUT:
|
| 709 | reply=hi
|
| 710 | ## END
|
| 711 | ## N-I dash/mksh stdout-json: ""
|
| 712 |
|
| 713 | #### read -u syntax error
|
| 714 | read -u -3
|
| 715 | echo status=$?
|
| 716 | ## STDOUT:
|
| 717 | status=2
|
| 718 | ## END
|
| 719 | ## OK bash/zsh STDOUT:
|
| 720 | status=1
|
| 721 | ## END
|
| 722 |
|
| 723 | #### read -N doesn't respect delimiter, while read -n does
|
| 724 | case $SH in (dash|zsh|ash) exit ;; esac
|
| 725 |
|
| 726 | echo foobar | { read -n 5 -d b; echo $REPLY; }
|
| 727 | echo foobar | { read -N 5 -d b; echo $REPLY; }
|
| 728 | ## STDOUT:
|
| 729 | foo
|
| 730 | fooba
|
| 731 | ## END
|
| 732 | ## OK mksh STDOUT:
|
| 733 | fooba
|
| 734 | fooba
|
| 735 | ## END
|
| 736 | ## N-I dash/zsh/ash stdout-json: ""
|
| 737 |
|
| 738 | #### read -p (not fully tested)
|
| 739 |
|
| 740 | # hm DISABLED if we're not going to the terminal
|
| 741 | # so we're only testing that it accepts the flag here
|
| 742 |
|
| 743 | case $SH in (dash|mksh|zsh) exit ;; esac
|
| 744 |
|
| 745 | echo hi | { read -p 'P'; echo $REPLY; }
|
| 746 | echo hi | { read -p 'P' -n 1; echo $REPLY; }
|
| 747 | ## STDOUT:
|
| 748 | hi
|
| 749 | h
|
| 750 | ## END
|
| 751 | ## stderr-json: ""
|
| 752 | ## N-I dash/mksh/zsh stdout-json: ""
|
| 753 |
|
| 754 | #### read usage
|
| 755 | read -n -1
|
| 756 | echo status=$?
|
| 757 | ## STDOUT:
|
| 758 | status=2
|
| 759 | ## END
|
| 760 | ## OK bash stdout: status=1
|
| 761 | ## BUG mksh stdout-json: ""
|
| 762 | # zsh gives a fatal error? seems inconsistent
|
| 763 | ## BUG zsh stdout-json: ""
|
| 764 | ## BUG zsh status: 1
|
| 765 |
|
| 766 | #### read with smooshed args
|
| 767 | echo hi | { read -rn1 var; echo var=$var; }
|
| 768 | ## STDOUT:
|
| 769 | var=h
|
| 770 | ## END
|
| 771 | ## N-I dash/zsh STDOUT:
|
| 772 | var=
|
| 773 | ## END
|
| 774 |
|
| 775 | #### read -r -d '' for NUL strings, e.g. find -print0
|
| 776 |
|
| 777 |
|
| 778 | case $SH in (dash|zsh|mksh) exit ;; esac # NOT IMPLEMENTED
|
| 779 |
|
| 780 | mkdir -p read0
|
| 781 | cd read0
|
| 782 | rm -f *
|
| 783 |
|
| 784 | touch a\\b\\c\\d # -r is necessary!
|
| 785 |
|
| 786 | find . -type f -a -print0 | { read -r -d ''; echo "[$REPLY]"; }
|
| 787 |
|
| 788 | ## STDOUT:
|
| 789 | [./a\b\c\d]
|
| 790 | ## END
|
| 791 | ## N-I dash/zsh/mksh STDOUT:
|
| 792 | ## END
|
| 793 |
|
| 794 |
|
| 795 | #### redirection from directory is non-fatal error)
|
| 796 |
|
| 797 | # This tickles an infinite loop bug in our version of mksh! TODO: upgrade the
|
| 798 | # version and enable this
|
| 799 | case $SH in (mksh) return ;; esac
|
| 800 |
|
| 801 | cd $TMP
|
| 802 | mkdir -p dir
|
| 803 | read x < ./dir
|
| 804 | echo status=$?
|
| 805 |
|
| 806 | ## STDOUT:
|
| 807 | status=1
|
| 808 | ## END
|
| 809 | # OK mksh stdout: status=2
|
| 810 | ## OK mksh stdout-json: ""
|
| 811 |
|
| 812 | #### read -n from directory
|
| 813 |
|
| 814 | case $SH in (dash|ash) return ;; esac # not implemented
|
| 815 |
|
| 816 | # same hanging bug
|
| 817 | case $SH in (mksh) return ;; esac
|
| 818 |
|
| 819 | mkdir -p dir
|
| 820 | read -n 3 x < ./dir
|
| 821 | echo status=$?
|
| 822 | ## STDOUT:
|
| 823 | status=1
|
| 824 | ## END
|
| 825 | ## OK mksh stdout-json: ""
|
| 826 | ## N-I dash/ash stdout-json: ""
|
| 827 |
|
| 828 | #### mapfile from directory (bash doesn't handle errors)
|
| 829 | case $SH in (dash|ash|mksh|zsh) return ;; esac # not implemented
|
| 830 |
|
| 831 | mkdir -p dir
|
| 832 | mapfile $x < ./dir
|
| 833 | echo status=$?
|
| 834 |
|
| 835 | ## STDOUT:
|
| 836 | status=1
|
| 837 | ## END
|
| 838 | ## BUG bash STDOUT:
|
| 839 | status=0
|
| 840 | ## END
|
| 841 | ## N-I dash/ash/mksh/zsh stdout-json: ""
|
| 842 |
|
| 843 | #### Redirect to directory
|
| 844 | mkdir -p dir
|
| 845 |
|
| 846 | echo foo > ./dir
|
| 847 | echo status=$?
|
| 848 | printf foo > ./dir
|
| 849 | echo status=$?
|
| 850 |
|
| 851 | ## STDOUT:
|
| 852 | status=1
|
| 853 | status=1
|
| 854 | ## END
|
| 855 | ## OK dash STDOUT:
|
| 856 | status=2
|
| 857 | status=2
|
| 858 | ## END
|
| 859 |
|