Difference between revisions of "Golang exec.command"

From UVOO Tech Wiki
Jump to navigation Jump to search
Line 10: Line 10:
 
     "runtime"
 
     "runtime"
 
)
 
)
 +
  
 
func shell_cmd(name string, args []string) {
 
func shell_cmd(name string, args []string) {
Line 18: Line 19:
 
}
 
}
 
fmt.Println(string(out))
 
fmt.Println(string(out))
 +
}
 +
 +
 +
func shellcmd() {
 +
system_os := runtime.GOOS
 +
if len(os.Args) < 3 {
 +
fmt.Printf("Usage: %v <cmd> <args>\n", os.Args[0])
 +
fmt.Printf("Example: %v ls -lhat\n", os.Args[0])
 +
os.Exit(3)
 +
}
 +
 +
if system_os == "linux" {
 +
name := os.Args[1]
 +
args := os.Args[2:]
 +
shell_cmd(name, args)
 +
}else if system_os == "windows" {
 +
var name string = "powershell.exe"
 +
args := os.Args[2:]
 +
shell_cmd(name, args)
 +
}else{
 +
  fmt.Printf("OS %v not supported.\n", system_os)
 +
}
 
}
 
}
  
Line 24: Line 47:
 
if len(os.Args) < 2 {
 
if len(os.Args) < 2 {
 
fmt.Printf("Usage: %v <fuction>\n", os.Args[0])
 
fmt.Printf("Usage: %v <fuction>\n", os.Args[0])
fmt.Printf("Example: %v rcmd ls -lhat\n", os.Args[0])
+
fmt.Printf("Example: %v shellcmd ls -lhat\n", os.Args[0])
 
os.Exit(3)
 
os.Exit(3)
 
  }
 
  }
 
  function := os.Args[1]
 
  function := os.Args[1]
  system_os := runtime.GOOS
+
if function == "shellcmd" {
    if system_os == "linux" && function == "rcmd" {
+
shellcmd()
    name := os.Args[1]
+
}else{
    args := os.Args[2:]
+
fmt.Printf("E: Unsupported function %v\n", function)
        shell_cmd(name, args)
+
}
    }else if system_os == "windows" && function == "rcmd" {
 
    if len(os.Args) < 3 {
 
    fmt.Printf("Usage: %v <cmd> <args>\n", os.Args[0])
 
    fmt.Printf("Example: %v ls -lhat\n", os.Args[0])
 
    os.Exit(3)
 
}
 
    var name string = "powershell.exe"
 
    args := os.Args[2:]
 
    shell_cmd(name, args)
 
  }else{
 
  fmt.Printf("OS %v not supported.\n", system_os)
 
  }
 
 
}
 
}
 
```
 
```

Revision as of 00:52, 16 November 2020

Windows and Linux

package main

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


func shell_cmd(name string, args []string) {
    cmd := exec.Command(name, args...)
    out, err := cmd.Output()
    if err != nil {
      fmt.Println(err)
    }
    fmt.Println(string(out))
}


func shellcmd() {
    system_os := runtime.GOOS
    if len(os.Args) < 3 {
        fmt.Printf("Usage: %v <cmd> <args>\n", os.Args[0])
        fmt.Printf("Example: %v ls -lhat\n", os.Args[0])
        os.Exit(3)
    }

    if system_os == "linux" {
        name := os.Args[1]
        args := os.Args[2:]
        shell_cmd(name, args)
    }else if system_os == "windows" {
        var name string = "powershell.exe"
        args := os.Args[2:]
        shell_cmd(name, args)
    }else{
        fmt.Printf("OS %v not supported.\n", system_os)
    }
}


func main() {
    if len(os.Args) < 2 {
        fmt.Printf("Usage: %v <fuction>\n", os.Args[0])
        fmt.Printf("Example: %v shellcmd ls -lhat\n", os.Args[0])
        os.Exit(3)
      }
      function := os.Args[1]
    if function == "shellcmd" {
        shellcmd()
    }else{
        fmt.Printf("E: Unsupported function %v\n", function)
    }
}

Windows and Linux 2

package main

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


func linux_cmd() {
  if len(os.Args) < 2 {
    fmt.Printf("Usage: %v <cmd> <args>\n", os.Args[0])
    fmt.Printf("Example: %v ls -lhat\n", os.Args[0])
    os.Exit(3)
  }
  name := os.Args[1]
  args := os.Args[2:]
  cmd := exec.Command(name, args...)
  out, err := cmd.Output()
  if err != nil {
    fmt.Println(err)
  }
  fmt.Println(string(out))
}


func win_cmd() {
  if len(os.Args) < 2 {
    fmt.Printf("Usage: %v <cmd> <args>\n", os.Args[0])
    fmt.Printf("Example: %v ls -lhat\n", os.Args[0])
    os.Exit(3)
  }
  // name := os.Args[1]
  name := "powershell.exe"
  args := os.Args[1:]
  cmd := exec.Command(name, args...)
  out, err := cmd.Output()
  if err != nil {
    fmt.Println(err)
  }
  fmt.Println(string(out))
}

func main() {
  os := runtime.GOOS
  //var os string = runtime.GOOS
  if os == "linux" {
    linux_cmd()
  }else if os == "windows" {
    fmt.Printf("OS %v is supported.", os)
    win_cmd()
  }else{
      fmt.Printf("OS %v not supported.", os)
  }
}

Windows

package main

import(
    "fmt"
    "os/exec"
    "os"
    "log"
)


func pscmd(s string){
    args := []string{s}
    // cmd := exec.Command("powershell.exe", "ls", "-Depth", "1")
    cmd := exec.Command("powershell.exe", args...)

    cmd.Stdout = os.Stdout
    cmd.Stderr = os.Stderr
    err := cmd.Run()
    if err != nil {
        log.Fatalf("cmd.Run() failed with %s\n", err)
    }
    fmt.Println("======")
}

func main(){   

    pscmd("ls -Depth 1")
    pscmd("echo 'hi there'")

}