How can I run the dir command with golang? - go

Here is my code:
package main
import (
"bytes"
"fmt"
"io"
"os/exec"
)
func runCommand(command string) io.Writer{
cmdName := "cmd.exe"
cmdArgs := []string{"/c", command}
fmt.Println("Running command: " + command)
cmd := exec.Command(cmdName, cmdArgs...)
var out bytes.Buffer
var stderr bytes.Buffer
cmd.Stdout = &out
cmd.Stderr = &stderr
cmd.Run()
return cmd.Stdout
}
func main(){
fmt.Println(runCommand("dir")) // Prints the output of dir for the current directory
fmt.Println(runCommand("dir C:\\")) // Prints nothing
fmt.Println(runCommand("dir C:\\Users\\")) //Prints the output of dir for the users directory
fmt.Println(runCommand("dir C:\\..\\")) // Prints the output of dir for the C drive (What I want)
}
I'm expecting that when I execute dir C:\ That I would get the output as if I had ran in in a windows command prompt. Instead I get nothing. Intestingly, any other path when running dir works just fine. I can even see C:\ If I instead execute C:\..\ Why is this? I don't understand why this happens, and every other windows command I have given it works fine.

First of all, never ignore errors. The call to cmd.Run() returns an error, you should always check it:
if err := cmd.Run(); err != nil {
fmt.Printf(os.Stderr, "%v", err)
}
Try that and you might see why your command is failing.
Without knowing the error, it's hard to help fixing your problem, but I'd guess you need to split the string command into several fields and append them to cmdArgs. When running runCommand("dir C:\\"), your cmdArgs is actually []string{"/c", "dir C:\\"), I think it should be []string{"/c", "dir", "C:\\"}. Take a look at the function strings.Split(string, string), it might help you. But that's just a guess, we need to know the exact error message you're having for a proper solution :)

Related

Go : Correctly running external program with arguments

