| 1 | ## tags: dev-minimal interactive | 
| 2 | ## compare_shells: bash | 
| 3 |  | 
| 4 | #### 'exit' in oshrc (regression) | 
| 5 | cat >$TMP/oshrc <<EOF | 
| 6 | echo one | 
| 7 | exit 42 | 
| 8 | echo two | 
| 9 | EOF | 
| 10 | $SH --rcfile $TMP/oshrc -i -c 'echo hello' | 
| 11 | ## status: 42 | 
| 12 | ## STDOUT: | 
| 13 | one | 
| 14 | ## END | 
| 15 |  | 
| 16 | #### fatal errors continue | 
| 17 | # NOTE: tried here doc, but sys.stdin.isatty() fails.  Could we fake it? | 
| 18 | $SH --rcfile /dev/null -i -c ' | 
| 19 | echo $(( 1 / 0 )) | 
| 20 | echo one | 
| 21 | exit 42 | 
| 22 | ' | 
| 23 | ## status: 42 | 
| 24 | ## STDOUT: | 
| 25 | one | 
| 26 | ## END | 
| 27 |  | 
| 28 | #### interactive shell loads rcfile (when combined with -c) | 
| 29 | $SH -c 'echo 1' | 
| 30 | cat >$TMP/rcfile <<EOF | 
| 31 | echo RCFILE | 
| 32 | EOF | 
| 33 | $SH --rcfile $TMP/rcfile -i -c 'echo 2' | 
| 34 | ## STDOUT: | 
| 35 | 1 | 
| 36 | RCFILE | 
| 37 | 2 | 
| 38 | ## END | 
| 39 |  | 
| 40 | #### interactive shell loads files in rcdir (when combined with -c) | 
| 41 |  | 
| 42 | $SH -c 'echo A' | 
| 43 |  | 
| 44 | cat >$TMP/rcfile <<EOF | 
| 45 | echo 'rcfile first' | 
| 46 | EOF | 
| 47 |  | 
| 48 | mkdir -p $TMP/rcdir | 
| 49 |  | 
| 50 | cat >$TMP/rcdir/file1 <<EOF | 
| 51 | echo rcdir 1 | 
| 52 | EOF | 
| 53 |  | 
| 54 | cat >$TMP/rcdir/file2 <<EOF | 
| 55 | echo rcdir 2 | 
| 56 | EOF | 
| 57 |  | 
| 58 | # --rcdir only | 
| 59 | $SH --rcdir $TMP/rcdir -i -c 'echo B' | 
| 60 |  | 
| 61 | $SH --rcfile $TMP/rcfile --rcdir $TMP/rcdir -i -c 'echo C' | 
| 62 |  | 
| 63 | ## STDOUT: | 
| 64 | A | 
| 65 | rcdir 1 | 
| 66 | rcdir 2 | 
| 67 | B | 
| 68 | rcfile first | 
| 69 | rcdir 1 | 
| 70 | rcdir 2 | 
| 71 | C | 
| 72 | ## END | 
| 73 |  | 
| 74 | ## N-I bash status: 2 | 
| 75 | ## N-I bash STDOUT: | 
| 76 | A | 
| 77 | ## END | 
| 78 |  | 
| 79 | #### nonexistent --rcdir is ignored | 
| 80 | case $SH in bash) exit ;; esac | 
| 81 |  | 
| 82 | $SH --rcdir $TMP/__does-not-exist -i -c 'echo hi' | 
| 83 | echo status=$? | 
| 84 |  | 
| 85 | ## STDOUT: | 
| 86 | hi | 
| 87 | status=0 | 
| 88 | ## END | 
| 89 | ## N-I bash STDOUT: | 
| 90 | ## END | 
| 91 |  | 
| 92 | #### shell doesn't load rcfile/rcdir if --norc is given | 
| 93 |  | 
| 94 | $SH -c 'echo A' | 
| 95 |  | 
| 96 | cat >$TMP/rcfile <<EOF | 
| 97 | echo rcfile | 
| 98 | EOF | 
| 99 |  | 
| 100 | mkdir -p $TMP/rcdir | 
| 101 | cat >$TMP/rcdir/file1 <<EOF | 
| 102 | echo rcdir 1 | 
| 103 | EOF | 
| 104 |  | 
| 105 | cat >$TMP/rcdir/file2 <<EOF | 
| 106 | echo rcdir 2 | 
| 107 | EOF | 
| 108 |  | 
| 109 | $SH --norc --rcfile $TMP/rcfile -c 'echo C' | 
| 110 | case $SH in bash) exit ;; esac | 
| 111 |  | 
| 112 | $SH --norc --rcfile $TMP/rcfile --rcdir $TMP/rcdir -c 'echo D' | 
| 113 |  | 
| 114 | ## STDOUT: | 
| 115 | A | 
| 116 | C | 
| 117 | D | 
| 118 | ## END | 
| 119 | ## OK bash STDOUT: | 
| 120 | A | 
| 121 | C | 
| 122 | ## END | 
| 123 |  | 
| 124 |  | 
| 125 | #### interactive shell runs PROMPT_COMMAND after each command | 
| 126 | export PS1=''  # OSH prints prompt to stdout | 
| 127 |  | 
| 128 | case $SH in | 
| 129 | *bash|*osh) | 
| 130 | $SH --rcfile /dev/null -i << EOF | 
| 131 | PROMPT_COMMAND='echo PROMPT' | 
| 132 | echo one | 
| 133 | echo two | 
| 134 | EOF | 
| 135 | ;; | 
| 136 | esac | 
| 137 |  | 
| 138 | # Paper over difference with OSH | 
| 139 | case $SH in *bash) echo '^D';; esac | 
| 140 |  | 
| 141 | ## STDOUT: | 
| 142 | PROMPT | 
| 143 | one | 
| 144 | PROMPT | 
| 145 | two | 
| 146 | PROMPT | 
| 147 | ^D | 
| 148 | ## END | 
| 149 |  | 
| 150 |  | 
| 151 | #### parse error in PROMPT_COMMAND | 
| 152 | export PS1=''  # OSH prints prompt to stdout | 
| 153 |  | 
| 154 | case $SH in | 
| 155 | *bash|*osh) | 
| 156 | $SH --rcfile /dev/null -i << EOF | 
| 157 | PROMPT_COMMAND=';' | 
| 158 | echo one | 
| 159 | echo two | 
| 160 | EOF | 
| 161 | ;; | 
| 162 | esac | 
| 163 |  | 
| 164 | # Paper over difference with OSH | 
| 165 | case $SH in *bash) echo '^D';; esac | 
| 166 |  | 
| 167 | ## STDOUT: | 
| 168 | one | 
| 169 | two | 
| 170 | ^D | 
| 171 | ## END | 
| 172 |  | 
| 173 | #### runtime error in PROMPT_COMMAND | 
| 174 | export PS1=''  # OSH prints prompt to stdout | 
| 175 |  | 
| 176 | case $SH in | 
| 177 | *bash|*osh) | 
| 178 | $SH --rcfile /dev/null -i << 'EOF' | 
| 179 | PROMPT_COMMAND='echo PROMPT $(( 1 / 0 ))' | 
| 180 | echo one | 
| 181 | echo two | 
| 182 | EOF | 
| 183 | ;; | 
| 184 | esac | 
| 185 |  | 
| 186 | # Paper over difference with OSH | 
| 187 | case $SH in *bash) echo '^D';; esac | 
| 188 |  | 
| 189 | ## STDOUT: | 
| 190 | one | 
| 191 | two | 
| 192 | ^D | 
| 193 | ## END | 
| 194 |  | 
| 195 | #### Error message with bad oshrc file (currently ignored) | 
| 196 | cd $TMP | 
| 197 | echo 'foo >' > bad_oshrc | 
| 198 |  | 
| 199 | $SH --rcfile bad_oshrc -i -c 'echo hi' 2>stderr.txt | 
| 200 | echo status=$? | 
| 201 |  | 
| 202 | # bash prints two lines | 
| 203 | grep --max-count 1 -o 'bad_oshrc:' stderr.txt | 
| 204 |  | 
| 205 | ## STDOUT: | 
| 206 | hi | 
| 207 | status=0 | 
| 208 | bad_oshrc: | 
| 209 | ## END | 
| 210 |  | 
| 211 |  | 
| 212 | #### PROMPT_COMMAND can see $?, like bash | 
| 213 |  | 
| 214 | # bug fix #853 | 
| 215 |  | 
| 216 | export PS1=''  # OSH prints prompt to stdout | 
| 217 |  | 
| 218 | case $SH in | 
| 219 | *bash|*osh) | 
| 220 | $SH --rcfile /dev/null -i << 'EOF' | 
| 221 | myfunc() { echo last_status=$?;  } | 
| 222 | PROMPT_COMMAND='myfunc' | 
| 223 | ( exit 42 ) | 
| 224 | ( exit 43 ) | 
| 225 | echo ok | 
| 226 | EOF | 
| 227 | ;; | 
| 228 | esac | 
| 229 |  | 
| 230 | # Paper over difference with OSH | 
| 231 | case $SH in *bash) echo '^D';; esac | 
| 232 | ## STDOUT: | 
| 233 | last_status=0 | 
| 234 | last_status=42 | 
| 235 | last_status=43 | 
| 236 | ok | 
| 237 | last_status=0 | 
| 238 | ^D | 
| 239 | ## END | 
| 240 |  | 
| 241 | #### PROMPT_COMMAND that writes to BASH_REMATCH | 
| 242 | export PS1='' | 
| 243 |  | 
| 244 | case $SH in | 
| 245 | *bash|*osh) | 
| 246 | $SH --rcfile /dev/null -i << 'EOF' | 
| 247 | PROMPT_COMMAND='[[ clobber =~ (.)(.)(.) ]]; echo ---' | 
| 248 | echo one | 
| 249 | [[ bar =~ (.)(.)(.) ]] | 
| 250 | echo ${BASH_REMATCH[@]} | 
| 251 | EOF | 
| 252 | ;; | 
| 253 | esac | 
| 254 |  | 
| 255 | # Paper over difference with OSH | 
| 256 | case $SH in *bash) echo '^D';; esac | 
| 257 |  | 
| 258 | ## STDOUT: | 
| 259 | --- | 
| 260 | one | 
| 261 | --- | 
| 262 | --- | 
| 263 | bar b a r | 
| 264 | --- | 
| 265 | ^D | 
| 266 | ## END | 
| 267 | ## OK bash STDOUT: | 
| 268 | --- | 
| 269 | one | 
| 270 | --- | 
| 271 | --- | 
| 272 | clo c l o | 
| 273 | --- | 
| 274 | ^D | 
| 275 | ## END | 
| 276 |  | 
| 277 |  | 
| 278 | #### NO ASSERTIONS: Are startup files sourced before or after job control? | 
| 279 |  | 
| 280 | cat >myrc <<'EOF' | 
| 281 |  | 
| 282 | # from test/process-table-portable.sh | 
| 283 | PS_COLS='pid,ppid,pgid,sid,tpgid,comm' | 
| 284 |  | 
| 285 | show-shell-state() { | 
| 286 | local prefix=$1 | 
| 287 |  | 
| 288 | echo -n "$prefix: " | 
| 289 |  | 
| 290 | echo "pid = $$" | 
| 291 |  | 
| 292 | # Hm TPGID has changed in both OSH and bash | 
| 293 | # I guess that's because because ps itself becomes the leader of the process | 
| 294 | # group | 
| 295 |  | 
| 296 | ps -o $PS_COLS $$ | 
| 297 | } | 
| 298 |  | 
| 299 | show-shell-state myrc | 
| 300 |  | 
| 301 |  | 
| 302 | EOF | 
| 303 |  | 
| 304 | $SH --rcfile myrc -i -c 'show-shell-state main' | 
| 305 |  | 
| 306 | ## status: 0 | 
| 307 |  | 
| 308 | # No assertions | 
| 309 | # TODO: spec test framework should be expanded to properly support these | 
| 310 | # comparisons. | 
| 311 | # The --details flag is useful | 
| 312 |  | 
| 313 |  | 
| 314 | #### HISTFILE is written in interactive shell | 
| 315 |  | 
| 316 | rm -f myhist | 
| 317 | export HISTFILE=myhist | 
| 318 | echo 'echo hist1; echo hist2' | $SH --norc -i | 
| 319 |  | 
| 320 | if test -n "$BASH_VERSION"; then | 
| 321 | echo '^D'  # match OSH for now | 
| 322 | fi | 
| 323 |  | 
| 324 | cat myhist | 
| 325 | # cat ~/.config/oil/history_osh | 
| 326 |  | 
| 327 | ## STDOUT: | 
| 328 | hist1 | 
| 329 | hist2 | 
| 330 | ^D | 
| 331 | echo hist1; echo hist2 | 
| 332 | ## END | 
| 333 |  | 
| 334 |  | 
| 335 | #### HISTFILE default value | 
| 336 |  | 
| 337 | # it ends with _history | 
| 338 | $SH --norc -i -c 'echo HISTFILE=$HISTFILE' | egrep -q '_history$' | 
| 339 | echo status=$? | 
| 340 |  | 
| 341 | ## STDOUT: | 
| 342 | status=0 | 
| 343 | ## END |