• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Golang exec.Cmd类代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Golang中exec.Cmd的典型用法代码示例。如果您正苦于以下问题:Golang Cmd类的具体用法?Golang Cmd怎么用?Golang Cmd使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。



在下文中一共展示了Cmd类的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Golang代码示例。

示例1: RunExternalDump

func RunExternalDump(cmd, wd string, argv []string, dump io.Writer) (err os.Error) {
	var p *exec.Cmd
	p, err = exec.Run(cmd, argv, nil, wd, exec.PassThrough, exec.Pipe, exec.PassThrough)
	if err != nil {
		return
	}
	if p != nil {
		src := p.Stdout
		buffer := make([]byte, 1024)
		for {
			n, cpErr := src.Read(buffer)
			if cpErr != nil {
				break
			}
			_, cpErr = dump.Write(buffer[0:n])
			if cpErr != nil {
				break
			}
		}

		var wmsg *os.Waitmsg
		wmsg, err = p.Wait(0)
		if wmsg.ExitStatus() != 0 {
			err = os.NewError(fmt.Sprintf("%v: %s\n", argv, wmsg.String()))
			return
		}
		if err != nil {
			return
		}
	}
	return
}
开发者ID:clarencethecat,项目名称:gbide,代码行数:32,代码来源:gbrun.go


示例2: build

func build(gcfile string, opts []string, proFileName string, files []string, envv []string, dir string) (status syscall.WaitStatus, err os.Error) {
	arg := []string{gcfile, "-o", proFileName}
	for _, v := range opts {
		arg = append(arg, v)
	}
	for _, v := range files {
		arg = append(arg, string(v))
	}
	fmt.Println("\t", arg)
	var cmd *exec.Cmd
	cmd, err = exec.Run(gcfile, arg[:], envv[:], dir, 0, 1, 2)
	if err != nil {
		fmt.Printf("Error, %s", err)
		return
	}
	defer cmd.Close()
	var wait *os.Waitmsg
	wait, err = cmd.Wait(0)
	if err != nil {
		fmt.Printf("Error, %s", err)
		return
	}
	status = wait.WaitStatus
	return
}
开发者ID:gaopeng1106,项目名称:liteide.liteide,代码行数:25,代码来源:promake.go


示例3: RunWithEnvAndWd

func RunWithEnvAndWd(command string, args []string, env []string, wd string) (proc *exec.Cmd, err os.Error) {
	//log.Println(command, args)
	//hho := exec.PassThrough
	args = prepend(args, command)
	env = mergeEnv(os.Environ(), env)

	binpath, err := findCmd(findEnv(env, "PATH"), command)
	if err != nil {
		return nil, err
	}

	cmd := new(exec.Cmd)
	cmd.Path = binpath
	cmd.Args = args
	cmd.Env = env
	cmd.Dir = wd
	cmd.Stderr = os.Stderr
	cmd.Stdout = os.Stdout

	err = cmd.Start()
	if err != nil {
		log.Print("Error running command ", command, ": ", err, "\n")
		return nil, err
	}
	return cmd, nil
}
开发者ID:Epictetus,项目名称:wfdr,代码行数:26,代码来源:osutil.go


示例4: RunExternalAndStdout

func RunExternalAndStdout(cmd, wd string, argv []string) (stdout []byte, err os.Error) {
	argv = SplitArgs(argv)
	var c *exec.Cmd
	if len(argv) > 0 {
		c = exec.Command(cmd, argv...)
	} else {
		c = exec.Command(cmd)
	}
	c.Dir = wd
	c.Env = os.Environ()

	bStdout := bytes.NewBuffer(nil)
	bStderr := bytes.NewBuffer(nil)
	c.Stdout = bStdout
	c.Stderr = bStderr

	err = c.Run()

	if wmsg, ok := err.(*os.Waitmsg); ok {
		if wmsg.ExitStatus() != 0 {
			err = os.NewError(fmt.Sprintf("command: %s %v\n%s\n", cmd, argv, bStderr.String()))
		} else {
			err = nil
		}
	} else {
		stdout = bStdout.Bytes()
	}
	return
}
开发者ID:wb253,项目名称:go-gb,代码行数:29,代码来源:runext.go


示例5: pack

func pack(pkfile string, target string, ofile string, envv []string, dir string) (status syscall.WaitStatus, err os.Error) {
	arg := []string{pkfile, "grc", target, ofile}
	fmt.Println("\t", arg)
	var cmd *exec.Cmd
	cmd, err = exec.Run(pkfile, arg[:], envv[:], dir, 0, 1, 2)
	if err != nil {
		fmt.Printf("Error, %s", err)
		return
	}
	defer cmd.Close()
	var wait *os.Waitmsg
	wait, err = cmd.Wait(0)
	if err != nil {
		fmt.Printf("Error, %s", err)
		return
	}
	status = wait.WaitStatus
	return
}
开发者ID:gaopeng1106,项目名称:liteide.liteide,代码行数:19,代码来源:promake.go


