Skip to content

Instantly share code, notes, and snippets.

@backslash-f
Last active June 13, 2023 19:11
Show Gist options
  • Save backslash-f/02a25d10cbb8d8ff9038bf50728da162 to your computer and use it in GitHub Desktop.
Save backslash-f/02a25d10cbb8d8ff9038bf50728da162 to your computer and use it in GitHub Desktop.

Revisions

  1. Fernando Fernandes revised this gist Jan 26, 2020. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions Shell.swift
    Original file line number Diff line number Diff line change
    @@ -19,4 +19,5 @@ func shell(_ command: String) {
    shell("export MY_VAR=123 ; echo $MY_VAR")

    // In CLI:
    // chmod a+x Shell.swift
    // ./Shell.swift
  2. Fernando Fernandes revised this gist Jan 26, 2020. 1 changed file with 3 additions and 0 deletions.
    3 changes: 3 additions & 0 deletions Shell.swift
    Original file line number Diff line number Diff line change
    @@ -17,3 +17,6 @@ func shell(_ command: String) {
    }

    shell("export MY_VAR=123 ; echo $MY_VAR")

    // In CLI:
    // ./Shell.swift
  3. Fernando Fernandes created this gist Jan 26, 2020.
    19 changes: 19 additions & 0 deletions Shell.swift
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,19 @@
    #!/usr/bin/swift

    import Foundation

    func shell(_ command: String) {
    let task = Process()
    task.launchPath = "/bin/zsh"
    task.arguments = ["-c", command]

    let pipe = Pipe()
    task.standardOutput = pipe
    task.launch()

    let data = pipe.fileHandleForReading.readDataToEndOfFile()
    let output = String(data: data, encoding: .utf8) ?? ""
    print(output)
    }

    shell("export MY_VAR=123 ; echo $MY_VAR")