drgmszo076956 2016-09-24 10:06
浏览 108
已采纳

如何在FreeBSD上向os.StartProcess添加参数?

I'm trying to use os.StartProcess to start a process with arguments on a FreeBSD machine. After trying several different ways of adding the correct arguments and always getting errors I have set up this simple proof of concept.

package main

import (
    "log"
    "os"
)

func main() {
    command := "/usr/local/sbin/pkg"
    args := []string{"install"}

    procAttr := new(os.ProcAttr)
    procAttr.Files = []*os.File{os.Stdin, os.Stdout, os.Stderr}
    if process, err := os.StartProcess(command, args, procAttr); err != nil {
        log.Println(err.Error())
    } else {
        log.Printf("%d", process.Pid)
    }
}

The idea here is that if the system starts just pkg it will complain about missing arguments, but if it starts pkg install it will suggest the help for the install command. See below:

pkg: not enough arguments
Usage: pkg [-v] [-d] [-l] [-N] [-j <jail name or id>|-c <chroot path>|-r <rootdir>] [-C <configuration file>] [-R <repo config dir>] [-o var=value] [-4|-6] <command> [<args>]

For more information on available commands and options see 'pkg help'.

Or

Usage: pkg install [-AfInFMqRUy] [-r reponame] [-Cgix] <pkg-name> ...

For more information see 'pkg help install'.

When I use the go code above it will complaint about missing arguments, so why is my argument 'install' not added to the command, and how to correct my code?

  • 写回答

1条回答 默认 最新

  • duanli0687 2016-09-25 18:30
    关注

    The first element of args is the process name. args[1] is the actual first argument. This is just like the os.Args (and argv in C) lists work. To avoid dealing with these low level details you should just use the os/exec package to launch external processes.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试,帮帮忙吧
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建