executeScript function

This commit is contained in:
yo 2022-06-19 17:43:48 +02:00
parent d9e1e20afc
commit 4d8bf6e0d5

View File

@ -13,6 +13,7 @@ import (
"strconv"
"strings"
"io/ioutil"
"github.com/google/shlex"
"github.com/c2h5oh/datasize"
)
@ -216,6 +217,39 @@ func executeCommandInJail(jail *Jail, cmdline string) (string, error) {
return string(out), err
}
/********************************************************************************
* Execute a script, or shell command. Need to double escape special characters
* Ex.: executeScript("echo '\\\\_o\\< \\~ COINCOIN' > /tmp/coincoin.txt")
*******************************************************************************/
func executeScript(script string) (string, error) {
var cmd []string
var out []byte
var err error
if gUseSudo {
cmd = append(cmd, "sudo")
}
cmd = append(cmd, []string{"/bin/sh", "-c"}...)
s, err := shlex.Split(script)
if err != nil {
return "", err
}
cmd = append(cmd, strings.Join(s, " "))
//fmt.Printf("DEBUG: Prepare to execute %v\n", cmd)
if len(cmd) > 1 {
out, err = exec.Command(cmd[0], cmd[1:]...).CombinedOutput()
} else {
out, err = exec.Command(cmd[0]).CombinedOutput()
}
return string(out), err
}
/*****************************************************************************
*
* ZFS datasets/pools operations