macos - 如何在 Mac OS X 中创建我自己的 "shell"命令行程序

标签 macos swift shell command-line osx-yosemite

我想知道如何使用 Swift 和 Xcode 创建我自己的命令行或 shell 程序,如“cat”、“git”等...?

例如:

rod -a "hello world!" 
rod --version
rod /list -e "bye world!"

提前致谢

最佳答案

最简单的方法可能如下所示:

  1. 打开终端窗口并导航到您选择的目录
  2. 通过输入终端命令创建一个空的 swift 文件

    touch myFirstProgram.swift
  3. Make this file executable by entering the terminal command

    chmod +x myFirstProgram.swift
  4. Open this directory in finder by entering the terminal command

    open .
  5. Double-click on myFirstProgram.swift to open the empty file in Xcode. Then, inside Xcode, write the following lines into your first swift program. The first line starts the swift interpreter, anything after that is simple swift code.

    #!/usr/bin/env xcrun swift
    
    println("Hello world.")
    
  6. 保存程序后,您可以通过输入终端命令启动它

    ./myFirstProgram.swift

完成。

了解了这些步骤后,就可以出去自由探索了。例如,我在 http://practicalswift.com/2014/06/07/swift-scripts-how-to-write-small-command-line-scripts-in-swift/ .我发现的唯一过时的信息是 swift 程序的第一行应该如我在第 5 点中详述的那样(行尾没有“-i”)。

玩得开心!

关于macos - 如何在 Mac OS X 中创建我自己的 "shell"命令行程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31846780/

相关文章:

Python 程序 Airnef 在下载图像时卡住了

ios - 以编程方式显示和隐藏 View Controller

linux - 如果在 Linux 中找到匹配的字符串,如何跳过期望工作并继续处理

java - 如何在Java中执行 native 命令?

python - 为什么我在这里需要 shell=True 才能让 Python 的 subprocess.check_output 工作?

c - 防止 dlsym() 在 Mac OS X 上导入旧版 Sleep() 函数

ios - 2019 年编写 Apple 帮助手册

swift - 在 swift 中更改按钮标题的颜色和字体大小(对于 osx NOT ios)

ios - 如何上传用户个人资料图片

ios - 如何更改第二个 UIWindow 的状态栏颜色?