代码如下
import Foundation
let commandLine = "ls -l"
let process = Process()
process.launchPath = "/usr/bin/env"
process.arguments = ["bash", "-c", commandLine]
process.launch()
process.waitUntilExit()
代码使用了 Process
类,并设置了要执行的命令字符串 "ls -l"
。launchPath
属性指定了要执行的程序路径,arguments
属性指定了命令行参数。launch()
方法用于启动进程,waitUntilExit()
方法用于等待进程执行完毕。