cmd_exit.go 506 B

1234567891011121314151617181920212223
  1. package main
  2. import (
  3. "os"
  4. "strconv"
  5. )
  6. // 退出shell
  7. func cmd_exit(fields []string, stdin *os.File, stdout *os.File, stderr *os.File) {
  8. if len(fields) > 2 { // 最多1个参数
  9. panic("exit: too many arguments")
  10. }
  11. if len(fields) == 1 { // 如果没有指定退出状态
  12. fields = append(fields, "0") // 则默认为0
  13. }
  14. code, err := strconv.Atoi(fields[1]) // 字符串转数字
  15. if err != nil {
  16. panic("exit: " + fields[1] + ": numeric argument required")
  17. }
  18. os.Exit(code) // 退出shell
  19. }