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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29510815/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 21:55:11  来源:igfitidea点击:

how to pass command-line arguments to a program run with the open command?

macosbash

提问by Francisco Aguilera

Is there a way to pass arguments to a program being run via:

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

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 就好像它们是路径的一部分而不是参数一样。

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 就会被传递到终端。

NOTE

笔记

I am only allowed to change the arguments to Terminal.app (the part within [ ]):

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

open -a /Applications/Utilities/Terminal.app [~/my_executable arg1 arg2]

采纳答案by Paul R

Probably the easiest way is to create a temporary shell script, e.g.

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

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

回答by Enrico

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]

%?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 All remaining arguments are passed in argv to the application's main() function instead of opened.

[...]

[...]

You can see there is an option --argsyou 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) 上对其进行了测试,所以我不知道早期版本中是否存在该选项。

回答by Zetton Nara

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