package main import ( "fmt" "os" "strconv" "strings" ) // 显示所有环境变量或设置命令行参数 func cmd_set(fields []string, stdin *os.File, stdout *os.File, stderr *os.File) { if len(fields) == 1 { // 如果没有参数 for _, env := range os.Environ() { // 则输出所有用户和shell环境变量 fmt.Fprintln(stdout, env) } } else { tot, _ := strconv.Atoi(os.Getenv("#")) // 获取参数个数 for i := 1; i <= tot; i++ { // 重设所有参数 os.Unsetenv(strconv.Itoa(i)) } for i := 1; i < len(fields); i++ { // 设置新参数 os.Setenv(strconv.Itoa(i), fields[i]) } os.Setenv("#", strconv.Itoa(len(fields)-1)) // 更新参数个数 os.Setenv("*", strings.Join(fields[1:], " ")) // 更新所有命令行参数的值 } }