示例6: execute

/* Execute command coming from inch channel */
func execute(inch chan *command) {
	var c *command
	var cmd *exec.Cmd
	var err os.Error

	var argv0, dir string
	var argv []string

	for {
		c = <-inch

		dir = os.Getenv("PWD")

		switch c.cmdtype {
		case BUILTIN:
			cb := BUILTIN_COMMANDS[c.name]
			LAST_RETURN_VALUE = cb(c.args)
		case EXTERNAL:
			if argv0, err = exec.LookPath(c.name); err != nil {
				goto Error
			}

			if len(c.args) > 0 {
				argv = c.args
			}
			envv := os.Environ()

			cmd, err = exec.Run(argv0, argv, envv, dir,
				exec.PassThrough, exec.PassThrough, exec.PassThrough)

			err = cmd.Close()
			//TODO update LAST_RETURN_VALUE

		Error:
			if err != nil {
				error(err.String())
			}
		}
		inch <- nil //release blockage in main loop()
	}
}
开发者ID:igoralmeida,项目名称:gosh,代码行数:42,代码来源:execution.go


示例7: link

func link(glfile string, opts []string, target string, ofile string, envv []string, dir string) (status syscall.WaitStatus, err os.Error) {
	arg := []string{glfile, "-o", target}
	for _, v := range opts {
		arg = append(arg, v)
	}
	arg = append(arg, ofile)
	fmt.Println("\t", arg)
	var cmd *exec.Cmd
	cmd, err = exec.Run(glfile, arg[:], envv[:], dir, 0, 1, 2)
	if err != nil {
		fmt.Printf("Error, %s", err)
		return
	}
	defer cmd.Close()
	var wait *os.Waitmsg
	wait, err = cmd.Wait(0)
	if err != nil {
		fmt.Printf("Error, %s", err)
		return
	}
	status = wait.WaitStatus
	return
}
开发者ID:gaopeng1106,项目名称:liteide.liteide,代码行数:23,代码来源:promake.go


示例8: run

// run runs the command argv, resolving argv[0] if necessary by searching $PATH.
// It provides input on standard input to the command.
func run(argv []string, input []byte) (out string, err os.Error) {
	if len(argv) < 1 {
		err = os.EINVAL
		goto Error
	}
	prog, ok := lookPathCache[argv[0]]
	if !ok {
		prog, err = exec.LookPath(argv[0])
		if err != nil {
			goto Error
		}
		lookPathCache[argv[0]] = prog
	}
	// fmt.Fprintf(os.Stderr, "%v\n", argv);
	var cmd *exec.Cmd
	if len(input) == 0 {
		cmd, err = exec.Run(prog, argv, os.Environ(), "", exec.DevNull, exec.Pipe, exec.MergeWithStdout)
		if err != nil {
			goto Error
		}
	} else {
		cmd, err = exec.Run(prog, argv, os.Environ(), "", exec.Pipe, exec.Pipe, exec.MergeWithStdout)
		if err != nil {
			goto Error
		}
		go func() {
			cmd.Stdin.Write(input)
			cmd.Stdin.Close()
		}()
	}
	defer cmd.Close()
	var buf bytes.Buffer
	_, err = io.Copy(&buf, cmd.Stdout)
	out = buf.String()
	if err != nil {
		cmd.Wait(0)
		goto Error
	}
	w, err := cmd.Wait(0)
	if err != nil {
		goto Error
	}
	if !w.Exited() || w.ExitStatus() != 0 {
		err = w
		goto Error
	}
	return

Error:
	err = &runError{copy(argv), err}
	return
}
开发者ID:ivanwyc,项目名称:google-go,代码行数:54,代码来源:main.go


示例9: StdExecve

func StdExecve(argv []string, stopOnTrouble bool) bool {

	var err os.Error
	var cmd *exec.Cmd

	switch len(argv) {
	case 0:
		if stopOnTrouble {
			log.Fatalf("[ERROR] len(argv) == 0\n")
		}
		return false
	case 1:
		cmd = exec.Command(argv[0])
	default:
		cmd = exec.Command(argv[0], argv[1:]...)
	}

	// pass-through
	cmd.Stdout = os.Stdout
	cmd.Stderr = os.Stderr
	cmd.Stdin = os.Stdin

	err = cmd.Start()

	if err != nil {
		if stopOnTrouble {
			log.Fatalf("[ERROR] %s\n", err)
		} else {
			log.Printf("[ERROR] %s\n", err)
			return false
		}
	}

	err = cmd.Wait()

	if err != nil {
		if stopOnTrouble {
			log.Fatalf("[ERROR] %s\n", err)
		} else {
			log.Printf("[ERROR] %s\n", err)
			return false
		}
	}

	return true
}
开发者ID:ssrl,项目名称:mgd,代码行数:46,代码来源:handy.go


