[swift-corelibs-dev] Better integration with UNIX tools

Sneed, Brandon brsneed at ebay.com
Tue Nov 28 12:28:52 CST 2017

+1 from me.

I ended up making a wrapper objc->swift around system() since it was removed from swift recently, however, it behaves (from a user standpoint) exactly how I’d like.  If my command line util jumps out to another program and it needs stdin/out/err, it all just passes through which is what I usually want.  I found trying to pipe everything together via the other mechanisms available was just too much and seemed a little error prone.  I can’t speak to system()’s security or general implementation, just that it seems to do what I want 100% of the time.


From: <swift-corelibs-dev-bounces at swift.org> on behalf of William Dillon via swift-corelibs-dev <swift-corelibs-dev at swift.org>
Reply-To: William Dillon <william at housedillon.com>
Date: Friday, November 17, 2017 at 11:39 AM
To: Tony Parker <anthony.parker at apple.com>
Cc: "swift-corelibs-dev at swift.org" <swift-corelibs-dev at swift.org>
Subject: Re: [swift-corelibs-dev] Better integration with UNIX tools

I, to, have wished for such an API.

I think that would be a very welcome addition.

On Nov 17, 2017, at 11:34 AM, Tony Parker via swift-corelibs-dev <swift-corelibs-dev at swift.org<mailto:swift-corelibs-dev at swift.org>> wrote:

Hi Abhi,

It does seem like there is a possibility of some better convenience API here.

Any ideas on what form it would take? A class method on Process that returns the output, maybe?

- Tony

On Nov 16, 2017, at 3:34 PM, Abhi Beckert via swift-corelibs-dev <swift-corelibs-dev at swift.org<mailto:swift-corelibs-dev at swift.org>> wrote:

Swift is a great shell scripting language except for it's lack of any API to execute UNIX commands. Compare these two shell scripts:


$files = `find ~/Desktop -name *.png`;

foreach (explode("\n", $files) as $file) {
  // do something with $file



import Foundation

let process = Process()
process.launchPath = "/usr/bin/find"
process.arguments = [

let output = Pipe()
process.standardOutput = output


let files: String
if let filesUtf8 = NSString(data: output.fileHandleForReading.readDataToEndOfFile(), encoding: String.Encoding.utf8.rawValue) {
  files = filesUtf8 as String
} else {
  files = NSString(data: output.fileHandleForReading.readDataToEndOfFile(), encoding: String.Encoding.isoLatin1.rawValue) as NSString! as String

files.enumerateLines { file, _ in
  // do something with file

It's a contrived example, I could have used NSFileManager, but I run into this all the time integrating with more complex tools such as rsync.

Adding my own high level wrapper around the Process command isn't an option since there is no good way to import code from another file when executing swift asa shell script. All your code needs to be in one file.

- Abhi
swift-corelibs-dev mailing list
swift-corelibs-dev at swift.org<mailto:swift-corelibs-dev at swift.org>

swift-corelibs-dev mailing list
swift-corelibs-dev at swift.org<mailto:swift-corelibs-dev at swift.org>

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-corelibs-dev/attachments/20171128/0cf69941/attachment.html>

More information about the swift-corelibs-dev mailing list