[SOLVED] exec.Command problem

Hello Gophers,

I am currently developing a web app and I wanted to run shell commands off the web app itself.
Currently, my code has a very weird output and it’s definitely not showing what I wanted.

Lets say, I wanted this output to be shown on my webpage:
Image: https://forum.golangbridge.org/uploads/default/original/2X/5/5a3aa500e7b18910cb39a2a720a5df286693e389.png

But currently, it’s showing this junk:
Image: https://forum.golangbridge.org/uploads/default/original/2X/b/b9f0548738836c196f58a949dd656e5b472319be.png

With all that said, here’s my code:

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

// Render Page
err := tpl_admin.ExecuteTemplate(w, “index.html”, exec.Command(“uptime”))
if err != nil {
log.Fatalln("Function admin(tpl_admin.ExecuteTemplate): ", err)

I would be happy if someone could share a proper working code that includes documentation for me to learn.
Also, sorry that I couldn’t actually include the images inside here. I’m a new user

CrAzYz_ (Zypeth)

General feedback - you can include both code snippets and images here in the forums making this post both useful in the future when these external links die and making it easier for me to give feedback on mobile.

As of now it’s super hard to give feedback cause copying and pasting links in new tabs on mobile isn’t fun.

Edit - just saw your image issue was due to a new account. Is it the same for code snippets?

Hello, Joncalhoun

Hmm I don’t think so but I use images more as I feel that it’ll be more presentable.
I’ll change one of them to a code block now :slight_smile:

Edit: I changed the code into a blockquote because Code Snippets had some trouble

CrAzYz_ (Zypeth)

Images are just hard because I can’t copy paste the code to run it and see it myself. Even if you used the go playground or GitHub gists for the sample code that would help.

Anyway I believe your issue is that you are outputting an exec.Cmd object but you really want the output of that command. Try looking here - https://golang.org/pkg/os/exec/#Cmd.Output

exec.Command returns a Cmd object which you then need to call something like Output on to run it and get the output. The docs I linked have an example that should help you right under the function definition

Hey Joncalhoun,

Good one. I’ve change the code from:
err := tpl_admin.ExecuteTemplate(w, "index.html", exec.Command("uptime"))

To this now:
err := tpl_admin.ExecuteTemplate(w, "index.html", exec.Command("uptime").Output()

But now, I get an error:
# command-line-arguments ./main.go:42: multiple-value exec.Command("uptime").Output() in single-value context

I can’t initialize variables to it as I’m executing it into the web app.
Any help?

CrAzYz_ (Zypeth)

On mobile so giving a code snippet is hard but here it goes.

uptime, err := exec.Command("uptime").Output()
if err != nil {
  // Handle this
err = tpl_admin.ExecuteTemplate(w, "index.html",  uptime)

Output returns 2 arguments (this is pretty common in go) so you need to capture those and then only pass the relevant one to the Execute template call. Make sense?

Ps - prefix all your code with 4 spaces to get a code block like mine rather than a 1-liner like your code snippets. This helps retain formatting too.


Hey Joncalhoun,

Yep it makes sense. I’ve changed my code to what you showed there and now it’s outputting something like a Binary if I’d suppose.

Here’s the output:
[32 50 49 58 53 54 58 48 57 32 117 112 32 51 49 32 109 105 110 44 32 32 48 32 117 115 101 114 115 44 32 32 108 111 97 100 32 97 118 101 114 97 103 101 58 32 48 46 53 50 44 32 48 46 53 56 44 32 48 46 53 57 10]

Must I encase the output in a string() tag or something?

CrAzYz_ (Zypeth)

Oh right. Output returns a byte slice so you want to turn that into a string. Do this:

err = tpl_admin.ExecuteTemplate(w, "index.html",  string(uptime))

Assuming I can code on my phone you should be set.

1 Like

Hey Joncalhoun,

Thank you! It worked :smile:.

Anyway, your coding on phone skills are quite good so no worries there.

You’re awesome,
CrAzYz_ (Zypeth)

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.