如何将命令行参数传递给使用open命令运行的程序?

Fra*_*era 16 macos bash

有没有办法将参数传递给正在运行的程序:

open -a /Applications/Utilities/Terminal.app ~/my_executable
Run Code Online (Sandbox Code Playgroud)

我试过了:

open -a /Applications/Utilities/Terminal.app ~/my_executable arg1 arg2
Run Code Online (Sandbox Code Playgroud)

但这被解释为告诉终端打开 ~/my_executable ~/arg1 ~/arg2.

我试过了:

open -a /Applications/Utilities/Terminal.app '~/my_executable arg1 arg2'
Run Code Online (Sandbox Code Playgroud)

但是它选择了arg1和arg2,好像它们是路径的一部分而不是参数.

我试过了:

open -a /Applications/Utilities/Terminal.app ~/my_executable | xargs arg1 arg2
Run Code Online (Sandbox Code Playgroud)

我也尝试过:

open -a /Applications/Utilities/Terminal.app ~/my_executable --args arg1 arg2
Run Code Online (Sandbox Code Playgroud)

但是使用那个标志,args被传递到终端.

注意

我只允许将参数更改为Terminal.app([]中的部分):

open -a /Applications/Utilities/Terminal.app [~/my_executable arg1 arg2]
Run Code Online (Sandbox Code Playgroud)

Enr*_*ico 10

您可以通过不带参数的open运行来找到答案:

% open Usage: open [-e] [-t] [-f] [-W] [-R] [-n] [-g] [-h] [-b <bundle identifier>] [-a <application>] [filenames] [--args arguments]

[...]

--args All remaining arguments are passed in argv to the application's main() function instead of opened.

[...]

您可以看到有一个选项--args可以像这样使用它:

open ./Untitled.app --args arg1 arg2 arg3
Run Code Online (Sandbox Code Playgroud)

我在el Capitan(10.11.3)上进行了测试,所以我不知道早期版本中是否存在该选项.

  • 这在您直接打开应用程序时有效,但OP正在尝试让终端运行带参数的命令行可执行文件. (4认同)
  • 由于某种原因,--args 无法在我的 macOS Catalina 10.15.7 计算机上运行。终端应用程序将打开并显示脚本文件,但没有任何参数传递给脚本文件。 (3认同)
  • 恐怕我不值得所有这些赞成票,因为我并没有真正理解这个问题。尝试查看已接受的答案。不幸的是,我认为没有任何其他方法可以做到这一点,除非有人添加更多上下文来说明为什么需要这样做(也就是说,这个问题假设 open 命令是原始未说明问题的唯一可能的解决方案,这似乎与 xcode 中调试 cli 工具有关...)。 (2认同)

Pau*_*l R 7

可能最简单的方法是创建一个临时shell脚本,例如

$ echo "~/my_executable arg1 arg2" > /tmp/tmp.sh ; chmod +x /tmp/tmp.sh ; open -a Terminal /tmp/tmp.sh ; rm /tmp/tmp.sh
Run Code Online (Sandbox Code Playgroud)

  • 那么让终端运行一个 shell 脚本来启动带有参数的可执行文件?这会起作用,但不幸的是,它必须创建和管理额外的 shell 脚本才能做到这一点。 (2认同)
  • 另外,我必须在 `rm /tmp/tmp.sh` 之前添加 `sleep 2` 命令,因为 rm 命令运行得太快。所以我的最终脚本是这样的: `$ echo "~/my_executable arg1 arg2" &gt; /tmp/tmp.sh ; chmod +x /tmp/tmp.sh ; 打开 -a 终端 /tmp/tmp.sh ;睡觉 2 ;rm /tmp/tmp.sh` (2认同)