exec.Command(“date”)无法找到日期命令 [英] exec.Command("date") not able to find date command

查看:386
本文介绍了exec.Command(“date”)无法找到日期命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  package main 

import(
fmt
log
os / exec


func main(){
out,err:= exec.Command(date)。Output()
if err!= nil {
log.Fatal(err)
}
fmt.Printf(日期是%s\\\
,out)
}

这是执行系统命令的文档中的代码示例。 http://golang.org/pkg/os/exec/#example_Cmd_Output 即使在文档网站示例执行框不会运行,并具有相同的错误:
2009/11/10 23:00:00 exec:date:$ PATH中找不到可执行文件

在Windows上,我得到:
exec:date:在%PATH%找不到可执行文件

如何获得命令的工作?我需要设置一个路径或写出命令的完整路径吗?

解决方案

可悲的是,这个例子不会为你工作,除非你从某处(Cygwin?)得到一个 date.exe 并放入你的%PATH%



我认为, date 是PowerShell中的内建。它适用于你,因为你的shell正在解释它。



你也许可以这样做

  out,err:= exec.Command(cmd,/ C,date)。Output()

建议这里;我不知道,我没有一台Windows机器。






旁注:


Get-Command日期表示术语'date'未被识别为cmdlet的名称


该短语恰好有两个Google结果。其中一人导致我,这帮助我弄清楚。


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)
}

This is a code example from the documentation for executing system commands. http://golang.org/pkg/os/exec/#example_Cmd_Output Even on the documentation site the example execute box doesn't run and has the same error: 2009/11/10 23:00:00 exec: "date": executable file not found in $PATH

On Windows I get: exec: "date": executable file not found in %PATH%

How do I get commands to work? Do I need to set a path or write out the full path of the command?

解决方案

Sadly, that example isn't going to work for you unless you get a date.exe from somewhere (Cygwin?) and put in on your %PATH%.

What's going on, I believe, is that date is a builtin in Powershell. It works for you because your shell is interpreting it.

You may be able to do

out, err := exec.Command("cmd", "/C", "date").Output()

as suggested here; I don't know, I don't have a Windows machine handy.


Sidenote:

Get-Command date says "The term 'date' is not recognized as the name of a cmdlet"

There are exactly two Google results for that phrase. One of them leads me to this, which helped me figure this out.

这篇关于exec.Command(“date”)无法找到日期命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