如何运行一个脚本迅速的终端命令? (例如,x codebuild) [英] How do I run an terminal command in a swift script? (e.g. xcodebuild)

查看:238
本文介绍了如何运行一个脚本迅速的终端命令? (例如,x codebuild)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想与SWIFT来代替我的CI的bash脚本。我无法弄清楚如何调用普通的终端命令,例如 LS X codebuild

 #!的/ usr / bin中/ env的xcrun迅速进口基金会//工程
的println(测试)//作品
LS //失败
X codebuild -workspace myApp.xcworkspace //失败


  $ ./script.swift
./script.swift:5:1:错误:使用未解决的标识符LS
LS //失败
^
...等....


解决方案

如果你不斯威夫特code使用命令输出,以下就足够了:

 #!的/ usr / bin中/ env的迅速进口基金会FUNC外壳(参数:字符串...) -  GT; INT32 {
    让任务= NSTask()
    task.launchPath =的/ usr / bin中/ env的
    task.arguments = ARGS
    task.launch()
    task.waitUntilExit()
    返回task.terminationStatus
}贝(LS)
贝(X codebuild,-workspace,myApp.xcworkspace)

I want to replace my CI bash scripts with swift. I cant figure out how to invoke normal terminal command such as ls or xcodebuild

#!/usr/bin/env xcrun swift

import Foundation // Works
println("Test") // Works
ls // Fails
xcodebuild -workspace myApp.xcworkspace // Fails


$ ./script.swift
./script.swift:5:1: error: use of unresolved identifier 'ls'
ls // Fails
^
... etc ....

解决方案

If you don't use command outputs in Swift code, following would be sufficient:

#!/usr/bin/env swift

import Foundation

func shell(args: String...) -> Int32 {
    let task = NSTask()
    task.launchPath = "/usr/bin/env"
    task.arguments = args
    task.launch()
    task.waitUntilExit()
    return task.terminationStatus
}

shell("ls")
shell("xcodebuild", "-workspace", "myApp.xcworkspace")

这篇关于如何运行一个脚本迅速的终端命令? (例如,x codebuild)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