Skip to content

Instantly share code, notes, and snippets.

@smallnest
Created October 17, 2018 11:17
Show Gist options
  • Select an option

  • Save smallnest/c3b7f6f0e814e1825be96789ea958215 to your computer and use it in GitHub Desktop.

Select an option

Save smallnest/c3b7f6f0e814e1825be96789ea958215 to your computer and use it in GitHub Desktop.
在exec使用linux管道
out, err := exec.Command("bash", "-c", "ps cax | grep myapp").Output()
@smallnest

Copy link
Copy Markdown
Author

When you type the command at the shell, the shell takes ./source_dir/* and replaces it with a list of all of the files that match, one per argument. The mv command sees a list of filenames, not a wildcard.

What you need to do is either do the same thing yourself (using filepath.Glob which returns a []string of matching files), or to invoke the shell so that it can do the work (using exec.Command("/bin/sh", "-c", "mv ./source_dir/* ./dest_dir")).

https://stackoverflow.com/questions/41037870/go-exec-command-run-command-which-contains-pipe

https://stackoverflow.com/questions/31467153/golang-failed-exec-command-that-works-in-terminal

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment