How to execute system command with unknown arguments?

I found a relatively decent way to achieve the same .

out, err := exec.Command("sh","-c",cmd).Output()

Works for me until now. Still finding better ways to achieve the same.

Edit1:

Finally a easier and efficient (atleast so far) way to do would be like this

func exeCmd(cmd string, wg *sync.WaitGroup) {
  fmt.Println("command is ",cmd)
  // splitting head => g++ parts => rest of the command
  parts := strings.Fields(cmd)
  head := parts[0]
  parts = parts[1:len(parts)]

  out, err := exec.Command(head,parts...).Output()
  if err != nil {
    fmt.Printf("%s", err)
  }
  fmt.Printf("%s", out)
  wg.Done() // Need to signal to waitgroup that this goroutine is done
}

Thanks to variadic arguments in go and people that pointed that out to me 🙂

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)