2
1

More than 5 years have passed since last update.

二重引用符などで面倒なことにならない cmd.exe の呼び方

Last updated at Posted at 2018-09-12

環境変数を使おう

system.go
package dos

import (
    "os"
    "os/exec"
)

func System(cmdline string) error {
    const CMDVAR = "CMDVAR"

    orgcmdarg := os.Getenv(CMDVAR)
    defer os.Setenv(CMDVAR, orgcmdarg)

    os.Setenv(CMDVAR, cmdline)

    cmd1 := exec.Command("cmd.exe", "/c", "%"+CMDVAR+"%")
    cmd1.Stdout = os.Stdout
    cmd1.Stderr = os.Stderr
    cmd1.Stdin = os.Stdin
    return cmd1.Run()
}
system_test.go
package dos

import (
    "testing"
)

func TestSystem(t *testing.T) {
    err := System("echo \"ahaha ihihi\"")
    if err != nil {
        t.Fatalf("%s\n", err.Error())
    }
}
$ go test -v
=== RUN   TestSystem
"ahaha ihihi"

※テストがおかしいのは分かっているので、つっこまないでくださいw

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1