2 # This file contains tests corresponding to the `Shell Grammar' texinfo node.
7 mkdir basic.tmp && cd basic.tmp
10 echo "'" >unmatched_quote.txt
14 # Tests for `Simple Commands and Pipelines'
16 echo foo | cat | sed 's/foo/bar/'
17 0:Basic pipeline handling
21 0:Exit status of pipeline with builtins (true)
24 1:Exit status of pipeline with builtins (false)
26 fn() { local foo; read foo; print $foo; }
28 print -p coproc test output
31 0:Basic coprocess handling
34 true | false && print true || print false
38 false | true && print true || print false
42 (cd /NonExistentDirectory >&/dev/null) || print false
43 0:Basic subshell list with error
46 { cd /NonExistentDirectory >&/dev/null } || print false
47 0:Basic current shell list with error
51 # Tests for `Precommand Modifiers'
53 - $ZTST_testdir/../Src/zsh -fc "[[ \$0 = \"-$ZTST_testdir/../Src/zsh\" ]]"
54 0:`-' precommand modifier
58 0:`noglob' precommand modifier
62 (exec /bin/sh; echo bar)
63 0:`exec' precommand modifier
65 (exec -l /bin/sh -c 'echo $0')
66 0:`exec' with -l option
69 (exec -a /bin/SPLATTER /bin/sh -c 'echo $0')
70 0:`exec' with -a option
73 (exec -a/bin/SPLOOSH /bin/sh -c 'echo $0')
74 0:`exec' with -a option, no space
77 (export FOO=bar; exec -c /bin/sh -c 'echo x${FOO}x')
78 0:`exec' with -c option
81 cat() { echo Function cat executed; }
82 command cat && unfunction cat
83 0:`command' precommand modifier
84 <External command cat executed
85 >External command cat executed
87 cd() { echo Not cd at all; }
88 builtin cd . && unfunction cd
89 0:`builtin' precommand modifier
92 # Tests for `Complex Commands'
129 ?(eval):3: parse error near `fi'
131 for name in word to term; do
143 0:`for' loop with newline before in keyword
148 for (( name = 0; name < 3; name++ )); do
151 0:arithmetic `for' loop
156 for keyvar valvar in key1 val1 key2 val2; do
157 print key=$keyvar val=$valvar
159 0:enhanced `for' syntax with two loop variables
163 for keyvar valvar stuffvar in keyA valA stuffA keyB valB stuffB; do
164 print key=$keyvar val=$valvar stuff=$stuffvar
166 0:enhanced `for' syntax with three loop variables
167 >key=keyA val=valA stuff=stuffA
168 >key=keyB val=valB stuff=stuffB
170 for in in in in in stop; do
173 0:compatibility of enhanced `for' syntax with standard syntax
180 while (( name < 3 )); do
190 until (( name == 3 )); do
246 0:`case', new syntax, cascaded
251 ## Select now reads from stdin if the shell is not interactive.
252 ## Its own output goes to stderr.
255 select name in one two three; do
260 ?1) one 2) two 3) three
264 function name1 name2 () { print This is $0; }
266 name1 name2() { print This is still $0; }
272 (time cat) >&/dev/null
273 0:`time' keyword (status only)
275 if [[ -f foo && -d . && -n $ZTST_testdir ]]; then
280 0:basic [[ ... ]] test
283 # Current shell execution with try/always form.
284 # We put those with errors in subshells so that any unhandled error doesn't
291 print The always block.
293 print After the always block.
294 0:Basic `always' syntax
297 >After the always block.
301 print ${*this is an error*}
304 if (( TRY_BLOCK_ERROR )); then
305 print An error occurred.
307 print No error occurred.
310 print Position three)
311 1:Always block with error not reset
314 ?(eval):3: bad substitution
318 print ${*voici une erreur}
321 if (( TRY_BLOCK_ERROR )); then
322 print Erratum factum est. Retro ponetur.
323 (( TRY_BLOCK_ERROR = 0 ))
325 print unray touay foay anguageslay
328 print Status after always block is $?.)
329 0:Always block with error reset
331 >Erratum factum est. Retro ponetur.
332 >Status after always block is 1.
333 ?(eval):3: bad substitution
335 fn() { { return } always { echo always 1 }; echo not executed }
337 fn() { { echo try 2 } always { return }; echo not executed }
339 0:Always block interaction with return
343 # Outputting of structures from the wordcode is distinctly non-trivial,
344 # we probably ought to have more like the following...
345 fn1() { { echo foo; } }
346 fn2() { { echo foo; } always { echo bar; } }
347 fn3() { ( echo foo; ) }
348 functions fn1 fn2 fn3
349 0:Output of syntactic structures with and without always blocks
370 # Tests for `Alternate Forms For Complex Commands'
373 if (true) { print true-1 } elif (true) { print true-2 } else { print false }
374 if (false) { print true-1 } elif (true) { print true-2 } else { print false }
375 if (false) { print true-1 } elif (false) { print true-2 } else { print false }
376 0:Alternate `if' with braces
385 for name ( word1 word2 word3 ) print $name
386 0:Form of `for' with parentheses.
391 for name in alpha beta gamma; print $name
392 0:Short form of `for'
397 for (( val = 2; val < 10; val *= val )) print $val
398 0:Short arithmetic `for'
402 foreach name ( verbiage words periphrasis )
410 # see comment with braces used in if loops
412 while (( val < 2 )) { print $((val++)); }
413 0:Alternative `while'
418 until (( val == 0 )) { print $((val--)); }
419 0:Alternative `until'
423 repeat 3 print Hip hip hooray
430 (alpha) print schmalpha
432 (bravo) print schmavo
434 (charlie) print schmarlie
440 for word in artichoke bladderwort chrysanthemum Zanzibar
442 (*der*) print $word contains the forbidden incantation der
444 (a*) print $word begins with a
446 ([[:upper:]]*) print $word either begins with a or an upper case letter
448 ([[:lower:]]*) print $word begins with a lower case letter
450 (*e*) print $word contains an e
453 0:`case' with mixed ;& and ;|
454 >artichoke begins with a
455 >artichoke either begins with a or an upper case letter
456 >artichoke begins with a lower case letter
457 >artichoke contains an e
458 >bladderwort contains the forbidden incantation der
459 >chrysanthemum begins with a lower case letter
460 >chrysanthemum contains an e
461 >Zanzibar either begins with a or an upper case letter
463 print -u $ZTST_fd 'This test hangs the shell when it fails...'
465 # The number 4375 here is chosen to produce more than 16384 bytes of output
466 while (( name < 4375 )); do
469 done < /dev/null | { read name; print done }
470 0:Bug regression: `while' loop with redirection and pipeline
473 # This used to be buggy and print X at the end of each iteration.
477 0:Handling of ||'s and &&'s with a for loop in between
484 # Same bug for &&, used to print `no' at the end of each iteration
486 false && print strange
488 0:Handling of &&'s and ||'s with a for loop in between
491 $ZTST_testdir/../Src/zsh -f unmatched_quote.txt
492 1:Parse error with file causes non-zero exit status
493 ?unmatched_quote.txt:2: unmatched '
495 $ZTST_testdir/../Src/zsh -f <unmatched_quote.txt
496 1:Parse error on standard input causes non-zero exit status
499 $ZTST_testdir/../Src/zsh -f -c "'"
500 1:Parse error on inline command causes non-zero exit status
503 $ZTST_testdir/../Src/zsh -f NonExistentScript
504 127q:Non-existent script causes exit status 127
505 ?$ZTST_testdir/../Src/zsh: can't open input file: NonExistentScript
508 # use this to get stuff at start of line
509 contents=$'# comment \'\necho value #with " stuff\n# comment\n#comment
511 eval 'VAR=$('"$contents"')'
513 0:comments within $(...)