[英]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.