示例10: run

func run(argv []string, stdin, stdout, stderr int) (out string, err os.Error) {

	if len(argv) < 1 {
		err = os.EINVAL
		goto Error
	}

	var cmd *exec.Cmd

	cmd, err = exec.Run(argv[0], argv, os.Environ(), "", stdin, stdout, stderr)

	if err != nil {
		goto Error
	}

	defer cmd.Close()

	var buf bytes.Buffer

	_, err = io.Copy(&buf, cmd.Stdout)
	out = buf.String()

	if err != nil {
		cmd.Wait(0)
		goto Error
	}

	w, err := cmd.Wait(0)

	if err != nil {
		goto Error
	}

	if !w.Exited() || w.ExitStatus() != 0 {
		err = w
		goto Error
	}

	return

Error:
	err = &runError{copy(argv), err}
	return
}
开发者ID:remogatto,项目名称:makengo,代码行数:44,代码来源:system.go


示例11: ProcessRequest

func ProcessRequest(request DownloadRequest) {
	var tempFile string = MakeTempFileName(request.url, ".flv")
	var tempFileSansExtension string
	{
		pathDotIndex := strings.LastIndex(tempFile, ".")
		tempFileSansExtension = tempFile[0:pathDotIndex]
	}

	var cmd *exec.Cmd = MakeCommand(&request, tempFile)

	if cmd == nil {
		request.WriteError("forming the download command", nil)
		return
	}

	var proc *StdoutProcessor
	if pipe, e := cmd.StdoutPipe(); e != nil {
		request.WriteError("setting youtube-dl pipe", e)
		return
	} else {
		proc = NewStdoutProcessor(&pipe)
	}

	if e := cmd.Start(); e != nil {
		request.WriteError("running youtube-dl", e)
		return
	}

	var state DownloadState = kStarting
	var data map[string]interface{}
	var e os.Error

	for {
		status, e := proc.ParseOneStatusMessage()
		if e != nil {
			if state < kExtractingAudio {
				request.WriteError("downloading the video", e)
				return
			} else {
				break
			}
		} else if status == nil {
			continue
		}

		if status.state != kDownloadingVideo && (status.state == 0 || status.state == state) {
			continue
		}

		fmt.Printf("Proc SM: %v\n", status)
		if status.state >= kDownloadingVideo && state != status.state {
			if data, e = ReadJson(tempFile + ".info.json"); e != nil {
				request.WriteError("reading video JSON", e)
				return
			}
			status.title = data["title"].(string)
		}
		state = status.state

		status.requestId = request.id
		request.statusChannel <- *status
	}

	var finalFileBaseName = data["title"].(string) + "." + *outputFormat
	var finalFile = request.basePath + "/" + finalFileBaseName
	if _, e := CopyFile(finalFile,
		tempFileSansExtension+"."+*outputFormat); e != nil {
		request.WriteError("tagging", e)
		return
	}

	request.statusChannel <- StatusMessage{request.id, kTagging, "", "", 0, 0, 0, nil, ""}
	var c = exec.Command("lltag", "--yes", "-G", "--rename=%a/%t", finalFileBaseName)
	c.Dir = request.basePath
	out, ioError := c.StdoutPipe()
	if ioError != nil {
		request.WriteError("starting the tagger", ioError)
		return
	}

	var in io.WriteCloser
	in, ioError = c.StdinPipe()
	if ioError != nil {
		request.WriteError("starting the tagger", ioError)
		return
	}

	var link string
	if e = c.Start(); e != nil {
		request.WriteError("tagging", e)
		return
	} else {
		in.Write([]byte(strings.Repeat("y\n", 20)))
		outData, e := ioutil.ReadAll(out)
		if e != nil {
			request.WriteError("reading the tagger output", e)
			return
		}
		match := regexp.MustCompile("New filename is '([^\n]*)'\n").FindStringSubmatch(string(outData))
		fmt.Printf("out data: %s\n\nmatch: %v\n", string(outData), match)
//.........这里部分代码省略.........
开发者ID:areusch,项目名称:ytd-server,代码行数:101,代码来源:worker.go


示例12: set_exec

/*
Set up a command to use os standard in/out/err
*/
func set_exec(c *exec.Cmd) {
	c.Stdout = os.Stdout
	c.Stdin = os.Stdin
	c.Stderr = os.Stderr
}
开发者ID:Altece,项目名称:Go-Go-Gadget-Repo,代码行数:8,代码来源:screen.go



注:本文中的exec.Cmd类示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Golang draw.Draw函数代码示例发布时间:2022-05-24
下一篇:
Golang exec.Run函数代码示例发布时间:2022-05-24
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap