Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/macos/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Macos 如何将命令行参数传递给使用open命令运行的程序?_Macos_Bash - Fatal编程技术网

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

Macos 如何将命令行参数传递给使用open命令运行的程序?,macos,bash,Macos,Bash,有没有办法通过以下方式将参数传递给正在运行的程序: open -a /Applications/Utilities/Terminal.app ~/my_executable 我试过: open -a /Applications/Utilities/Terminal.app ~/my_executable arg1 arg2 open -a /Applications/Utilities/Terminal.app '~/my_executable arg1 arg2' open -a /Ap

有没有办法通过以下方式将参数传递给正在运行的程序:

open -a /Applications/Utilities/Terminal.app ~/my_executable
我试过:

open -a /Applications/Utilities/Terminal.app ~/my_executable arg1 arg2
open -a /Applications/Utilities/Terminal.app '~/my_executable arg1 arg2'
open -a /Applications/Utilities/Terminal.app ~/my_executable | xargs arg1 arg2
但这被解释为告诉终端打开
~/my\u executable~/arg1~/arg2.

我试过:

open -a /Applications/Utilities/Terminal.app ~/my_executable arg1 arg2
open -a /Applications/Utilities/Terminal.app '~/my_executable arg1 arg2'
open -a /Applications/Utilities/Terminal.app ~/my_executable | xargs arg1 arg2
但它拾取arg1和arg2,就好像它们是路径的一部分而不是参数一样

我试过:

open -a /Applications/Utilities/Terminal.app ~/my_executable arg1 arg2
open -a /Applications/Utilities/Terminal.app '~/my_executable arg1 arg2'
open -a /Applications/Utilities/Terminal.app ~/my_executable | xargs arg1 arg2
我也尝试过:

open -a /Applications/Utilities/Terminal.app ~/my_executable --args arg1 arg2
但是有了这个标志,arg就被传递到终端

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


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

$ echo "~/my_executable arg1 arg2" > /tmp/tmp.sh ; chmod +x /tmp/tmp.sh ; open -a Terminal /tmp/tmp.sh ; rm /tmp/tmp.sh

编辑:保留下面的原始答案,因为有些人似乎觉得它很有用,但请记住,这并不能真正回答OP的问题,这是将参数传递给以“打开”打开的应用程序,而不是传递给以“打开”打开的Terminal.app和以“打开”打开的Terminal.app

您可以在不带参数的情况下运行open来找到答案:

%open用法:open[-e][t][f][W][R][n][g][h][b][a][filename][--args参数]

[……]

--args所有剩余的参数都在argv中传递给应用程序的main()函数,而不是opened。

[……]

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

open ./Untitled.app --args arg1 arg2 arg3

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

是的,我知道。需要管理另一个脚本。 但是换个角度思考。您不是在终端上工作,而是在脚本编辑器上工作。 (不是bash脚本,而是AppleScript'ing)


有什么原因不使用
open
命令就不能直接运行可执行文件吗?是的,因为Xcode使用的是命令:/(因此必须在终端窗口中调试)。您尝试将--args放在ecexutable前面的内容:
打开-a/Applications/Utilities/Terminal.app--args~/my_可执行文件arg1 arg2
?这也不起作用,@chown。您可以通过打开终端并输入此命令来自己尝试
open-n-a/Applications/Utilities/Terminal.app--args~/my_可执行文件arg1 arg2
那么让终端运行一个shell脚本来启动带有参数的可执行文件?这将起作用,不幸的是,它必须创建和管理一个额外的shell脚本才能这样做。是的,不幸的是,我看不到更优雅的方法-这似乎是一个不容易解决的限制。此外,我必须在
rm/tmp/tmp.sh
之前添加
sleep 2
命令,因为rm命令运行得太快。所以我的最后一个脚本是这样的:
$echo“~/my_可执行文件arg1 arg2”>/tmp/tmp.sh;chmod+x/tmp/tmp.sh;打开-终端/tmp/tmp.sh;睡眠2;rm/tmp/tmp.sh
在您直接打开应用程序时有效,但OP试图让终端运行带有参数的命令行可执行文件。出于某种原因,-args在我的macOS Catalina 10.15.7计算机上不起作用。终端应用程序将打开脚本文件,但没有向脚本文件传递任何参数。我担心我不值得所有的投票,因为我没有真正理解这个问题。试着看看被接受的答案。不幸的是,我不认为有任何其他方法可以做到这一点,除非有人添加了更多关于为什么需要这样做的上下文(这是一个假设问题,即open命令是原始未声明问题的唯一可能解决方案,这似乎与在xcode中调试cli工具有关……)。