| 123456789101112131415161718192021222324 |
- package main
- import (
- "os"
- "regexp"
- )
- // 重设变量
- func cmd_unset(fields []string, stdin *os.File, stdout *os.File, stderr *os.File) {
- if len(fields) == 1 { // 至少1个参数
- panic("unset: not enough arguments")
- }
- for _, str := range fields[1:] { // 遍历参数
- matched, err := regexp.MatchString("^[A-Za-z_][0-9A-Za-z_]*", str) // 是否满足用户定义变量名规则
- if err != nil {
- panic(err)
- }
- if !matched {
- panic("unset: '" + str + "': not a valid identifier")
- }
- os.Unsetenv(str)
- }
- }
|