[英]how to pass command-line arguments to a program run with the open command?
Is there a way to pass arguments to a program being run via:有没有办法将 arguments 传递给正在运行的程序:
open -a /Applications/Utilities/Terminal.app ~/my_executable
I have tried:我试过了:
open -a /Applications/Utilities/Terminal.app ~/my_executable arg1 arg2
But this is interpreted as telling the terminal to open ~/my_executable ~/arg1 ~/arg2.
但这被解释为告诉终端打开
~/my_executable ~/arg1 ~/arg2.
I have tried:我试过了:
open -a /Applications/Utilities/Terminal.app '~/my_executable arg1 arg2'
But it picks up arg1 and arg2 as if they were part of the path rather than arguments.但它会选择 arg1 和 arg2,就好像它们是路径的一部分而不是 arguments。
I have tried:我试过了:
open -a /Applications/Utilities/Terminal.app ~/my_executable | xargs arg1 arg2
I have also tried:我也试过:
open -a /Applications/Utilities/Terminal.app ~/my_executable --args arg1 arg2
But with that flag, args are passed to the terminal.但是有了那个标志,args 被传递到终端。
I am only allowed to change the arguments to Terminal.app (the part within [ ]):我只允许将 arguments 更改为 Terminal.app([ ] 内的部分):
open -a /Applications/Utilities/Terminal.app [~/my_executable arg1 arg2]
Edit: Leaving the original answer below as some people seem to find it useful, but keep in mind that this doesn't really answers OP's question, this is to pass arguments to an app opened with "open" not to and app opened with Terminal.app which was opened with "open".编辑:保留下面的原始答案,因为有些人似乎觉得它很有用,但请记住,这并不能真正回答 OP 的问题,这是将参数传递给使用“打开”而不是“打开”的应用程序打开的应用程序,而不是使用终端打开的应用程序.app 用“open”打开。
You can find your answer by running open without arguments:您可以通过不带参数运行 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.
[...] [...]
You can see there is an option --args
you can use it like this:你可以看到有一个选项
--args
你可以像这样使用它:
open ./Untitled.app --args arg1 arg2 arg3
I tested it on el Capitan (10.11.3) so I don't know if the option is present in earlier versions.我在 el Capitan (10.11.3) 上对其进行了测试,所以我不知道早期版本中是否存在该选项。
可能最简单的方法是创建一个临时的 shell 脚本,例如
$ echo "~/my_executable arg1 arg2" > /tmp/tmp.sh ; chmod +x /tmp/tmp.sh ; open -a Terminal /tmp/tmp.sh ; rm /tmp/tmp.sh
Yes, I know.是的我知道。 need to manage another script.
需要管理另一个脚本。 but think differently.
但换位思考。 you work not on Terminal, but on Script Editor.
您不是在终端上工作,而是在脚本编辑器上工作。 (not bash scripting, but AppleScript'ing)
(不是 bash 脚本,而是 AppleScript'ing)
property testScript : "/tmp/sh.sh"
set input to display dialog "args?" default answer ""
log input
tell application "Terminal"
activate
do script testScript & " " & text returned of input
end tell
For those with having an issue with Paul R's answer, add the ; rm /tmp/tmp.sh
对于那些对 Paul R 的回答有疑问的人,请添加
; rm /tmp/tmp.sh
; rm /tmp/tmp.sh
to the tmp.sh script itself. ; rm /tmp/tmp.sh
到 tmp.sh 脚本本身。 Don't add a sleep command like Maksim as that creates a race condition.不要添加像 Maksim 这样的睡眠命令,因为这会产生竞争条件。
echo "~/my_executable arg1 arg2 ; rm /tmp/tmp.sh" > /tmp/tmp.sh ; chmod +x /tmp/tmp.sh ; open -a Terminal /tmp/tmp.sh
Use the --args
switch before your arguments.在 arguments 之前使用
--args
开关。
open./Terminal.app --args --your-args
From the docs:从文档:
--args: All remaining arguments are passed in argv to the application's main() function instead of opened.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.