<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Vinicius Vendramini on Medium]]></title>
        <description><![CDATA[Stories by Vinicius Vendramini on Medium]]></description>
        <link>https://medium.com/@vvendra?source=rss-bed1d03e0f0c------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*1KHc02A0a_MgZxrJNkoFyw.jpeg</url>
            <title>Stories by Vinicius Vendramini on Medium</title>
            <link>https://medium.com/@vvendra?source=rss-bed1d03e0f0c------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Thu, 19 Nov 2026 16:17:07 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@vvendra/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Running a shell process from swift]]></title>
            <link>https://medium.com/@vvendra/running-a-shell-process-from-swift-50308e5ef3b4?source=rss-bed1d03e0f0c------2</link>
            <guid isPermaLink="false">https://medium.com/p/50308e5ef3b4</guid>
            <category><![CDATA[shell]]></category>
            <category><![CDATA[swift]]></category>
            <category><![CDATA[bash]]></category>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[Vinicius Vendramini]]></dc:creator>
            <pubDate>Wed, 15 Mar 2017 15:46:00 GMT</pubDate>
            <atom:updated>2020-05-29T17:29:06.509Z</atom:updated>
            <content:encoded><![CDATA[<p><em>Update (May 2020)</em>: the code in this post has since been integrated into <a href="https://github.com/vinivendra/Gryphon">Gryphon</a> (a Swift to Kotlin translator), and it&#39;s been through a lot of improvements since this was written. There&#39;s an updated version <a href="https://github.com/vinivendra/Gryphon/blob/183a6509ca23a936a8e54bbc9cfe6088f1f60402/Sources/GryphonLib/Shell.swift">in this commit</a>, and there may be an even better version in <a href="https://github.com/vinivendra/Gryphon/blob/master/Sources/GryphonLib/Shell.swift">the master branch</a>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*KmM46jLlTzuTCTP_5DU9Dg.png" /></figure><p>Whenever I want to do some quick scripting, like creating a lot of random test cases to test some python program, <strong>perl</strong> is usually my language of choice. Sure, it’s unbelievably difficult to understand a few days later, but for something quick and dirty it does its job very well.</p><p>One of the main advantages is that it’s remarkably simple to run shell commands from perl: you just write something in back ticks:</p><pre>`echo &quot;foo&quot;`</pre><p>This makes it easy, for instance, to call the appropriate python script once I’ve calculated some test input to use — then validate the output once the process finishes.</p><p>In an effort to switch my scripting processes to Swift, I wanted to know if something like this would be possible. Turns out there&#39;s a class called Process in Foundation that does exactly that. However, like most Foundation classes, it is considerably flexible, which also means there&#39;s a lot of boilerplate going on; here&#39;s my attempt to turn it into a simple function call.</p><p>Firstly, I googled <a href="http://stackoverflow.com/questions/26971240/how-do-i-run-an-terminal-command-in-a-swift-script-e-g-xcodebuild">some snippets</a> that used the Process class so I could get an initial approach. Turns out I also needed to use the Pipe class to catch the output. What really bugged me here is that the program I was trying to test needed to read its input from stdin. I wanted to feed it a normal string (instead of writing having it read a file, which seemed too cumbersome), but I couldn&#39;t find a way to do that with a Pipe—the official documentation got me going really well on other aspects but it failed me here.</p><p>The solution, then: write the input to a temporary file, run the process using that file as input, then delete the file. Cumbersome <em>and</em> a bit too hacky for my taste, but oh well.</p><p>The resulting functions worked surprisingly well, considering I thought this wasn&#39;t even possible when I started. I just used them to create a few test functions and grade a few assignments successfully. The final code is in the gist below:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7d4eda023e24940287c7e1a444cbdcd3/href">https://medium.com/media/7d4eda023e24940287c7e1a444cbdcd3/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=50308e5ef3b4" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>