I am trying to pass an argument to a bash script, via a golang progam, but I'm having issues with the bash script properly recieving the argument.
Golang function:
func testLink(link string) {
stdout, err := exec.Command("/bin/sh", "-c", "./test.sh", link).Output()
if err != nil {
log.Fatalf("Error running test script: %s", err)
}
fmt.Println(string(stdout))
}
Which calls the following bash script:
#!/bin/sh
LINK=${@:$OPTIND:1}
if [[ -z "$LINK" ]]; then
echo "The link is required"
exit 1
fi
PARSED_LINK=$(echo $LINK | awk '{split($0,a,"/"); print a[5]}')
#do some other actions
I can run the script no problem, and I get the expected output. However, upon running the golang code:
$ go run main.go test https://github.com/test/test
2021/12/28 20:48:02 Error running test script: exit status 1
exit status 1
How do I pass the argument in: go run main.go function argument to the bash script?
Additionaly, I know it fails with the receiving argument, because if I change the exit code in the if segment, the output from the go executable changes.
/bin/shis not bash, that's where it starts.OPTINDis normally set bygetopts, but you don't appear to have used it here.