I want to execute multiple shell (and not shell – httpx) commands in Go and save result in to the variable. But got an error.
Here is the piece of code
echo, lookErr := exec.LookPath("echo")
httpx, lookErr := exec.LookPath("httpx")
checkError(lookErr)
cmd := fmt.Sprintf("%s %q | %s -title ", echo, urls[i], httpx)
httpxTitleResult := exec.Command(cmd)
httpxTitleResultOutput, err := httpxTitleResult.Output()
checkError(err)
In general it should look like this on
/usr/bin/echo "somewebsite" | /snap/bin/httpx -title
In the shell it works fine, but in the code I got an error
panic: fork/exec /usr/bin/echo "somewebsite" | /snap/bin/httpx -title : no such file or directory
What I’m doing wrong?
I’ve also try another approach.
cmd := fmt.Sprintf("%s %q | %s -title ", echo, urls[i], httpx)
env := os.Environ()
args := []string{"-c", cmd}
execErr := syscall.Exec(echo, args, env)
checkError(execErr)
httpxTitleResult := exec.Command(cmd)
httpxTitleResultOutput, err := httpxTitleResult.Output()
checkError(err)
fmt.Println(httpxTitleResultOutput)
but this one just printing command, not executing it and saving result in to the variable