exec.Command return error if command is not in PATH

2,249 views
Skip to first unread message

Archos

unread,
Aug 25, 2012, 6:09:20 AM8/25/12
to golan...@googlegroups.com
There is a failure in exec.Command because it returns an error when a command is not found in the path so you can not run aliases defined in your Unix system neither run typical command in windows systems like "copy".

package main

import (
    "log"
    "os/exec"
)

func main() {
    err := exec.Command("ll").Run()
    if err != nil {
        log.Fatal(err)
    }
}

exec: "ll": executable file not found in $PATH
exit status 1

* * *

$ alias | grep "ll="
alias ll='ls -alF'

In Windows system, you can not run the basic commands:

package main

import (
    "log"
    "os/exec"
)

func main() {
    err := exec.Command("copy", "/?").Run()
    if err != nil {
        log.Fatal(err)
    }
}

exec: "copy": executable file not found in %PATH%

Archos

unread,
Aug 25, 2012, 6:37:34 AM8/25/12
to golan...@googlegroups.com
I don't think that it's a problem in Unix systems since the shell is who must find if the command to run is an alias. In change, it's a true problem in Windows systems where you can not execute a simple "dir".

Jan Mercl

unread,
Aug 25, 2012, 6:55:42 AM8/25/12
to Archos, golan...@googlegroups.com
On Sat, Aug 25, 2012 at 12:37 PM, Archos <raul...@sent.com> wrote:
> I don't think that it's a problem in Unix systems since the shell is who
> must find if the command to run is an alias. In change, it's a true problem
> in Windows systems where you can not execute a simple "dir".

exec.Command.Run runs (executes) commands, i.e. runnable/executable
binaries. AFAIK, dir is not an executable binary in Windows. That's
not exec.Command's fault.

-j

minux

unread,
Aug 25, 2012, 6:56:27 AM8/25/12
to Archos, golan...@googlegroups.com
On Sat, Aug 25, 2012 at 6:09 PM, Archos <raul...@sent.com> wrote:
There is a failure in exec.Command because it returns an error when a command is not found in the path so you can not run aliases defined in your Unix system neither run typical command in windows systems like "copy".
In Windows system, you can not run the basic commands:

package main

import (
    "log"
    "os/exec"
)

func main() {
    err := exec.Command("copy", "/?").Run()
"cmd /c copy /?" 

DisposaBoy

unread,
Aug 25, 2012, 7:31:15 AM8/25/12
to golan...@googlegroups.com
You need to learn how shells work in general but I'll be helpful and tell you that if you need features provided by your shell, then you need to execute the shell. On windows, copy is a shell command(unless you installed cygwin et al. or you actually have such a binary in your %PATH%). Likewise, if you have an alias and you want to execute it, then you  need to call your shell. Note also that an alias in bash doen't necessarily work in zsh, fish etc.


 

Archos

unread,
Aug 25, 2012, 7:39:16 AM8/25/12
to golan...@googlegroups.com

El sábado, 25 de agosto de 2012 12:31:15 UTC+1, DisposaBoy escribió:
You need to learn how shells work in general but I'll be helpful and tell you that if you need features provided by your shell, then you need to execute the shell. On windows, copy is a shell command(unless you installed cygwin et al. or you actually have such a binary in your %PATH%). Likewise, if you have an alias and you want to execute it, then you  need to call your shell. Note also that an alias in bash doen't necessarily work in zsh, fish etc.
 I'm forced to it since in a little time I'll start to build a shell (a minimal shell) that works in all systems where Go works.

Miki Tebeka

unread,
Aug 25, 2012, 11:07:20 AM8/25/12
to golan...@googlegroups.com
> I'm forced to it since in a little time I'll start to build a shell (a minimal shell) that works in all systems where Go works.
https://github.com/pclouds/busybox-w32 ?
Reply all
Reply to author
Forward
0 new messages