简体   繁体   English

当我尝试使用 exec 在 golang 中的 cmd 上执行以下代码时,执行命令给出错误“没有这样的文件或目录”。 有人可以帮我弄这个吗?

[英]Exec command giving error "No such file or directory" when I try to execute the below code on cmd in golang with exec. Can someone help me with this?

"No such file or directory" when I try to execute the below code on cmd in golang with exec.当我尝试使用 exec 在 golang 中的 cmd 上执行以下代码时,“没有这样的文件或目录”。 This is basically because of the white spaces in this path ->这基本上是因为此路径中的空格 ->

/Users/ltuser/Library/Application Support/Google/Chrome Beta/Default 

How do i escape the white spaces when executing with exec command in golang cmd in macos?在macos中使用golang cmd中的exec命令执行时如何转义空格?

        cmdStr := fmt.Sprintf("find /Users/ltuser/Library/Application Support/Google/Chrome Beta/Default -mindepth 1 ! -name Preferences -delete")
        args := strings.Fields(cmdStr)
        cmd := exec.Command(args[0], args[1:]...)
        op, err := cmd.CombinedOutput()
        if err != nil {
            fmt.Println("error",err.Error())
        }

How do i escape the white spaces when executing with exec command in golang cmd in macos?在macos中使用golang cmd中的exec命令执行时如何转义空格?

"find /Users/ltuser/Library/Application Support/Google/Chrome Beta/Default -mindepth 1 ! -name Preferences -delete"

It's important to distinguish between exec.Command and a shell statement.区分exec.Command和 shell 语句很重要。 When you're running things at "the command line", you're running them in a shell.当你在“命令行”运行东西时,你是在 shell 中运行它们。 This lets you create pipelines with |这使您可以使用|创建管道。 , redirect with < , > , etc, use variables, and so on. ,使用<>等重定向,使用变量等等。 It has a specific syntax for executing executables in the $PATH such as find .它具有用于在$PATH中执行可执行文件的特定语法,例如find In shell syntax, a sequence of characters executable arg1 arg2 arg3 will be parsed around the spaces.在 shell 语法中,将围绕空格解析executable arg1 arg2 arg3的字符序列。 executable , if a program found in the path, will be executed with exec . executable ,如果在路径中找到程序,将使用exec The arguments, split by spaces, will become the arguments to exec .用空格分隔的 arguments 将成为 arguments 到exec

That's why when you run a command like your find at the shell, strings like /Users/ltuser/Library/Application Support/Google/Chrome Beta/Default must be enquoted if they're to be passed as one argument.这就是为什么当您在 shell 上运行类似find的命令时,如果要将它们作为一个参数传递,则必须引用/Users/ltuser/Library/Application Support/Google/Chrome Beta/Default之类的字符串。

But you're not running this at the shell, even though you expressed your command as a sequence of string-separated values.但是您没有在 shell 上运行它,即使您将命令表示为字符串分隔值的序列。 That's why you这就是为什么你

        args := strings.Fields(cmdStr)

That's where your path with spaces becomes multiple arguments.这就是你的空格路径变成多个 arguments 的地方。

exec.Command has an interface like the OS exec , because that's what it uses to execute your commands for you. exec.Command有一个类似于 OS exec的接口,因为它是用来为你执行命令的。 And that's why it takes a list of strings;这就是为什么它需要一个字符串列表 no parsing need be done, and no characters in the strings need to be escaped.不需要进行解析,也不需要对字符串中的字符进行转义。

So just split up the arguments in the code, and pass them straight into exec.Command :因此,只需在代码中拆分 arguments,并将它们直接传递给exec.Command

cmd := exec.Command("find", 
   "/Users/ltuser/Library/Application Support/Google/Chrome Beta/Default", 
   "-mindepth",
    ... ... ...,
)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在Golang中使用exec.Command,如何打开新终端并执行命令? - Using exec.Command in golang, how do I open a new terminal and execute a command? “ dir / *:没有这样的文件或目录”,带有find -exec…“ {} / *” - “dir/*: No such file or directory” with find -exec … “{}/*” 无法为Xcode 10 Beta 6执行&#39;coremlc&#39;(无此类文件或目录) - Can't exec 'coremlc' (No such file or directory) for Xcode 10 Beta 6 xcrun:错误:无法执行真正的xcrun。 (没有相应的文件和目录) - xcrun: Error: failed to exec real xcrun. (No such file or directory) go(golang)中的os.exec不执行$ PATH中的命令吗? - Does os.exec from go (golang) not execute commands in $PATH? Mac本地bin目录中的奇怪exec文件 - strange exec file in local bin directory in mac 如何在 MacOS 上编译 Rust 以在 AWS EC2 实例上运行? “无法执行二进制文件:Exec 格式错误” - How do I compile Rust on MacOS, to be run on an AWS EC2 instance? "cannot execute binary file: Exec format error" 当我尝试从 command 或 android studio 获取 SHA-1 号码时,它给了我一个错误 - when I try to get SHA-1 number from command or android studio it gives me an error FileNotFoundError:[Errno 2] 没有这样的文件或目录:&#39;/Users/fuadhafiz/Documents\\\\chromedriver.exec&#39; 在 MAC OSX 上使用 Selenium 时出错 - FileNotFoundError: [Errno 2] No such file or directory: '/Users/fuadhafiz/Documents\\chromedriver.exec' error using Selenium on MAC OSX 使用-or和exec进行命令查找 - command find with -or and exec malfunctioning
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM