我在使用 Swift Process 运行 MySQL 恢复转储文件时遇到问题。
let command = "/usr/local/bin/mysql -h theHost -P 3306 -u root -pTheInlinePassword example_database < dumpFile.sql"
let task = Process()
task.launchPath = "/usr/bin/env"
task.arguments = command.components(separatedBy: " ")
task.launch()
问题是进程不理解标准输入<
.我如何使用这样的标准输入运行命令?谢谢。
更新:
let task = Process()
task.launchPath = "/usr/local/bin/mysql"
task.arguments = ["-h", "theHost", "-P", "3306", "-u", "root", "-pTheInLinePassword", "example_data"]
task.standardInput = try! FileHandle(forReadingFrom: filePath!)
task.launch()
我试过下面的代码。这对我有用
最佳答案
< filename
语法是 shell 提供的一个特性,而不是程序本身处理过的东西。
处理这个问题的正确方法是构造一个 FileHandle
用于阅读 dumpFile.sql
然后设置 FileHandle
作为 standardInput
Process
的属性(property).
作为旁注,我不知道你为什么要使用 /usr/bin/env
作为您的启动路径,因为您不依赖于 PATH 查找或设置任何环境变量。
let input = try FileHandle(forReadingFrom: URL(fileURLWithPath: "dumpFile.sql"))
let task = Process()
task.launchPath = "/usr/bin/mysql"
task.arguments = ["-h", "theHost", "-P", "3306", "-u", "root", "-pTheInlinePassword", "example_database"]
task.standardInput = input
task.launch()
关于mysql - 如何使用标准输入在 Swift 3.0 中运行进程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44637535/