Skip to content

Commit ff6db71

Browse files
author
Andy C
committed
[main] --eval flag respects errexit
1 parent d303d93 commit ff6db71

File tree

3 files changed

+13
-12
lines changed

3 files changed

+13
-12
lines changed

core/shell.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -974,8 +974,8 @@ def Main(
974974
if not ok:
975975
return 1
976976

977-
# Runtime error
978-
if status != 0:
977+
# YSH will stop on errors. OSH keep going, a bit like 'source'.
978+
if status != 0 and exec_opts.errexit():
979979
return status
980980

981981
#

doc/ref/chap-front-end.md

+6-6
Original file line numberDiff line numberDiff line change
@@ -106,16 +106,16 @@ They also accept these flags:
106106
-n Parse the program but don't execute it. Print the AST.
107107
--ast-format FMT The format for the AST (text|text-abbrev)
108108
--eval FILE Evaluate the given file, similar to the 'source' builtin.
109-
Specify it multiple times to run multiple files. The
110-
shell stops on normal errors, as well as when $? is
111-
non-zero after evaluating a file (even if errexit is
112-
off).
109+
Specify it multiple times to run multiple files. If the
110+
errexit option is on (e.g. in YSH), then the shell stops
111+
when $? is non-zero after evaluating a file.
113112
--tool Run a tool instead of the shell (cat-em|syntax-tree)
114113
115114
Examples:
116115
117-
osh -n -c 'hello' # pretty-print the AST
118-
ysh --ast-format text -n -c 'hello' # print it full
116+
ysh --eval one.ysh --eval two.ysh -c 'echo hi' # Run 2 files first
117+
osh -n -c 'hello' # pretty-print the AST
118+
ysh --ast-format text -n -c 'hello' # in unabridged format
119119
```
120120

121121
<h3 id="config" class="osh-ysh-topic">config</h3>

spec/sh-usage.test.sh

+5-4
Original file line numberDiff line numberDiff line change
@@ -100,23 +100,24 @@ echo 'echo one "$@"' > one.sh
100100
echo 'echo fail "$@"; ( exit 42 )' > fail.sh
101101

102102
$SH --eval one.sh \
103-
-c 'echo flag -c "$@"' dummy x y z
103+
-c 'echo status=$? flag -c "$@"' dummy x y z
104104
echo
105105

106106
# Even though errexit is off, the shell exits if the last status of an --eval
107107
# file was non-zero.
108108

109109
$SH --eval one.sh --eval fail.sh \
110-
-c 'echo flag -c "$@"' dummy x y z
110+
-c 'echo status=$? flag -c "$@"' dummy x y z
111111
echo status=$?
112112

113113
## STDOUT:
114114
one x y z
115-
flag -c x y z
115+
status=0 flag -c x y z
116116

117117
one x y z
118118
fail x y z
119-
status=42
119+
status=42 flag -c x y z
120+
status=0
120121
## END
121122

122123
## N-I bash/dash/mksh/zsh STDOUT:

0 commit comments

Comments
 (0)