Xcode/Swift: How to add extra argument to a bash execution


Xcode/Swift: How to add extra argument to a bash execution



I have this Code:


func syncShellExec(path: String?) {
let script = [path!]
let process = Process()
let outputPipe = Pipe()
let filelHandler = outputPipe.fileHandleForReading

process.launchPath = "/bin/bash"
process.arguments = script
process.standardOutput = outputPipe
.
.
.



In Swift I call it this way:


self.syncShellExec(path: Bundle.main.path(forResource: "initial", ofType: "command"))



Now I want to add an Extra argument for the script itself (using Functions within the Bashscript). In Terminal it would be like this:


/usr/bin/bash initial.command Do_My_Function



How to add this to the process?




1 Answer
1



You can add a “variadic parameter” to your Swift function,
and append the arguments to process.arguments:


process.arguments


func syncShellExec(path: String, args: String...) {

let process = Process()
process.launchPath = "/bin/bash"
process.arguments = [path] + args

// ...
}



Now you can call for example:


let scriptPath = Bundle.main.path(forResource: "initial", ofType: "command")!

syncShellExec(path: scriptPath)
syncShellExec(path: scriptPath, args: "Do_My_Function")
syncShellExec(path: scriptPath, args: "arg1", "arg2")



Remark: The syncShellExec() function expects a path to a script,
therefore I would not make that parameter optional and force-unwrap
it inside the function. On the other hand, Bundle.main.path(...)
would only return nil if the resource is missing. That is a
programming error so that force-unwrapping the return value is
justified.


syncShellExec()


Bundle.main.path(...)



If the number of arguments is only determined at runtime then
you can define the arguments as an array


func syncShellExec(path: String, args: [String] = )



and call it as


syncShellExec(path: scriptPath)
syncShellExec(path: scriptPath, args: ["Do_My_Function"])
syncShellExec(path: scriptPath, args: ["arg1", "arg2"])





I get this Error at process.arguments: Value of optional type 'String?' not unwrapped; did you mean to use '!' or '?'? If a add the ! I get: Binary operator '+' cannot be applied to operands of type '[String]' and 'String?'
– Sascha7777
Jul 1 at 7:46






@Sascha7777: Did you add the ! at Bundle.main.path(forResource: "initial", ofType: "command")! as I suggested?
– Martin R
Jul 1 at 7:47


!


Bundle.main.path(forResource: "initial", ofType: "command")!





Ok I inserted it your way. Compiles fine but has no Function: fs5.directupload.net/images/180701/zyvuiqh6.png and fs5.directupload.net/images/180701/gfjrmgfe.png
– Sascha7777
Jul 1 at 8:11






@Sascha7777: What does the shell script look like?
– Martin R
Jul 1 at 8:16





fs5.directupload.net/images/180701/h6kxwa34.png
– Sascha7777
Jul 1 at 8:18







By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

List of Kim Possible characters

Audio Livestreaming with Python & Flask

NSwag: Generate C# Client from multiple Versions of an API