| 1 | #
|
| 2 | # Cases from
|
| 3 | # http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html
|
| 4 |
|
| 5 | # My tests
|
| 6 |
|
| 7 | #### Empty for loop is allowed
|
| 8 | set -- a b
|
| 9 | for x in; do
|
| 10 | echo hi
|
| 11 | echo $x
|
| 12 | done
|
| 13 | ## stdout-json: ""
|
| 14 |
|
| 15 | #### Empty for loop without in. Do can be on the same line I guess.
|
| 16 | set -- a b
|
| 17 | for x do
|
| 18 | echo hi
|
| 19 | echo $x
|
| 20 | done
|
| 21 | ## STDOUT:
|
| 22 | hi
|
| 23 | a
|
| 24 | hi
|
| 25 | b
|
| 26 | ## END
|
| 27 |
|
| 28 | #### Empty case statement
|
| 29 | case foo in
|
| 30 | esac
|
| 31 | ## stdout-json: ""
|
| 32 |
|
| 33 | #### Last case without ;;
|
| 34 | foo=a
|
| 35 | case $foo in
|
| 36 | a) echo A ;;
|
| 37 | b) echo B
|
| 38 | esac
|
| 39 | ## stdout: A
|
| 40 |
|
| 41 | #### Only case without ;;
|
| 42 | foo=a
|
| 43 | case $foo in
|
| 44 | a) echo A
|
| 45 | esac
|
| 46 | ## stdout: A
|
| 47 |
|
| 48 | #### Case with optional (
|
| 49 | foo=a
|
| 50 | case $foo in
|
| 51 | (a) echo A ;;
|
| 52 | (b) echo B
|
| 53 | esac
|
| 54 | ## stdout: A
|
| 55 |
|
| 56 | #### Empty action for case is syntax error
|
| 57 | # POSIX grammar seems to allow this, but bash and dash don't. Need ;;
|
| 58 | foo=a
|
| 59 | case $foo in
|
| 60 | a)
|
| 61 | b)
|
| 62 | echo A ;;
|
| 63 | d)
|
| 64 | esac
|
| 65 | ## status: 2
|
| 66 | ## OK mksh status: 1
|
| 67 |
|
| 68 | #### Empty action is allowed for last case
|
| 69 | foo=b
|
| 70 | case $foo in
|
| 71 | a) echo A ;;
|
| 72 | b)
|
| 73 | esac
|
| 74 | ## stdout-json: ""
|
| 75 |
|
| 76 | #### Case with | pattern
|
| 77 | foo=a
|
| 78 | case $foo in
|
| 79 | a|b) echo A ;;
|
| 80 | c)
|
| 81 | esac
|
| 82 | ## stdout: A
|
| 83 |
|
| 84 |
|
| 85 | #### Bare semi-colon not allowed
|
| 86 | # This is disallowed by the grammar; bash and dash don't accept it.
|
| 87 | ;
|
| 88 | ## status: 2
|
| 89 | ## OK mksh status: 1
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 | #
|
| 94 | # Explicit tests
|
| 95 | #
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 | #### Command substitution in default
|
| 100 | echo ${x:-$(ls -d /bin)}
|
| 101 | ## stdout: /bin
|
| 102 |
|
| 103 |
|
| 104 | #### Arithmetic expansion
|
| 105 | x=3
|
| 106 | while [ $x -gt 0 ]
|
| 107 | do
|
| 108 | echo $x
|
| 109 | x=$(($x-1))
|
| 110 | done
|
| 111 | ## stdout-json: "3\n2\n1\n"
|
| 112 |
|
| 113 | #### Newlines in compound lists
|
| 114 | x=3
|
| 115 | while
|
| 116 | # a couple of <newline>s
|
| 117 |
|
| 118 | # a list
|
| 119 | date && ls -d /bin || echo failed; cat tests/hello.txt
|
| 120 | # a couple of <newline>s
|
| 121 |
|
| 122 | # another list
|
| 123 | wc tests/hello.txt > _tmp/posix-compound.txt & true
|
| 124 |
|
| 125 | do
|
| 126 | # 2 lists
|
| 127 | ls -d /bin
|
| 128 | cat tests/hello.txt
|
| 129 | x=$(($x-1))
|
| 130 | [ $x -eq 0 ] && break
|
| 131 | done
|
| 132 | # Not testing anything but the status since output is complicated
|
| 133 | ## status: 0
|
| 134 |
|
| 135 | #### Multiple here docs on one line
|
| 136 | cat <<EOF1; cat <<EOF2
|
| 137 | one
|
| 138 | EOF1
|
| 139 | two
|
| 140 | EOF2
|
| 141 | ## stdout-json: "one\ntwo\n"
|
| 142 |
|
| 143 | #### cat here doc; echo; cat here doc
|
| 144 | cat <<EOF1; echo two; cat <<EOF2
|
| 145 | one
|
| 146 | EOF1
|
| 147 | three
|
| 148 | EOF2
|
| 149 | ## STDOUT:
|
| 150 | one
|
| 151 | two
|
| 152 | three
|
| 153 | ## END
|