diff options
| author | jet2tlf <jet2tlf@gmail.com> | 2024-05-31 03:18:58 +0000 |
|---|---|---|
| committer | jet2tlf <jet2tlf@gmail.com> | 2024-05-31 03:18:58 +0000 |
| commit | 5f49f08043849b9d2b2b14d492fb56f139277581 (patch) | |
| tree | 218728a8e7cb01abd88b454daf1c7833aa745371 /cmd/myshell | |
| parent | e88d9da033f47496018ac16248995238a7473b1a (diff) | |
| download | shell-go-5f49f08043849b9d2b2b14d492fb56f139277581.tar.gz shell-go-5f49f08043849b9d2b2b14d492fb56f139277581.zip | |
pass 5st stage
Diffstat (limited to 'cmd/myshell')
| -rw-r--r-- | cmd/myshell/main.go | 23 |
1 files changed, 19 insertions, 4 deletions
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 } } } |