1
在使用Swift進程運行MySQL恢復轉儲文件時遇到問題。如何使用標準輸入在Swift 3.0中運行進程
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()
我試過代碼波紋管。這適用於我
或'文件句柄(forReadingAtPath:「dumpFile.sql」)' –
你可以使用過,雖然它返回'nil'如果不存在該文件,而URL版本拋出一個錯誤。所以如果你使用字符串版本,確保你處理'nil'值。 –
謝謝凱文和馬丁,我現在試試 – Max