您的位置:首页 > 博客中心 > 电脑问题 >

golang执行Linux和Windows命令

时间:2022-03-18 14:50

1. 可接收变参命令

package main
import (
    "fmt"   
    "os"
    "os/exec"
    "strings"
)

func main(){
    sliceFunc(os.Args[1:]...)
}

func sliceFunc(cmd... string){
    fmt.Println(cmd)
    if len(cmd) == 0 { 
        fmt.Printf("Usage: %s args...\n", os.Args[0])
        os.Exit(-1)
    }   
    fmt.Println(cmdFunc(cmd...))
}

func cmdFunc(cmd... string) string {
    fmt.Printf("cmd slice len: %d, value:%v\n", len(cmd),  cmd)
    result, err := exec.Command(cmd[0], cmd[1:]...).Output()
    if err != nil {
        fmt.Println("Command failed:", err.Error())
    }   

//  return string(result)  // with ‘\n‘
    return strings.TrimSpace(string(result))
}

执行结果:

$ go run slice.go uname -a
[uname -a]
cmd slice len: 2, value:[uname -a]
Linux e531 5.0.0-29-generic #31~18.04.1-Ubuntu SMP Thu Sep 12 18:29:21 UTC 2019 x86_64 x86_64 x86_64 GNU/Linux
$ go run slice.go uname 
[uname]
cmd slice len: 1, value:[uname]
Linux

2. Linux和Windows命令

只接收单个命令并执行:

package main

import (
    "fmt"
    "errors"
    "os"
    "os/exec"
    "runtime"
    "strings"
)

func main(){
    if len(os.Args) == 1{
        fmt.Printf("Usage: %s args...\n", os.Args[0])
        os.Exit(-1)
    }
    str1, err := RunCommandWithErr(os.Args[1])
    if err != nil {
        fmt.Println(err.Error())
    } else {
        fmt.Println(str1)    
    }

    str := RunCommand(os.Args[1])    
    fmt.Println(str)    
}

func runInLinux(cmd string) string{
    fmt.Println("Running Linux cmd:" , cmd)
    result, err := exec.Command("/bin/sh", "-c", cmd).Output()
    if err != nil {
        fmt.Println(err.Error())
    }
    return strings.TrimSpace(string(result))
}

func runInWindows(cmd string) string{
    fmt.Println("Running Win cmd:", cmd)
    result, err := exec.Command("cmd", "/c", cmd).Output()
    if err != nil {
        fmt.Println(err.Error())
    }
    return strings.TrimSpace(string(result))
}

func RunCommand(cmd string) string{
    if runtime.GOOS == "windows" {
        return runInWindows(cmd)
    } else {
        return runInLinux(cmd)
    }
}

func RunLinuxCommand(cmd string) string{
    if runtime.GOOS == "windows" {
        return ""
    } else {
        return runInLinux(cmd)
    }
}

func runInLinuxWithErr(cmd string) (string, error) {
    fmt.Println("Running Linux cmd:"+cmd)
    result, err := exec.Command("/bin/sh", "-c", cmd).Output()
    if err != nil {
        fmt.Println(err.Error())
    }
    return strings.TrimSpace(string(result)), err
}

func runInWindowsWithErr(cmd string) (string, error){
    fmt.Println("Running Windows cmd:"+cmd)
    result, err := exec.Command("cmd", "/c", cmd).Output()
    if err != nil {
        fmt.Println(err.Error())
    }
    return strings.TrimSpace(string(result)), err
}

func RunCommandWithErr(cmd string) (string, error){
    if runtime.GOOS == "windows" {
        return runInWindowsWithErr(cmd)
    } else {
        return runInLinuxWithErr(cmd)
    }
}

func RunLinuxCommandWithErr(cmd string)(string, error){
    if runtime.GOOS == "windows" {
        return "", errors.New("could not run in Windows Os") 
    } else {
        return runInLinuxWithErr(cmd)
    }
}

执行结果:

$ go run cmd.go uname
Running Linux cmd:uname
Linux
Running Linux cmd: uname
Linux
$ go run cmd.go uname -a
Running Linux cmd:uname
Linux
Running Linux cmd: uname
Linux

 

参考:

1.

热门排行

今日推荐

热门手游