Skip to content

Commit

Permalink
cmd/gosh: don't print exit status errors
Browse files Browse the repository at this point in the history
If the final interpreter error is an exit status, we should use that for
gosh's own exit status and not print the unnecessary error line. This is
the same that Bash and other shells do:

	$ gosh -c 'exit 2'; echo $?
	exit status 2
	1
	$ bash -c 'exit 2'; echo $?
	2

The interactive mode was handling this correctly already. Clean up the
code to be consistent there too.
  • Loading branch information
mvdan committed Feb 16, 2019
1 parent ba273b7 commit b42aebd
Showing 1 changed file with 15 additions and 10 deletions.
25 changes: 15 additions & 10 deletions cmd/gosh/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,13 @@ var (

func main() {
flag.Parse()
if err := runAll(); err != nil {
switch err := runAll().(type) {
case nil:
case interp.ShellExitStatus:
os.Exit(int(err))
case interp.ExitStatus:
os.Exit(int(err))
default:
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
Expand Down Expand Up @@ -79,15 +85,14 @@ func interactive(runner *interp.Runner) error {
}
ctx := context.Background()
for _, stmt := range stmts {
if err := runner.Run(ctx, stmt); err != nil {
switch x := err.(type) {
case interp.ShellExitStatus:
os.Exit(int(x))
case interp.ExitStatus:
default:
fmt.Fprintln(runner.Stderr, err)
os.Exit(1)
}
switch err := runner.Run(ctx, stmt).(type) {
case nil:
case interp.ShellExitStatus:
os.Exit(int(err))
case interp.ExitStatus:
default:
fmt.Fprintln(runner.Stderr, err)
os.Exit(1)
}
}
fmt.Fprintf(runner.Stdout, "$ ")
Expand Down

0 comments on commit b42aebd

Please sign in to comment.