Two different results of executing the command in the cmd terminal and executing the cmd command in golang
cmd
C:\Users\Administrator>cmd /c tasklist | findstr /R /I "\<cmd.exe\>"
cmd.exe 4852 Console 1 5,676 K
cmd.exe 11736 Console 1 4,836 K
golang
func main() {
cmd := exec.Command("cmd.exe", "/C", "tasklist", "|", "findstr", "/R", "/I", "cmd.exe")
output, err := cmd.Output()
if err != nil {
panic(err)
}
fmt.Println(string(output))
// Use regular filtering
cmd2 := exec.Command("cmd.exe", "/C", "tasklist", "|", "findstr", "/R", "/I", "\\<cmd.exe\\>")
output, err = cmd2.Output()
if err != nil {
panic(err)
}
fmt.Println(string(output))
}
result:
C:\Users\Administrator\AppData\Local\Temp\GoLand\___go_build_demo_go.exe
cmd.exe 4852 Console 1 5,380 K
cmd.exe 11816 Console 1 5,044 K
panic: exit status 1