简体   繁体   English

如何将命令行 arguments 传递给使用打开命令运行的程序?

[英]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 被传递到终端。

NOTE笔记

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.

相关问题 在Swift 3内部命令行程序中如何执行“说”? - How to do a “say” in Swift 3 inside command-line program? 如何从终端运行命令行应用程序? - How to run command-line application from the terminal? 如何将命令行参数传递给 MonoMac 应用程序? - How to pass command line arguments to MonoMac application? 将星号传递给命令行程序会使文件名出现在argv中 - Passing an asterisk to a command-line program makes filenames appear in argv 如何在bash脚本中运行命令行工具的多个实例? +脚本的用户输入 - How to run multiple instances of command-line tool in bash script? + user input for script 如何使用内置命令行参数运行Java应用程序? - How to run java application with built-in command line arguments? 如何在 Mac 终端中使用命令行参数运行 exe 文件? - How to run exe file with command line arguments in Mac terminal? 打开终端并在命令行mac中执行带有参数的命令 - Open terminal and execute command with arguments in command line mac 我需要在OS X上使用--release-license命令选项运行命令行Ioncube(PHP编码器) - I need to run Command-Line Ioncube (PHP Encoder) with --release-license command option on OS X 如何从命令行(MacOS)列出AFP共享? - How to list AFP shares from command-line (MacOS)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM