how to use the output of a bash script in a Golang function

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

how to use the output of a bash script in a Golang function

问题

这可能是不可能的,但我已经研究了一个小时,没有任何结果。我有一个 bash 脚本,它给我一个特定的输出,我想将该输出添加到 Golang 中,以便根据 bash 脚本的输出重定向一个网站。如果这没有意义,对不起,我刚接触 Go。

以下是我用来运行 bash 脚本并输出值的代码:

func main() {
    out, err := exec.Command("/bin/sh", "script.sh").Output()
    if err != nil {
        log.Fatal(err)
    }
    fmt.Printf(string(out))
}

然后,我想在另一个函数中使用输出的值来重定向一个 URL,以下是我想要复制的示例代码,其中我希望添加 $variable。这只是一个我从互联网上复制的示例,但这是我想要复制的。

func redirect(w http.ResponseWriter, r *http.Request) {
    http.Redirect(w, r, "**$variable**", 301)
}

func main() {
    http.HandleFunc("/", redirect)
    err := http.ListenAndServe(":8080", nil)
    if err != nil {
        log.Fatal("ListenAndServe: ", err)
    }
}
英文:

This might not even be possible but I've been researching for the past hour and come up blank. I have a bash script that gives me a certain output and I want to add that output to Golang in order to redirect a website based on the output of the bash script. Sorry if this makes no sense, im new to Go

Here is what I have to run the bash script and output the value

 func main() {
    out, err := exec.Command("/bin/sh", "script.sh").Output()
    if err != nil {
        log.Fatal(err)
    }
    fmt.Printf(string(out))
}

I then want to use the value that was output there in another function and to redirect a URL heres how I would redirect to a url and I wanted the $variable to be added. This is just an example I copied off the internet but its what I want to replicate.

func redirect(w http.ResponseWriter, r *http.Request) {

	http.Redirect(w, r, "**$variable**", 301)
}
 func main() {
     http.HandleFunc("/", redirect)
     err := http.ListenAndServe(":8080", nil)
     if err != nil {
         log.Fatal("ListenAndServe: ", err)
     }
 }

答案1

得分: 1

假设你的脚本只需要在启动时运行一次,那么可以使用以下代码:

var redirectTo string

func redirect(w http.ResponseWriter, r *http.Request) {
    http.Redirect(w, r, redirectTo, 301)
}

func main() {
    out, err := exec.Command("/bin/sh", "script.sh").Output()
    if err != nil {
       log.Fatal(err)
    }
    redirectTo = string(out)
    http.HandleFunc("/", redirect)
    err = http.ListenAndServe(":8080", nil)
    if err != nil {
        log.Fatal("ListenAndServe: ", err)
    }
}

或者,如果你不想使用全局变量,可以在运行时生成重定向函数:

func main() {
    out, err := exec.Command("/bin/sh", "script.sh").Output()
    if err != nil {
       log.Fatal(err)
    }
    http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
        http.Redirect(w, r, string(out), 301)
    })
    err = http.ListenAndServe(":8080", nil)
    if err != nil {
        log.Fatal("ListenAndServe: ", err)
    }
}

如果你想处理 out 为空的情况,可以这样做:

func redirect(w http.ResponseWriter, r *http.Request) {
    if len(redirectTo) == 0 {
        http.Error(w, "No URL parsed!", 500)
        return
    }
    http.Redirect(w, r, redirectTo, 301)
}

这将返回一个 HTTP 错误 500。

你也可以在不提供服务的情况下直接退出程序:

out, err := exec.Command("/bin/sh", "script.sh").Output()
if err != nil {
   log.Fatal(err)
}
if len(out) == 0 {
   log.Fatal("no output was returned from script")
}

如果你愿意,你还可以在这里对 out 添加更多的验证,比如使用 net/url 包来验证它是否是一个正确的 URL。

英文:

Assuming your script must be only run once at startup, then this should do the trick:

var redirectTo string

func redirect(w http.ResponseWriter, r *http.Request) {
    http.Redirect(w, r, redirectTo, 301)
}

func main() {
    out, err := exec.Command("/bin/sh", "script.sh").Output()
    if err != nil {
       log.Fatal(err)
    }
    redirectTo = string(out)
    http.HandleFunc("/", redirect)
    err = http.ListenAndServe(":8080", nil)
    if err != nil {
        log.Fatal("ListenAndServe: ", err)
    }
}

Or if you don't want to have a global variable you can generate the redirect function at runtime:

func main() {
    out, err := exec.Command("/bin/sh", "script.sh").Output()
    if err != nil {
       log.Fatal(err)
    }
    http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
        http.Redirect(w, r, string(out), 301)
    })
    err = http.ListenAndServe(":8080", nil)
    if err != nil {
        log.Fatal("ListenAndServe: ", err)
    }
}

if you want to handle when out is empty, you can do:

func redirect(w http.ResponseWriter, r *http.Request) {
    if len(redirectTo) == 0 {
        http.Error(w, "No URL parsed!", 500)
        return
    }
    http.Redirect(w, r, redirectTo, 301)
}

This will return an HTTP error 500 in that case.

You can also simply exit the program instead of serving:

out, err := exec.Command("/bin/sh", "script.sh").Output()
if err != nil {
   log.Fatal(err)
}
if len(out) == 0 {
   log.Fatal("no output was returned from script")
}

You can also add more verifications to out here if you wish, like if it is a correct URL, using net/url package for instance.

huangapple
  • 本文由 发表于 2022年1月12日 02:44:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/70671931.html
匿名

发表评论

匿名网友

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

确定