如何将 Golang 变量传递给 Bash 脚本?

huangapple go评论121阅读模式
英文:

How do i pass a Golang variable to Bash Script?

问题

script.sh
>

  1. #!/bin/sh
  2. dbt run --select $model_tag --profiles-dir .

想要运行这个 shell 脚本,从我的 .go 文件中获取变量 model_tag

invoke.go
>

  1. package main
  2. import (
  3. "fmt"
  4. "log"
  5. "net/http"
  6. "os"
  7. "os/exec"
  8. )
  9. func handler(w http.ResponseWriter, r *http.Request) {
  10. log.Print("helloworld: received a request")
  11. mt := r.Header.Get("Model-Tag")
  12. cmd := exec.CommandContext(r.Context(), "/bin/sh", "script.sh")
  13. cmd.Env = append(os.Environ(), fmt.Sprintf("model_tag=%s", mt))
  14. cmd.Stderr = os.Stderr
  15. out, err := cmd.Output()
  16. if err != nil {
  17. w.WriteHeader(500)
  18. }
  19. w.Write(out)
  20. }
  21. func main() {
  22. log.Print("helloworld: starting server...")
  23. http.HandleFunc("/", handler)
  24. port := os.Getenv("PORT")
  25. if port == "" {
  26. port = "8080"
  27. }
  28. log.Printf("helloworld: listening on %s", port)
  29. log.Fatal(http.ListenAndServe(fmt.Sprintf(":%s", port), nil))
  30. }

在这里,mt 是从请求中接收到的头部信息,我需要在执行 shell 脚本之前将其传递给脚本。
如何在使用 go 文件执行 shell 脚本之前设置 model_tag = mt?

尝试直接设置 model_tag = mt,会抛出语法错误。

英文:

script.sh
>

  1. #!/bin/sh
  2. dbt run --select $model_tag --profiles-dir .

Want to run this shell script that takes the Variable model_tag from my .go file

invoke.go
>

  1. package main
  2. import (
  3. "fmt"
  4. "log"
  5. "net/http"
  6. "os"
  7. "os/exec"
  8. )
  9. func handler(w http.ResponseWriter, r *http.Request) {
  10. log.Print("helloworld: received a request")
  11. mt := r.Header.Get("Model-Tag")
  12. cmd := exec.CommandContext(r.Context(), "/bin/sh","script.sh")
  13. cmd.Stderr = os.Stderr
  14. out, err := cmd.Output()
  15. if err != nil {
  16. w.WriteHeader(500)
  17. }
  18. w.Write(out)
  19. }
  20. func main() {
  21. log.Print("helloworld: starting server...")
  22. http.HandleFunc("/", handler)
  23. port := os.Getenv("PORT")
  24. if port == "" {
  25. port = "8080"
  26. }
  27. log.Printf("helloworld: listening on %s", port)
  28. log.Fatal(http.ListenAndServe(fmt.Sprintf(":%s", port), nil))
  29. }

Here, mt is the header being received from a request, that i need to pass to the shell script before execution?
How do i set model_tag = mt before executing the shell script using the go file?

Tried setting model_tag = mt directly, throws a syntax error

答案1

得分: 2

在执行运行 script.sh 的命令之前,执行以下操作:

  1. os.Setenv("model_tag", mt)
英文:

Before executing your cmd which runs script.sh, do a

  1. os.Setenv("model_tag", mt)

答案2

得分: 0

尝试使用以下代码:

  1. cmd := exec.CommandContext(r.Context(), "/bin/sh", fmt.Sprintf("model_tag=%s script.sh", mt))

请注意,这是一段Go语言代码,用于执行Shell命令。

英文:

try with

  1. cmd := exec.CommandContext(r.Context(), "/bin/sh",fmt.Sprintf("model_tag=%s script.sh",mt))

huangapple
  • 本文由 发表于 2022年11月18日 17:51:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/74487779.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定