英文:
Cannot pass argument to bash script through golang exec.Commad()
问题
我正在尝试通过一个Golang程序将参数传递给一个bash脚本,但是我在bash脚本正确接收参数方面遇到了问题。
Golang函数:
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))
}
该函数调用以下bash脚本:
#!/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
我可以顺利运行脚本,并获得预期的输出。然而,在运行Golang代码时:
$ 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
我如何将参数传递给bash脚本:go run main.go function argument
?
此外,我知道它在接收参数时失败,因为如果我更改if段中的退出代码,go可执行文件的输出也会发生变化。
英文:
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.
答案1
得分: 3
要传递参数,你需要删除-c
,否则它们将被视为sh可执行文件的参数,而不是你的脚本。
cmd := exec.Command("/bin/sh", "./test.sh", link)
我猜你的问题还在于你如何编写你的shell脚本。那个变量赋值看起来很奇怪。shellcheck 在下面的第2行报告了这个问题。
将数组分配给字符串!分配为数组,或者使用*而不是@进行连接。shellcheck(SC2124)
在POSIX sh中,字符串索引是未定义的。shellcheck(SC3057)
当我尝试像你那样通过shell执行带有参数的脚本时,我会得到一个错误。
#!/bin/sh
LINK=${@:$OPTIND:1}
echo "$LINK"
$ ./test.sh foo
./test.sh: 2: Bad substitution
我建议改为像这样分配变量。
#!/bin/sh
LINK="$1"
...
英文:
For the arguments to be passed, you need to remove the -c
otherwise they are treated as argument to the sh executable rather than your script.
cmd := exec.Command("/bin/sh", "./test.sh", link)
I guess additionally your problem is how you wrote your shell script. That variable assignment looks odd. shellcheck reports this at line 2 below.
> Assigning an array to a string! Assign as array, or use * instead of @ to concatenate. shellcheck(SC2124)
In POSIX sh, string indexing is undefined. shellcheck(SC3057)
When I try to execute a script with the argument set like you did via shell, I get an error.
#!/bin/sh
LINK=${@:$OPTIND:1}
echo "$LINK"
$ ./test.sh foo
./test.sh: 2: Bad substitution
I suggest assigning the variable like this instead.
#!/bin/sh
LINK="$1"
...
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论