From b42aebda8710c79c5b9dfac1bc4bd2483ea5b835 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Mart=C3=AD?= Date: Sat, 16 Feb 2019 21:29:14 +0000 Subject: [PATCH] cmd/gosh: don't print exit status errors 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. --- cmd/gosh/main.go | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/cmd/gosh/main.go b/cmd/gosh/main.go index 2b7911b8c..6a1e24b73 100644 --- a/cmd/gosh/main.go +++ b/cmd/gosh/main.go @@ -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) } @@ -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, "$ ")