1

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?

Martin R
  • 529,903
  • 94
  • 1,240
  • 1,382
Sascha7777
  • 89
  • 8

1 Answers1

0

You can add a “variadic parameter” to your Swift function, and append the arguments to 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.

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"])
Martin R
  • 529,903
  • 94
  • 1,240
  • 1,382
  • 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 01 '18 at 07:46
  • @Sascha7777: Did you add the `!` at `Bundle.main.path(forResource: "initial", ofType: "command")!` as I suggested? – Martin R Jul 01 '18 at 07:47
  • Ok I inserted it your way. Compiles fine but has no Function: http://fs5.directupload.net/images/180701/zyvuiqh6.png and http://fs5.directupload.net/images/180701/gfjrmgfe.png – Sascha7777 Jul 01 '18 at 08:11
  • @Sascha7777: What does the shell script look like? – Martin R Jul 01 '18 at 08:16
  • @Sascha7777: That script only defines a function. Are you sure that calling `/usr/bin/bash initial.command _ExecuteIt` from the shell would call that function? – Possibly helpful: https://stackoverflow.com/a/16159057/1187415. – Martin R Jul 01 '18 at 08:22
  • I´m so stupid. Calling a function which was not defined before but later couldn´t work. :) Now it works fine. Thank you. – Sascha7777 Jul 01 '18 at 08:44