| 1234567891011121314151617181920212223 |
- package main
- import (
- "os"
- "strconv"
- )
- // 退出shell
- func cmd_exit(fields []string, stdin *os.File, stdout *os.File, stderr *os.File) {
- if len(fields) > 2 { // 最多1个参数
- panic("exit: too many arguments")
- }
- if len(fields) == 1 { // 如果没有指定退出状态
- fields = append(fields, "0") // 则默认为0
- }
- code, err := strconv.Atoi(fields[1]) // 字符串转数字
- if err != nil {
- panic("exit: " + fields[1] + ": numeric argument required")
- }
- os.Exit(code) // 退出shell
- }
|