关于args和从position [1](不是position 0)传递args的问题

I am reading a commandline string from a config file (config.json) :

"execmd" : "c:\\windows\\system32\\cmd.exe /c runscript.cmd"

I want to pass this to exec.Command() - but this function requires 2 parameters:

exec.Comm*emphasized text*and (cmd, args...)

Where cmd is the first segment (cmd.exe) and args would then be every space deliminated value thereafter.

  1. I am not exactly sure if I need to read the config string, and then manually split it up in an array for each space deliminator? Is there any way of converting a string into args easily?

  2. How would it be possible to do something like this, where I can refer args... from an index? (the below code doesn't work, cant refer args this way)

    exec.Command (arg[0], args[1]...)

If the values coming it from the config file are in a format executable by shell, you're going to run into a host of problems just splitting on spaces (e.g. quoted arguments containing spaces). If you want to take in a command line that would be executable in a shell, you're going to want to have a shell execute it:

exec.Command("cmd.exe", "/c", execmd)

There is no way of "converting a string into args" because it varies from shell to shell.

I found this thread regarding the issue : https://groups.google.com/forum/#!topic/golang-nuts/pNwqLyfl2co

Edit 1: ( didnt work )

And saw this one using RegExp - and found out how i can index into the args also :

r := regexp.MustCompile("'.+'|\".+\"|\\S+")
s := appConfig.JobExec.ExecProcess
m := r.FindAllString(s, -1)
Exec.Command (m[0],m[1:])

This seems to work also with quoted strings !

Edit 2:

It didnt work and didnt work on windows, theres an issue with params being passed on when running with cmd. Using sysprocattr.cmdline will make it work :

cmdins := exec.Command(cmd)
cmdins.SysProcAttr = &syscall.SysProcAttr{}
for _, arg := range args {
    cmdins.SysProcAttr.CmdLine = cmdins.SysProcAttr.CmdLine + arg + " "
}

the issue described here :

https://github.com/golang/go/issues/17149