正在运行系统程序和基础bug
问题描述:
尝试使用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的可执行文件?
答
回答我自己的问题。这似乎是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)")