将包含变音符号的密码作为 Swift 中的参数传递给 Process 时出现问题 [英] Problems when passing password containing umlauts to Process as argument in Swift

查看:51
本文介绍了将包含变音符号的密码作为 Swift 中的参数传递给 Process 时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我正在尝试使用以下代码使用 Swift 中的 sudo 执行命令(如建议的 此处:

So I am trying to execute a command using sudo from Swift using the following code (as suggested here:

func doTask(_ password:String) {
    let taskOne = Process()
    taskOne.launchPath = "/bin/echo"
    taskOne.arguments = [password]

    let taskTwo = Process()
    taskTwo.launchPath = "/usr/bin/sudo"
    taskTwo.arguments = ["-S", "/usr/bin/xattr", "-d", "-r", "com.test.exemple", " /Desktop/file.extension"]
    //taskTwo.arguments = ["-S", "/usr/bin/touch", "/tmp/foo.bar.baz"]

    let pipeBetween:Pipe = Pipe()
    taskOne.standardOutput = pipeBetween
    taskTwo.standardInput = pipeBetween

    let pipeToMe = Pipe()
    taskTwo.standardOutput = pipeToMe
    taskTwo.standardError = pipeToMe

    taskOne.launch()
    taskTwo.launch()

    let data = pipeToMe.fileHandleForReading.readDataToEndOfFile()
    let output : String = NSString(data: data, encoding: String.Encoding.utf8.rawValue) as! String
    print(output)
}

它适用于诸如test"、password#123"之类的密码.等等.但是当我尝试包含变音符号(例如ä",ü")的密码时,或ö"在不起作用.任何想法为什么?

It works just fine for passwords such as "test", "password#123" etc. But when I try a password containing an umlaut such as "ä","ü" or "ö" in doesn't work. Any ideas why?

推荐答案

我不知道为什么通过 echo 传递的另一个问题的答案似乎引入了不必要的复杂性和未知数.

I'm not sure why the answer to the other question piped through echo... seems to introduce unnecessary complications and unknowns.

以下更直接的方法经过测试并有效:

The following more direct approach is tested and working:

import Foundation

let password = "äëïöü"
let passwordWithNewline = password + "\n"
let sudo = Process()
sudo.launchPath = "/usr/bin/sudo"
sudo.arguments = ["-S", "/bin/ls"]
let sudoIn = Pipe()
let sudoOut = Pipe()
sudo.standardOutput = sudoOut
sudo.standardError = sudoOut
sudo.standardInput = sudoIn
sudo.launch()

// Show the output as it is produced
sudoOut.fileHandleForReading.readabilityHandler = { fileHandle in
    let data = fileHandle.availableData
    if (data.count == 0) { return }
    print("read \(data.count)")
    print("\(String(bytes: data, encoding: .utf8) ?? "<UTF8 conversion failed>")")

}
// Write the password
sudoIn.fileHandleForWriting.write(passwordWithNewline.data(using: .utf8)!)

// Close the file handle after writing the password; avoids a
// hang for incorrect password.
try? sudoIn.fileHandleForWriting.close()

// Make sure we don't disappear while output is still being produced.
sudo.waitUntilExit()
print("Process did exit")

关键是你必须在密码后添加一个换行符.(我想在某些方面 echo 只是一种过于复杂的方法!)

The crux is that you must add a newline after the password. (I suppose in some ways echo is just an overly complicated way of doing that!)

这篇关于将包含变音符号的密码作为 Swift 中的参数传递给 Process 时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