call a shell script in swift

To call a shell script from Swift, you can use the system() function from the Foundation framework. Here’s an example:

main.swift
import Foundation

let task = Process()
task.launchPath = "/bin/bash"
task.arguments = ["your_script.sh", "arg1", "arg2"]
task.launch()
task.waitUntilExit()
157 chars
8 lines

In this example, you create a new Process object and set the launchPath to the path of the bash interpreter. You also set the arguments property to an array containing the path to your shell script and any arguments you want to pass to it.

Finally, you call launch() to start the process and waitUntilExit() to wait until the process has completed.

Make sure that the shell script has executable permissions. You can change the permissions by running the chmod command in the terminal:

main.swift
chmod +x your_script.sh
24 chars
2 lines

gistlibby LogSnag