如何将 golang 变量(可以包含任何字符)传递给 linux 中的 echo 二进制文件?
How to pass a golang variables( can contain any character) to echo binary in linux?
所以我的代码看起来像这样,我有一个函数定义如下:
func shellOut(command string) (string, string, error) {
var stdout bytes.Buffer
var stderr bytes.Buffer
cmd := exec.Command("bash", "-c", command)
cmd.Stdout = &stdout
cmd.Stderr = &stderr
err := cmd.Run()
return stdout.String(), stderr.String(), err
}
一段时间后我正在做这个。
t := "yoooooooooooo\"oo)(';#oooooooooooo"
out, stderr, err := shellOut("echo \"" + t + "\" | ./doOperation.sh")
if err != nil {
log.Printf("final error: %v\nstderr: %s", err, stderr)
}
fmt.Println(out)
但我收到如下所示的错误:
2021/10/14 22:54:18 final error: exit status 1
stderr: bash: -c: line 838: syntax error near unexpected token `('
bash: -c: line 838: ` return "Symbol(" + String(void 0 === t ? "" : t) + ")_" + (++n + r).toString(36)'
当我给变量 t 一个像“yooooo”这样的值时,它会很好地执行,那么我如何将一个带有任何奇怪字符的变量传递给 echo?有没有办法在通过之前转义所有不良字符?
出于纯粹的学术目的,我发布了这个功能,因为前阵子我不得不做类似的事情:
var bashReplacer = strings.NewReplacer(
`)`, `\)`, // using back-ticks to keep our sanity
`(`, `\(`,
`'`, `\'`,
`"`, `\"`,
`$`, `$`, // include if you don't want variable substitutions
"`", "\`", // can't use back-ticks to include a back-tick, so back to double-quotes
)
func bashEscape(s string) string { return bashReplacer.Replace(s) }
https://play.golang.org/p/uNfI_2MyjcI
然而,正如我在评论中提到的,您可以通过直接 运行 目标脚本避免 shell 转义的所有痛苦,并且不加改变地输入 UTF-8
字符串像这样:
func execWithStdin(command, stdinText string) (string, string, error) {
var (
stdout bytes.Buffer
stderr bytes.Buffer
)
cmd := exec.Command(command)
cmd.Stdin = strings.NewReader(stdinText) // pass in our data via Stdin
cmd.Stdout = &stdout
cmd.Stderr = &stderr
err := cmd.Run()
return stdout.String(), stderr.String(), err
}
使用:
t := `yoooooooooooo"oo)(';#oooooooooooo`
t += "`" // add a back-tick to make things interesting
// no shell escaping necessary:
out, stderr, err := execWithStdin("./doOperation.sh", t)
所以我的代码看起来像这样,我有一个函数定义如下:
func shellOut(command string) (string, string, error) {
var stdout bytes.Buffer
var stderr bytes.Buffer
cmd := exec.Command("bash", "-c", command)
cmd.Stdout = &stdout
cmd.Stderr = &stderr
err := cmd.Run()
return stdout.String(), stderr.String(), err
}
一段时间后我正在做这个。
t := "yoooooooooooo\"oo)(';#oooooooooooo"
out, stderr, err := shellOut("echo \"" + t + "\" | ./doOperation.sh")
if err != nil {
log.Printf("final error: %v\nstderr: %s", err, stderr)
}
fmt.Println(out)
但我收到如下所示的错误:
2021/10/14 22:54:18 final error: exit status 1
stderr: bash: -c: line 838: syntax error near unexpected token `('
bash: -c: line 838: ` return "Symbol(" + String(void 0 === t ? "" : t) + ")_" + (++n + r).toString(36)'
当我给变量 t 一个像“yooooo”这样的值时,它会很好地执行,那么我如何将一个带有任何奇怪字符的变量传递给 echo?有没有办法在通过之前转义所有不良字符?
出于纯粹的学术目的,我发布了这个功能,因为前阵子我不得不做类似的事情:
var bashReplacer = strings.NewReplacer(
`)`, `\)`, // using back-ticks to keep our sanity
`(`, `\(`,
`'`, `\'`,
`"`, `\"`,
`$`, `$`, // include if you don't want variable substitutions
"`", "\`", // can't use back-ticks to include a back-tick, so back to double-quotes
)
func bashEscape(s string) string { return bashReplacer.Replace(s) }
https://play.golang.org/p/uNfI_2MyjcI
然而,正如我在评论中提到的,您可以通过直接 运行 目标脚本避免 shell 转义的所有痛苦,并且不加改变地输入 UTF-8
字符串像这样:
func execWithStdin(command, stdinText string) (string, string, error) {
var (
stdout bytes.Buffer
stderr bytes.Buffer
)
cmd := exec.Command(command)
cmd.Stdin = strings.NewReader(stdinText) // pass in our data via Stdin
cmd.Stdout = &stdout
cmd.Stderr = &stderr
err := cmd.Run()
return stdout.String(), stderr.String(), err
}
使用:
t := `yoooooooooooo"oo)(';#oooooooooooo`
t += "`" // add a back-tick to make things interesting
// no shell escaping necessary:
out, stderr, err := execWithStdin("./doOperation.sh", t)