Swift-终端/命令行

Swift-终端/命令行,swift,Swift,我试图通过命令行发送Python命令。 我正在使用这些函数。 如果我使用像[“flash\u id”]这样的直接参数,则命令工作正常,而如果我插入像[read\u flash 0x00000 0x100000 data.bin]这样更复杂的参数,则不会执行命令。 而在mac终端上,它可以做到这一点。 我错在哪里 func shell(launchPath: String, arguments: [String]) -> String { let task = Process()

我试图通过命令行发送Python命令。 我正在使用这些函数。 如果我使用像
[“flash\u id”]
这样的直接参数,则命令工作正常,而如果我插入像
[read\u flash 0x00000 0x100000 data.bin]这样更复杂的参数,则不会执行命令。
而在mac终端上,它可以做到这一点。
我错在哪里

func shell(launchPath: String, arguments: [String]) -> String {
    let task = Process()
    task.launchPath = launchPath
    task.arguments = arguments
    let pipe = Pipe()
    task.standardOutput = pipe
    task.launch()
    let data = pipe.fileHandleForReading.readDataToEndOfFile()
    let output = String(data: data, encoding: String.Encoding.utf8)!
    if output.count > 0 {
        //remove newline character.
        let lastIndex = output.index(before: output.endIndex)
        return String(output[output.startIndex ..< lastIndex])
    }
    return output
}

func bash(command: String, arguments: [String]) -> String {
    let whichPathForCommand = shell(launchPath: "/bin/bash", arguments: ["-l", "-c","which \(command)"])
    return shell(launchPath: whichPathForCommand, arguments: arguments)
}


var result = bash(command: "esptool.py", arguments: ["flash_id"] // command executed

var result = bash(command: "esptool.py", arguments: ["read_flash 0x00000 0x100000 data.bin"]. //esptool: error: argument operation:
func shell(启动路径:String,参数:[String])->String{
让任务=进程()
task.launchPath=启动路径
task.arguments=参数
让管道=管道()
task.standardOutput=管道
task.launch()
让data=pipe.fileHandleForReading.readDataToEndOfFile()
让输出=字符串(数据:数据,编码:String.encoding.utf8)!
如果output.count>0{
//删除换行符。
让lastIndex=output.index(在:output.endIndex之前)
返回字符串(输出[output.startIndex..String{
让whichPathForCommand=shell(启动路径:“/bin/bash”,参数:[“-l”、“-c”、“which\(command)”)
返回shell(launchPath:whichPathForCommand,参数:参数)
}
var result=bash(命令:“esptool.py”,参数:[“flash_id”]//已执行命令
var result=bash(命令:“esptool.py”,参数:[“read_flash 0x000000x100000 data.bin”]。//esptool:错误:参数操作:

你有完整的错误信息吗?对于多个单词,它们不都是参数吗?
[“read_flash”、“0x00000”、“0x100000”、“data.bin”]
好的,谢谢,我照你说的做了,它工作得很好。