Good evening,
I'm working on converting some tools written in python to Go in order to better understand it.
I need the program to call an external .exe with some arguments in order for it to correctly format some data. In the windows shell I can do C:\path_to_exe\file.exe arg1 arg2 "C:\path_to_output\output.txt"
I believe the correct method to do this in Go would be using exec.Command, but I'm not getting any...meaningful results.
out, err := exec.Command("cmd", "C:\\path\\tools\\util\\Utility.exe C:\\file_Location \"select * from TABLE\" C:\\output_path\\output.txt").Output()
fmt.Printf("\n", string(out))
if err != nil {
println(" Error running decomp ", err)
}
This appears to be running command, as the output I am receiving is:
%!(EXTRA string=Microsoft Windows [Version 10.0.22000.739]
(c) Microsoft Corporation. All rights reserved.
Process finished with the exit code 0
Just for giggles I tried breaking up the arguments, but the same result was achieved
out, err := exec.Command("cmd", exPath, utilPath, statement, textOutputPath+"test.txt").Output()
I'm expecting the executed program to run, parse the correct file based on the input, and output the specified txt file. I am left with no .txt file, and the go program runs much faster then the parsing should take.
There must be something I'm missing, could someone please provide some insight on the correct usage of exec.Command, because every example I can find appears to show that this should work.
Why are you spawning cmd.exe and having it run your utility.exe?
You can just spawn utility on its own.
For instance, suppose you have two binaries, hello and say-hello living in the same directory, compiled from
hello.go → hello:
package main
import (
"fmt"
"os"
)
func main() {
argv := os.Args[1:]
if len(argv) == 0 {
argv = []string{"world"}
}
for _, arg := range argv {
fmt.Printf("Hello, %s!\n", arg)
}
}
say-hello.go → say-hello:
package main
import (
"fmt"
"os"
"os/exec"
)
func main() {
process := exec.Command("./hello", os.Args[1:]...)
process.Stdin = os.Stdin
process.Stdout = os.Stdout
process.Stderr = os.Stderr
if err := process.Run(); err != nil {
fmt.Printf("Command failed with exit code %d\n", process.ProcessState.ExitCode())
fmt.Println(err)
}
}
You can then run the command:
$ ./say-hello Arawn Gywdion Sarah Hannah
And get back the expected
Hello, Arawn!
Hello, Gwydion!
Hello, Sarah!
Hello, Hannah!
It appears to be working correctly according to the outputs in your question.
A few suggestions:
It might be useful to print the command out as a string before running it, to check it's what you want.
You may find backticks useful when you have a string containing backslashes and quotation marks.
You have not supplied any format to fmt.Printf, hence the EXTRA in that output.
Using println to print the error will not stringify it, so use fmt.Printf for that too.
package main
import (
"fmt"
"os/exec"
)
func main() {
cmd := exec.Command("cmd", `C:\path\tools\util\Utility.exe C:\file_Location "select * from TABLE" C:\output_path\output.txt`)
fmt.Printf("%s\n", cmd.String())
out, err := cmd.Output()
fmt.Printf("%s\n", string(out))
if err != nil {
fmt.Printf(" Error running decomp %s\n", err)
}
}
Playground: https://go.dev/play/p/3t0aOxAZRtU

exec.command - CMD.exe - Query by command line

I am with my first project. It is a small program written in GO that will run on Windows. This should make a query to free software, through a command line in the CMD.
So far I have managed to run the cmd.exe and position the terminal in the program path, but I cannot get the command line to be inserted in order to make the query. I can't find the instruction for it.
I don't know if what I'm missing is to write the query well -according to Windows- or is it a matter of better articulating the programming in Go.
I ask if you can help me. Thank you!
This is the code:
package main
import (
"log"
"os/exec"
)
func main() {
cmd := exec.Command(
"cmd.exe",
"/K",
"start",
)
cmd.Dir = "C:/sweph"
//command line to call program: swetest64 -p2 -b1.12.1900 -n15 -s2
err := cmd.Start()
if err != nil {
log.Fatal(err)
}
}
cmd := exec.Command("cmd", "/c", "swetest64 -p2 -b1.12.1900 -n15 -s2")
err := cmd.Run()
if err != nil {
log.Fatalf("run error: %v\n", err)
}
I think the code is absolutely fine. The program "swetest64 -p2 -b1.12.1900 -n15 -s2" which you are calling is returning a nonzero exit code.
Any nonzero exit code is treated as an error that is printed after cmd.Run() call.
So there is no problem with your code. Check the program you are calling.

Running os.execute to get the output

Im using the following code which run command against binary and need to provide output
if I run the command ftr get apps in the in my mac I got
[app1 apps2]
Now I copy the binary to the test data folder
and run the code below and I want to get the apps, currenlty there is no error but Im not getting also the data, what could be missing here?
Cmd := exec.Command("ftr", "get", "apps")
Cmd.Dir = "./testdata/"
err := Cmd.Start()
fmt.Println(err)
bytes, e := Cmd.Output()
fmt.Println(bytes, e)
You won't directly have an output, since the commands takes some time before it writes in stdout/stderr, so you need to basically wait for something to come out.
The way you can do it is by using bufio.NewScanner, like this:
package main
import (
"bufio"
"fmt"
"os/exec"
"strings"
)
func main() {
args := "get apps"
cmd := exec.Command("ftr", strings.Split(args, " ")...)
cmd.Dir = "./testdata/"
stdout, _ := cmd.StdoutPipe()
cmd.Start()
scanner := bufio.NewScanner(stdout)
scanner.Split(bufio.ScanWords)
for scanner.Scan() {
m := scanner.Text()
fmt.Println(m)
}
cmd.Wait()
}
If your command writes on stderr instead of stdout, you might need to use cmd.StderrPipe instead of cmd.Stdoutpipe in order to get the output.
Calling both Start and Output is redundant.
Output runs the command and returns its standard output.
Start is used to run the command asynchronously. Simply remove that call:
cmd := exec.Command("ftr", "get", "apps")
cmd.Dir = "./testdata/"
b, err := cmd.Output()
fmt.Println(string(b), err)

Go command output by default to stdout?

I started learning and playing around with Go to see what it is like to make some more complex console/cli type tools instead of using shells or Python. I want to execute commands and display the output. I figured out how to print the output like this:
out, err := exec.Command("pwd").Output()
print(string(out))
Is there a way to execute the commands and have it default to stdout like a shell script, or do I need to make a helper function for this?
Update: After getting IntelliJ and the Go plugin, I poked around in the Go source and agree there is currently no way to do with without a helper method.
It is not possible to reuse a Cmd object as per this comment in the exec.go source code:
// A Cmd cannot be reused after calling its Run, Output or CombinedOutput
// methods.
I did incorporate the stdout option into my own helper, including other options like shell integration. I will try turn that into open source if I can make it useful. An interesting first day of Go.
The solution
Actually, it is pretty easy. You can set the stdout of the command to os.Stdout and Bob's your uncle:
package main
import (
"os"
"os/exec"
)
func main() {
cmd := exec.Command("pwd")
cmd.Stdout = os.Stdout
err := cmd.Run()
if err != nil {
panic(err)
}
}
What's happening here?
By default, the output of a command is stored in a bytes.Buffer if cmd.Stdout is not set to another io.Writer. The call of cmd.Output() then runs the command and saves the output to said buffer.
Since os.Stdout implements io.Writer interface, we simply set cmd.Stdout to be os.Stdout. Now when .Run() is called, the output of the command gets written to the io.Writer defined in cmd.Stdout, which happens to be os.Stdout and the output gets written in the shell.
EDIT: As per comment, if all commands should write to os.Stdout, there of course is no way to prevent some helper. I'd do it like this:
package main
import (
"os"
"os/exec"
)
func CmdToStdout( c string ) (err error){
cmd := exec.Command(c)
cmd.Stdout = os.Stdout
err = cmd.Run()
return
}
func main() {
err := CmdToStdout("pwd")
if err != nil {
panic(err)
}
}
You have to create a helper if you need this often (and 5 lines looks too much). Based on the documentation this is a recommended way:
package main
import (
"fmt"
"log"
"os/exec"
)
func main() {
out, err := exec.Command("date").Output()
if err != nil {
log.Fatal(err)
}
fmt.Printf("The date is %s\n", out)
}

Go: execute bash script

How do I execute a bash script from my Go program? Here's my code:
Dir Structure:
/hello/
public/
js/
hello.js
templates
hello.html
hello.go
hello.sh
hello.go
cmd, err := exec.Command("/bin/sh", "hello.sh")
if err != nil {
fmt.Println(err)
}
When I run hello.go and call the relevant route, I get this on my console:
exit status 127
output is
I'm expecting ["a", "b", "c"]
I am aware there is a similar question on SO: Executing a Bash Script from Golang, however, I'm not sure if I'm getting the path correct. Will appreciate help!
exec.Command() returns a struct that can be used for other commands like Run
If you're only looking for the output of the command try this:
package main
import (
"fmt"
"log"
"os/exec"
)
func main() {
out, err := exec.Command("date").Output()
if err != nil {
log.Fatal(err)
}
fmt.Printf("The date is %s\n", out)
}
You can also use CombinedOutput() instead of Output(). It will dump standard error result of executed command instead of just returning error code.
See:
How to debug "exit status 1" error when running exec.Command in Golang
Check the example at http://golang.org/pkg/os/exec/#Command
You can try by using an output buffer and assigning it to the Stdout of the cmd you create, as follows:
var out bytes.Buffer
cmd.Stdout = &out
You can then run the command using
cmd.Run()
If this executes fine (meaning it returns nil), the output of the command will be in the out buffer, the string version of which can be obtained with
out.String()

Resources