2

I've been stuck on this one a few days, I'm trying to run a bash script which runs off of the first argument (maybe I should give up all hope, haha)

Syntax for running the script can be assumed to be:

sudo bash script argument or since it has og+x it can be ran as just sudo script argument

In go I'm running it using the following:

package main

import (
    "os"
    "os/exec"
    "fmt"
)

func main() {
    c := exec.Command("/bin/bash", "script " + argument)
    if err := c.Run(); err != nil {
        fmt.Println("Error: ", err)
    }
    os.Exit(0)
}

I have had absolutely no luck, I've tried loads of other variations as well for this...

exec.Command("/bin/sh", "-c", "sudo script", argument)

exec.Command("/bin/sh", "-c", "sudo script " + argument) (my first try)

exec.Command("/bin/bash", "-c", "sudo script" + argument)

exec.Command("/bin/bash", "sudo script", argument)

exec.Command("/bin/bash sudo script" + argument)

Most of these I am met with '/bin/bash sudo ect' no such file or directory, or Error: exit status 1 I have even gone as far as to write a Python wrapper looking for an argument and executing the bash script with subprocess. To rule out the path to the script not being defined I have tried all of the above with a direct route to the script rather than script name.

For the sake of my remaining hair, what am I doing wrong here? How can I better diagnose this problem so that I can get more information rather than exit status 1?

1
  • '/bin/bash sudo ect' would be incorrect - you could run a script this way, but not a binary executable (which sudo is). Invocation with -c option should do - but as you write that you tried that too, there's probably an additional problem. What's the error with -c? and what if you invoke sudo directly, without bash? Can you /bin/bash name_of_a_script execute that script? Commented Jul 12, 2014 at 11:33

1 Answer 1

4

You don't need to call bash/sh at all, simply pass each argument alone, also to get the error you have to capture the command's stderr, here's a working example:

func main() {
    c := exec.Command("sudo", "ls", "/tmp")
    stderr := &bytes.Buffer{}
    stdout := &bytes.Buffer{}
    c.Stderr = stderr
    c.Stdout = stdout
    if err := c.Run(); err != nil {
        fmt.Println("Error: ", err, "|", stderr.String())
    } else {
        fmt.Println(stdout.String())
    }
    os.Exit(0)
}
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks a bunch for this, with the added information from the error I'm getting: Error: exit status 1 | sudosh: couldn't get your controlling terminal. So the bash script is calling sudosh and can't find my terminal due to I assume the way go is calling it
You just need to use c := exec.Command("sudo", "script", argument)
That is what's being ran.. c := exec.Command("sudo", "path/to/script", argument) both /path/to/script and script yield the same
You'd have to use a fake terminal, has nothing to do with the way Go is calling it, some apps require a pty to operate, that'd be a different question with a very long answer.
Thanks for the reply @OneOfOne this helps a lot. <- that sounds sarcastic, it's not meant to

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.