我是一名Swift新手。我该如何从Swift代码运行这个过程?
- 打开终端窗口
- 执行
cd Desktop/firebase-mac
- 执行
npm start
我实际上想要做的是,在Swift代码中点击后启动Node服务器。
我是一名Swift新手。我该如何从Swift代码运行这个过程?
cd Desktop/firebase-mac
npm start
我实际上想要做的是,在Swift代码中点击后启动Node服务器。
完整示例:
桌面
swsh
的文件,并将以下内容添加到其中(明文,不是rtf或doc格式)#!/usr/bin/env xcrun swift
import Foundation
func shell(launchPath: String, arguments: [String]) -> String {
let process = Process()
process.launchPath = launchPath
process.arguments = arguments
let pipe = Pipe()
process.standardOutput = pipe
process.launch()
let output_from_command = String(data: pipe.fileHandleForReading.readDataToEndOfFile(), encoding: String.Encoding.utf8)!
// remove the trailing new-line char
if output_from_command.characters.count > 0 {
let lastIndex = output_from_command.index(before: output_from_command.endIndex)
return output_from_command[output_from_command.startIndex ..< lastIndex]
}
return output_from_command
}
let output = shell(launchPath: "/bin/date", arguments: [ ])
print(output)
保存,然后:
cd ~/Desktop
chmod 755 swsh
swift脚本
: ./swsh
你将得到如下输出:
Sat Mar 25 14:31:39 CET 2017
编辑你的swsh
,并将shell(...
行更改为:
let output = shell(launchPath: "/usr/bin/env", arguments: [ "date" ])
运行它,你将再次获得日期,但现在:
swsh
执行了 /usr/bin/env
(带有参数 date
)/usr/bin/env
查找命令 date
现在,在 ~/Desktop
中创建另一个文件,并将其命名为 from_swift
。
将以下内容添加到其中:
echo "Today's date is $(date)"
将文件 swsh
中的 shell
行更改为:
let output = shell(launchPath: "./from_swift", arguments: [ ])
注意,./from_swift
- 使用相对路径到.
(我们在~/Desktop
目录中)。运行swift程序:
./swsh
输出:
2017-03-25 14:42:20.176 swift[48479:638098] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'launch path not accessible'
from_swift
目前还不能执行。因此,请执行以下操作:chmod 755 from_swift
# and run
./swsh
再次出现错误:
2017-03-25 14:45:38.523 swift[48520:639486] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Couldn't posix_spawn: error 8'
from_swift
是一个脚本(而不是编译后的二进制文件),所以操作系统需要知道哪个二进制文件应该解释脚本内容。由于这是一个shell脚本
,请将from_swift
脚本编辑为:#!/bin/sh
echo "Today's date is $(date)"
#!/bin/sh
。运行swift ./swsh
,将会得到:Today's date is Sat Mar 25 14:50:23 CET 2017
恭喜,你已经成功地从 Swift
执行了你的第一个 Bash
脚本。 ;)
当然,你可以在 shebang
中使用 /usr/bin/env
,现在修改 from_swift
的内容,例如:
#!/usr/bin/env perl
use strict;
use utf8;
use English;
binmode STDOUT, ":utf8";
printf "The $EXECUTABLE_NAME (ver:$PERL_VERSION) runs me: $0\n";
printf "I ❤️ perl!\n";
./swsh
,会得到以下结果:The /usr/bin/perl (ver:v5.18.2) runs me: ./from_swift
I ❤️ perl!
注意,我们在./swsh
文件中没有做任何更改,只是修改了脚本文件./from_swift
!
以上所有操作均使用以下内容完成:
$ uname -a
Darwin nox.local 16.4.0 Darwin Kernel Version 16.4.0: Thu Dec 22 22:53:21 PST 2016; root:xnu-3789.41.3~3/RELEASE_X86_64 x86_64
$ swift --version
Apple Swift version 3.0.2 (swiftlang-800.0.63 clang-800.0.42.1)
Target: x86_64-apple-macosx10.9
~/Desktop/from_swift
文件夹。#!/bin/sh
cd $HOME/Desktop/firebase-mac
npm start
process.launch()
执行以下操作之一:
shebang
行chmod 755 /path/to/script.file
可执行您可以使用以下代码片段,例如:run("npm start")
。它将在默认 shell /bin/sh
上执行命令并将输出作为 String
返回。
func run(_ cmd: String) -> String? {
let pipe = Pipe()
let process = Process()
process.launchPath = "/bin/sh"
process.arguments = ["-c", String(format:"%@", cmd)]
process.standardOutput = pipe
let fileHandle = pipe.fileHandleForReading
process.launch()
return String(data: fileHandle.readDataToEndOfFile(), encoding: .utf8)
}
如果您想使用另一个bash,只需替换launchPath
,例如,对于Zsh,它将是/bin/zsh
。
echo $SHELL
并尝试输出吗?对我来说,它输出 /bin/zsh
,因为我使用 Zsh 作为 shell。 - Jens Meder/usr/bin/env
而不是 /bin/env
。 - clt60import Commands
Commands.Bash.system("cd /Users/${name}/Desktop/firebase-mac && npm start")
Process.launchedProcess(launchPath: "/Users/aharon/Desktop/firebase-mac/npm", arguments: ["start"])
但请注意,如果应用程序被沙盒化,则字面路径将无法正常工作。
cd
命令,你不需要它。只需传递完整路径 /users/aharon/desktop/firebase-mac/npm start
。 - vadian