cmd_unset.go 527 B

123456789101112131415161718192021222324
  1. package main
  2. import (
  3. "os"
  4. "regexp"
  5. )
  6. // 重设变量
  7. func cmd_unset(fields []string, stdin *os.File, stdout *os.File, stderr *os.File) {
  8. if len(fields) == 1 { // 至少1个参数
  9. panic("unset: not enough arguments")
  10. }
  11. for _, str := range fields[1:] { // 遍历参数
  12. matched, err := regexp.MatchString("^[A-Za-z_][0-9A-Za-z_]*", str) // 是否满足用户定义变量名规则
  13. if err != nil {
  14. panic(err)
  15. }
  16. if !matched {
  17. panic("unset: '" + str + "': not a valid identifier")
  18. }
  19. os.Unsetenv(str)
  20. }
  21. }