Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/16.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 将多个参数从Applescript传递到终端命令脚本_Macos_Bash_Parameters_Terminal_Applescript - Fatal编程技术网

Macos 将多个参数从Applescript传递到终端命令脚本

Macos 将多个参数从Applescript传递到终端命令脚本,macos,bash,parameters,terminal,applescript,Macos,Bash,Parameters,Terminal,Applescript,我一直在试图找出如何将多个参数从Applescript传递到终端命令脚本。例如,当运行终端命令文件时,您可以通过编程方式接收参数,如下所示: #!/bin/bash var=$1 var=$2 我一直使用的Applescript代码如下所示,以供参考: tell application "System Events" to set app_directory to POSIX path of (container of (path to me)) set thisFile to "Dev

我一直在试图找出如何将多个参数从Applescript传递到终端命令脚本。例如,当运行终端命令文件时,您可以通过编程方式接收参数,如下所示:

#!/bin/bash

var=$1

var=$2
我一直使用的Applescript代码如下所示,以供参考:

tell application "System Events" to set app_directory to POSIX path of (container of (path to me))

set thisFile to "Dev"

set testTarget to "/Users/lab/Desktop/TestTarget/"

do shell script "/Users/lab/Desktop/TempRoot/mycommand.command " & thisFile & testTarget with administrator privileges
我认为我出错的地方是第二个参数的输入。当我只有一个参数时,它运行得很好:

do shell script "/path/to/command/mycommand.command" &var with administrative privileges

我想知道传递第二个参数的正确语法是什么。如果有人有任何建议,请让我知道!如果你需要更多的信息,我很乐意提供

您只需要在参数之间添加一个空格。现在,在
这个文件
测试目标
之间没有添加任何空间。您的命令如下所示:

/Users/lab/Desktop/TempRoot/mycommand.command Dev/Users/lab/Desktop/TestTarget/
将shell脚本行更改为:

do shell script "/Users/lab/Desktop/TempRoot/mycommand.command " & thisFile & space & testTarget with administrator privileges
在构建脚本时,我发现有一点很有用,那就是在运行shell命令之前确保它们是正确的。因此,与其直接构建命令,不如将命令存储在变量中并记录它。稍后,用
do shell脚本
命令替换日志记录语句

set shellScript to "/Users/lab/Desktop/TempRoot/mycommand.command " & thisFile & space & testTarget with administrator privileges
log shellScript
-- do shell script shellScript

这成功了!我会支持这篇文章,但显然我没有足够的stackoverflow点数:(但非常感谢你!@mmiltinovic1313:你不需要声望点数来接受你的问题的答案(单击复选标记)。另一个值得一提的技巧是:确保(文字)文件名等字符串未经修改地传递给shell(为了保护字符串不受shell扩展的影响),在它们前面加上
引号形式的
。记住“引号形式的shell脚本”会很有帮助处理空格和引用字符。谢谢你们。你们是否也可以对尝试在cocoa应用程序中实现这种类型的脚本有任何建议?我在这里使用了这种脚本,但当我将其切换到objective-c时,我遇到了一些语法错误。如果你们有时间,下面是另一篇帖子: