【发布时间】:2015-01-21 17:03:35
【问题描述】:
这是我无法使用的代码。如果我删除带有空格的目录部分,我的 shell 脚本运行良好。
shell 脚本简化为:/usr/bin/find $1 -name *.txt
空格错误是二元运算符预期的
let bundle = NSBundle.mainBundle()
let path = bundle.pathForResource("doSimpleFind", ofType: "sh")
let findTask = NSTask()
let pipe = NSPipe()
findTask.standardOutput = pipe
// directory where to search from the script. testing the space in the name
let constArgsString:String = "/Users/aUser/Library/Application\\ Support"
//let constArgsString:String = "/Users/aUser/Library/"
findTask.launchPath = path!
findTask.arguments = [constArgsString]
findTask.launch()
findTask.waitUntilExit()
let data = pipe.fileHandleForReading.readDataToEndOfFile()
let outputText:String = NSString(data: data, encoding:NSUTF8StringEncoding)!
cmdTextResult.textStorage?.mutableString.setString(outputText)
【问题讨论】:
-
这种方法是我能够将参数从 NSTask 传递给 .sh 的唯一方法。我见过的所有其他示例都从参数启动命令。但无法将参数传递给 .sh 脚本