如何将变量值从go传递到shell脚本



shell。sh

#!/bin/bash
npx create-react-app <project-name> --template typescript

run.go

func runScript(genErr *error) {
if *genErr != nil {
return
}
cmd := exec.Command("/bin/sh", "-c", shell)
*genErr = cmd.Run()
}

details.go

type npmLibCommand struct {
ProjectName string
}
var npmLib npmLibCommand
func getNpmLibraryInput(genErr *error) {
if *genErr != nil {
return
}
npmLib.ProjectName = GetProjectName(genErr)
}

我想把projectName变量从run.go传递到shell.sh。如何使用golang?

我在shell.sh中使用了$。它不起作用。

a.sh

echo $PassedName

main.go

package main
import (
"os"
"os/exec"
)
func main() {
// run your shell script
// don't forget to mention ./
cmd := exec.Command("/bin/sh", "-c", "./a.sh")
// whatever variable you want to pass append it to cmd.Env
// it's format is of key=value
cmd.Env = append(cmd.Env, "PassedName=hello")
// set stdout and stderr appropriately as per your needs
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Run() // run the cmd
}

相关内容

  • 没有找到相关文章

最新更新