I recently encountered a similar problem, but my solution is different.
I fix the problem by changing the script resolution mode. In particular, chmod 777 xxx . The key must give permission to execute.
Let them conduct a controlled experiment to verify this:
prepare a script with the path /tmp/a.sh and resolution 666
Contents /tmp/a.sh :
#!/bin/sh echo aaa
- prepare the
swift script to run the script:
import Foundation let fileManager = FileManager.default let path = fileManager.currentDirectoryPath func shell(launchPath: String, arguments: [String] = []) -> NSString? { let task = Process() task.launchPath = launchPath task.arguments = arguments let pipe = Pipe() task.standardOutput = pipe task.launch() let data = pipe.fileHandleForReading.readDataToEndOfFile() let output = NSString(data: data, encoding: String.Encoding.utf8.rawValue) return output } if let output = shell(launchPath: "/tmp/a.sh", arguments: []) { print(output) }
Output:
... *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'launch path not accessible'
- change the permission of
/tmp/a.sh to 777 (giving permission to execute), and then re-run the swift script:
aaa
source share