How to add swift variable into command

i got the output ok print(contents) but i dont know how to include that variable in the command

task.arguments = ["-c", "./file/sshpass -p **contents** ssh -o StrictHostKeyChecking=no root@149.28.28.xxx"]

Looking forward to your guidance

my command:

import Foundation

if let url = URL(string: "http://www.example.vn/test.txt") {
    do {
        let contents = try String(contentsOf: url)
        print(contents)
    } catch {
        // contents could not be loaded
    }

@discardableResult func shell(_ command: String) -> String {
    let task = Process()
    task.launchPath = "/bin/bash"
    task.arguments = ["-c", "./file/sshpass -p +contents+ ssh -o StrictHostKeyChecking=no root@149.28.28.xxx"]
    task.arguments = ["-c", "./file/sshpass -p +contents+ scp ./Lockdown/*.zip root@149.28.28.xxx:/var/www/html/xxx/"]
    let pipe = Pipe()
    task.standardOutput = pipe
    task.launch()
    let data = pipe.fileHandleForReading.readDataToEndOfFile()
    let output: String = NSString(data: data, encoding: String.Encoding.utf8.rawValue)! as String
    return output
}
// Example usage:
let t = shell("ls")
print("\(t)")
}

What is it that you are trying to do, and what issue are you encountering?

What is "that" variable?

task.arguments is an Array of strings, each string being one component of the argument list.

task.arguments = ["-c","< ... sshpass ...>",
                  "-c","<... sshpass ...>",
                  command]

However, bash only takes one -c argument, unless the second sshpass command is to be consumed by the first sshpass command.

1 Like

If you are talking about the let contents = ..., note that contents is local to the do scope, and will go out of scope when the do block completes. You probably need to make it a var outside the do block, set it within the do block (maybe include the + signs), then use its value as a command line parameter similar to the suggestion for command.

If you were typing the command at the terminal, what should it look like?

1 Like

Moving this to “Using Swift” as it does not appear to be about developing the Swift compiler.

Terms of Service

Privacy Policy

Cookie Policy