From 5f49f08043849b9d2b2b14d492fb56f139277581 Mon Sep 17 00:00:00 2001 From: jet2tlf Date: Fri, 31 May 2024 00:18:58 -0300 Subject: pass 5st stage --- cmd/myshell/main.go | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) (limited to 'cmd') diff --git a/cmd/myshell/main.go b/cmd/myshell/main.go index 7e8a9a0..d44fb99 100644 --- a/cmd/myshell/main.go +++ b/cmd/myshell/main.go @@ -4,6 +4,7 @@ import ( "bufio" "fmt" "os" + "strconv" "strings" ) @@ -17,10 +18,24 @@ func main() { fmt.Println(err.Error()) } - if in == "exit 0\n" { - os.Exit(0) - } else { - fmt.Printf("%s: command not found\n", strings.TrimRight(in, "\n")) + trim := strings.TrimSpace(in) + + commands := strings.Split(trim, " ") + + switch commands[0] { + case "exit": + code, err := strconv.Atoi(commands[1]) + if err != nil { + fmt.Println(err.Error()) + } + os.Exit(code) + return + case "echo": + fmt.Printf("%s\n", strings.Join(commands[1:], " ")) + return + default: + fmt.Printf("%s: command not found\n", commands[0]) + return } } } -- cgit v1.2.3