在熟悉围棋和猩猩套路的过程中,我已经走上了挡路的道路,开始执行命令.这些命令的格式为:

sudo find /folder -type f | while read i; do sudo -S chmod 644 "$i"; done

对于取自How to execute system command in Golang with unknown arguments的代码,我试图执行此命令,但我相信由于第一个参数为sudo,它不会执行,我可能是错的.我只有两个问题.

当这些命令无法运行时,我将返回"退出状态1",有没有办法获得比我正在执行的操作更详细的错误?问题二,为什么我会用这个脚本获得"退出状态1"?发生了什么不该发生的事?

package main

import (
    "bufio"
    "fmt"
    "os"
    "os/exec"
    "strings"
    "sync"
)

func ExeCmd(cmd string, wg *sync.WaitGroup) {
    parts := strings.Fields(cmd)
    head := parts[0]
    // Head at this point is "sudo"
    parts = parts[1:len(parts)]
    out, err := exec.Command(head,parts...).Output()
    if err != nil {
        fmt.Printf("%s\n", err)
    }
    fmt.Printf("%s\n", out)
    wg.Done() // Signal to WaitGroup goroutine finished
}

func InArray(a []string, e string) bool {
    for _, x := range a {
        if x == e {
            return true
            fmt.Print("True")
        }
    }
    return false
}

func main() {
    exec.Command("sudo ls > /dev/null") // Get sudo password just once, doesn't seem to work with Go
    wg := new(sync.WaitGroup)
    reader := bufio.NewReader(os.Stdin)
    fdbslices := []string{"f", "d", "b", "files", "directories", "both"}
    commands := []string{}
    fdb := ""
    filep := ""
    dirp := ""

    // Grab Path
    fmt.Print("Path: ")
    findpath, _ := reader.ReadString('\n')
    findpath = strings.ToLower(strings.Trim(findpath, "\n"))

    // Grab Files, Directories, or Both
    for {
        fmt.Print("Files, Directories, or Both: ")
        fdb, _ = reader.ReadString('\n')
        fdb = strings.ToLower(strings.Trim(fdb, "\n"))
        if InArray(fdbslices, fdb) == true { break }
    }

    // Build commands string, these come out as they should
    for {
        if fdb == "f" || fdb == "files" {
            fmt.Print("Permissions for Files: ")
            filep, _ = reader.ReadString('\n')
            filep = strings.Trim(filep, "\n")
            commands = append(commands, "sudo find " + findpath + " -type f | while read i; do sudo -S chmod " + filep + " \"$i\"; done")
        }
        if fdb == "d" || fdb == "directories" {
            fmt.Print("Permissions for Directories: ")
            dirp, _ = reader.ReadString('\n')
            dirp = strings.Trim(dirp, "\n")
            commands = append(commands, "sudo find " + findpath + " -type d | while read i; do sudo -S chmod " + dirp + " \"$i\"; done")
        }
        if fdb == "b" || fdb == "both" {
            fmt.Print("Permissions for Files: ")
            filep, _ = reader.ReadString('\n')
            filep = strings.Trim(filep, "\n")
            commands = append(commands, "sudo find " + findpath + " -type f | while read i; do sudo -S chmod " + filep + " \"$i\"; done")
            fmt.Print("Permissions for Directories: ")
            dirp, _ = reader.ReadString('\n')
            dirp = strings.Trim(dirp, "\n")
            commands = append(commands, "sudo find " + findpath + " -type d | while read i; do sudo -S chmod " + dirp + " \"$i\"; done")
        }
        break
    }

    // Execute Commands
    for _, str := range commands {
        wg.Add(1)
        fmt.Print("Doing: " + str + "\r\n")
        go ExeCmd(str, wg)
    }

    wg.Wait()
}

终端输出示例:

Path: test
Files, Directories, or Both: b
Permissions for Files: 644
Permissions for Directories: 755
Doing: find test -type f | while read i; do sudo -S chmod 644 "$i"; done
Doing: find test -type d | while read i; do sudo -S chmod 755 "$i"; done
exit status 1

exit status 1

谢谢您抽时间见我.

推荐答案

exec.Command()将要求内核直接执行给定的进程.但是,您传递的命令是由一个shell脚本连接在一起的多个程序组成的字符串.

如果要执行shell脚本,应该使用以下内容:

cmd := exec.Command("/bin/sh", "-c", "sudo find ...")

Go相关问答推荐

如何在AWS SDK Go v2 STS上正确使用重试

如何在jsonrpc服务器的服务器端捕获错误?

是不是有什么原因导致`Strings.EqualFold`不先进行长度比较?

使用Golang的Lambda自定义al2运行时,初始化阶段超时

Kafka golang 生产者在错误后更改分区计数

Go Build删除信息

这是实现超时的常见方法,为什么 time.After 不起作用

如何将验证器标记添加到嵌套字段

如何使用管理员权限启动 powershell 进程并重定向标准输入 (os.exec)

如果值为 false,gRPC 不返回布尔值

致命错误 - 所有 Goroutines 都睡着了!僵局

Golang Oauth2 服务帐户返回空刷新令牌字符串

go-libp2p - 从流中接收字节

为什么 reflect.TypeOf(new(Encoder)).Elem() != reflect.TypeOf(interfaceVariable)?

未定义 protoc protoc-gen-go 时间戳

K8s 算子读取原始数据

GORM GIN 更新查询导致 500 内部服务器错误

comparable和any有什么区别?

Go 错误:cannot use generic type without instantiation

是否可以动态加载 Go 代码?