"Bad value " 在 exec.Command 输出
"Bad value " in exec.Command output
我的 go 语言程序在尝试使用以下代码片段进行 ping 时打印 "Bad value for option -n 1, valid range is from 1 to 4294967295."
result , err := exec.Command("ping","-n 1", "-w 1", ip).Output()
fmt.Printf("%s\n", result)
在 Win 中从 cmd 执行时,即 'ping -n 1 -w 1 8.8.8.8' 没问题
您需要将 -n
和 -w
标志及其值分隔成单独的参数(您的 shell 已经这样做了):
result , err := exec.Command("ping", "-n", "1", "-w", "1", ip).Output()
exec.Command()
不会一次创建到 运行 的字符串。
它创建 ping
进程和一组发送给它的选项。
所以每个flag及其对应的值都需要单独传递:
result, err := exec.Command("ping","-n", "1", "-w", "1", ip).Output()
我的 go 语言程序在尝试使用以下代码片段进行 ping 时打印 "Bad value for option -n 1, valid range is from 1 to 4294967295."
result , err := exec.Command("ping","-n 1", "-w 1", ip).Output()
fmt.Printf("%s\n", result)
在 Win 中从 cmd 执行时,即 'ping -n 1 -w 1 8.8.8.8' 没问题
您需要将 -n
和 -w
标志及其值分隔成单独的参数(您的 shell 已经这样做了):
result , err := exec.Command("ping", "-n", "1", "-w", "1", ip).Output()
exec.Command()
不会一次创建到 运行 的字符串。
它创建 ping
进程和一组发送给它的选项。
所以每个flag及其对应的值都需要单独传递:
result, err := exec.Command("ping","-n", "1", "-w", "1", ip).Output()