1

I'm trying to use Swift to run a command in the (macOS) terminal. Specifically, to create an empty file. Here's what I have for running in a shell:

    import Foundation
    func shell(_ args: String...) ->Int32{
        let task = Process()
        task.launchPath = "/bin/bash"
        task.arguments = args
        task.launch()
        task.waitUntilExit()
        return task.terminationStatus
    }

Note that I have tried /usr/bin/env/ for the launch path with no good result.
When I call shell("touch /path/to/new/file.txt") it returns an error like:

    /bin/bash: touch /Users/my_home_dir/Desktop/file.txt: No such file or directory
    127

If I change the launch path it gives a very verbose but unhelpful console message Which reminds me of Python typical output <'class 'demo' at 0x577e040ab4f'> I've even tried running python in the terminal and creating a file with open().
I am open to any new ways to create files in Swift (which would be great), and any ways to do the above so that it actually works.

Thanks in advance

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Benj
  • 736
  • 7
  • 21
  • I don't know swift, but the error message suggests that Bash interprets the whole command as a single string. In languages I know, calling out to the shell requires you to split your command into the separate words and hand those over as an array. So, just a guess, calling `shell("touch", "/path/to/new/file.txt")`? – Benjamin W. Sep 07 '18 at 17:15

1 Answers1

0

Calling that command (/bin/bash touch ~/Desktop/touch.txt) directly in Terminal result in the same error.

/usr/bin/touch: cannot execute binary file

If you only want to call "touch" you can set the launch path to /usr/bin/touch and only pass ~/Desktop/touch.txt for the argument.

If you want to call general bash/shell commands you pass -c followed by the shell command joined as a string so that it corresponds to: /bin/bash -c "touch ~/Desktop/touch.txt".

David Rönnqvist
  • 56,267
  • 18
  • 167
  • 205
  • Thanks @DavidRonnqvist but it then returns an error: `/usr/bin/touch: cannot execute binary file \n 126`. Any Ideas? – Benj Sep 08 '18 at 08:31
  • @Benj how are you calling the function when you get that error? – David Rönnqvist Sep 08 '18 at 08:59
  • `shell("touch","/Users/my_home_dir/Desktop/file.txt")` – Benj Sep 09 '18 at 14:15
  • I get the same error when I run `/bin/bash touch ~/Desktop/touch.txt` directly from Terminal. – David Rönnqvist Sep 10 '18 at 08:53
  • I think I've figured out that it can't execute the binary file 'touch' because it doesn't have the necessary permissions.... I've tried to get permissions but even I, the administrator, don't have the necessary permissions to change them. Is there an easy way to get around this, without messing up my system?? – Benj Sep 10 '18 at 10:46
  • when I try your new answer the error is that there is no such file or directory. I can't use `pwd` because it doesn't print the result. How can I fix this error? – Benj Sep 11 '18 at 09:18