2017-08-19 29 views
2

嘗試使用swift腳本在系統中運行可執行文件。我跟着第二個答案here,但是我碰到一個錯誤,抱怨基金會未正確創建:正在運行系統程序和基礎bug

錯誤:

/usr/lib/swift/CoreFoundation/CoreFoundation.h:25:10: note: while building module 'SwiftGlibc' imported from /usr/lib/swift/CoreFoundation/CoreFoundation.h:25: 
#include <sys/types.h> 
     ^

代碼:

import Foundation 

func execCommand(command: String, args: [String]) -> String { 
    if !command.hasPrefix("/") { 
     let commandFull = execCommand(command: "/usr/bin/which", args: [command]).trimmingCharacters(in: CharacterSet.whitespacesAndNewlines) 
     return execCommand(command: commandFull, args: args) 
    } else { 
     let proc = Process() 
     proc.launchPath = command 
     proc.arguments = args 
     let pipe = Pipe() 
     proc.standardOutput = pipe 
     proc.launch() 
     let data = pipe.fileHandleForReading.readDataToEndOfFile() 
     return String(data: data, encoding: String.Encoding.utf8)! 
    } 
} 

let commandOutput = executeCommand("/bin/echo", ["Hello, I am here!"]) 
println("Command output: \(commandOutput)") 

我運行這個使用Linux中的崇高REPL(Archlinux)。問題:

  • 我做的所有其他小項目都很好,從未發現基金會有錯誤,因爲它在這裏抱怨。我的安裝是否是問題?

  • 有沒有更簡單的方式來運行使用Glibc的可執行文件?

回答

0

回答我自己的問題。這似乎是Sublime REPL中的一個bug,因爲在命令行中運行它可以讓代碼順利運行。另外,Swift 3沒有更新的代碼存在一些問題,下面是傳遞代碼。我仍然希望找到一種使用Glibc在Swift中運行可執行文件的方法。

#! /usr/bin/swift 

import Foundation 

func execCommand(command: String, args: [String]) -> String { 
    if !command.hasPrefix("/") { 
     let commandFull = execCommand(command: "/usr/bin/which", args: [command]).trimmingCharacters(in: CharacterSet.whitespacesAndNewlines) 
     return execCommand(command: commandFull, args: args) 
    } else { 
     let proc = Process() 
     proc.launchPath = command 
     proc.arguments = args 
     let pipe = Pipe() 
     proc.standardOutput = pipe 
     proc.launch() 
     let data = pipe.fileHandleForReading.readDataToEndOfFile() 
     return String(data: data, encoding: String.Encoding.utf8)! 
    } 
} 

let commandOutput = execCommand(command:"/bin/echo", args:["Hello, I am here!"]) 
print("Command output: \(commandOutput)") 
相關問題