如何快速执行终端命令? [英] How to execute terminal command in swift?

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

问题描述

我是Swwift的新手.如何从Swift代码运行此过程?

I am new toSswift. How can I run this process from Swift code?

  1. 打开终端窗口
  2. 执行cd Desktop/firebase-mac
  3. 执行npm start
  1. open Terminal window
  2. execute cd Desktop/firebase-mac
  3. execute npm start

我实际上想做的是单击Swift代码以启动Node服务器.

What I am actually trying to do is to start Node server on click from Swift code.

推荐答案

完整示例:

  • 转到某个目录,假设Desktop
  • 创建一个名称为swsh的文件,并将其添加到文件中(纯文本格式,而不是rtf或doc)
  • go to some directory, let say Desktop
  • Create a file with name swsh, and add into it (plaintext, not rtf, or 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 script运行为:./swsh
  • open the Terminal
  • type cd ~/Desktop
  • use chmod 755 swsh
  • and run your swift script as: ./swsh

您将得到如下输出:

Sat Mar 25 14:31:39 CET 2017

编辑您的swsh并将shell(...行更改为:

Edit your swsh and change the shell(... line to:

let output = shell(launchPath: "/usr/bin/env", arguments: [ "date" ])

运行它,然后再次获取日期,但是现在:

run it and again will get the date, but now:

  • swsh执行了/usr/bin/env(带有参数date)
  • /usr/bin/env找到命令date
  • 并执行了
  • the swsh executed the /usr/bin/env, (with the argument date)
  • and the /usr/bin/env finds the command date
  • and executed it

现在,在~/Desktop中创建另一个文件,并将其命名为from_swift.

Now, create another file in the ~/Desktop and name it as from_swift.

添加到其中

echo "Today's date is $(date)"

更改文件swshshell行更改为:

let output = shell(launchPath: "./from_swift", arguments: [ ])

注意,./from_swift-使用到.的相对路径(我们在~/Desktop目录中).运行swift程序:

Note, the ./from_swift - using relative path to . (we are in the ~/Desktop directory). Run the swift program:

./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 尚未执行.因此执行:

Of course, the script from_swift is not executable yet. So execute:

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 script,所以将from_swift脚本编辑为:

This is because the from_swift is a script (not a compiled binary), so the operating system need to know which binary should interpret the script content. Because this is an shell script edit the from_swift script as:

#!/bin/sh
echo "Today's date is $(date)"

请注意添加的"shebang"行:#!/bin/sh.快速运行./swsh并获得

Note the added "shebang" line: #!/bin/sh. Run the swift ./swsh and will get

Today's date is Sat Mar 25 14:50:23 CET 2017

好极了,您从swift执行了第一个bash脚本. ;)

Horray, you executed your 1st bash script from swift. ;)

当然,您可以在shebang中使用/usr/bin/env,因此现在将from_swift的内容更改为:

Of course, you can use the /usr/bin/env in the shebang, so now change, the content of the from_swift for example to:

#!/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

NOTE, we changed nothing in the ./swsh file, just the script file ./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

So, it is easy to create and execute any script. So, you can enter into your ~/Desktop/from_swift

#!/bin/sh
cd $HOME/Desktop/firebase-mac
npm start

可以从swsh直接进行 (Jens Meder提出),但是使用此方法,您可以非常简单地执行给定脚本文件中的任何内容.

It is possible to do directly from the swsh, (Jens Meder proposed), but using this you got very easy method executing anything from the given script file.

请记住:process.launch()执行以下任一操作:

Just remember: the process.launch() executes either:

  • 已编译的二进制文件
  • 或脚本文件,但脚本文件
    • 必须具有shebang
    • ,并且必须使用chmod 755 /path/to/script.file
    • 可执行
    • compiled binaries
    • or script files, but the script files
      • must have the shebang line
      • and must be executable using chmod 755 /path/to/script.file

      这篇关于如何快速执行终端命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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