首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Kotlin-native执行命令并获取输出

Kotlin-native执行命令并获取输出
EN

Stack Overflow用户
提问于 2019-07-20 18:27:37
回答 3查看 1.3K关注 0票数 4

我想知道在原生kotlin中是否有一种方法可以通过posix调用命令并接收它的终端输出。例如,我想让"git diff“命令工作,而不必创建一个临时文件,将输出写入该文件,然后从该文件读取。

所以我只找到了需要ProcessBuilder的解决方案,这在kotlin-native上是不可用的,因为它是一个Java库。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2020-10-12 11:03:48

这是mg-lolenstine发布的Kotlin Native的exec命令的改进版本,它抛出了一个异常,命令stderr而不是只返回exit(1) (这本身并不总是令人满意的行为),而且trim现在是可选的

代码语言:javascript
复制
import kotlinx.cinterop.*
import platform.posix.*

fun executeCommand(
    command: String,
    trim: Boolean = true,
    redirectStderr: Boolean = true
): String {
    val commandToExecute = if (redirectStderr) "$command 2>&1" else command
    val fp = popen(commandToExecute, "r") ?: error("Failed to run command: $command")

    val stdout = buildString {
        val buffer = ByteArray(4096)
        while (true) {
            val input = fgets(buffer.refTo(0), buffer.size, fp) ?: break
            append(input.toKString())
        }
    }

    val status = pclose(fp)
    if (status != 0) {
        error("Command `$command` failed with status $status${if (redirectStderr) ": $stdout" else ""}")
    }

    return if (trim) stdout.trim() else stdout
}
票数 4
EN

Stack Overflow用户

发布于 2019-07-20 20:57:12

我找到了一段我想要使用的工作代码,所以我将其发布在这里,供将来的读者使用!

代码语言:javascript
复制
fun executeCommand(command: String): String{
    val fp: CPointer<FILE>? = popen(command, "r")
    val buffer = ByteArray(4096)
    val returnString = StringBuilder()

    /* Open the command for reading. */
    if (fp == NULL) {
        printf("Failed to run command\n" )
        exit(1)
    }

    /* Read the output a line at a time - output it. */
    var scan = fgets(buffer.refTo(0), buffer.size, fp)
    if(scan != null) {
        while (scan != NULL) {
            returnString.append(scan!!.toKString())
            scan = fgets(buffer.refTo(0), buffer.size, fp)
        }
    }
    /* close */
    pclose(fp)
    return returnString.trim().toString()
}
票数 11
EN

Stack Overflow用户

发布于 2021-04-26 20:16:32

我使用上面的答案创建了一个运行任意本地进程/命令的工作gradle kotlin原生/jvm多平台多项目:

这是我的结果:

https://github.com/hoffipublic/minimal_kotlin_multiplatform

代码语言:javascript
复制
import kotlinx.cinterop.refTo
import kotlinx.cinterop.toKString
import platform.posix.fgets
import platform.posix.pclose
import platform.posix.popen

actual object MppProcess : IMppProcess {
    actual override fun executeCommand(
        command: String,
        redirectStderr: Boolean
    ): String? {
        val commandToExecute = if (redirectStderr) "$command 2>&1" else command
        val fp = popen(commandToExecute, "r") ?: error("Failed to run command: $command")

        val stdout = buildString {
            val buffer = ByteArray(4096)
            while (true) {
                val input = fgets(buffer.refTo(0), buffer.size, fp) ?: break
                append(input.toKString())
            }
        }

        val status = pclose(fp)
        if (status != 0) {
            error("Command `$command` failed with status $status${if (redirectStderr) ": $stdout" else ""}")
        }

        return stdout
    }
}

在jvm上

代码语言:javascript
复制
import java.util.concurrent.TimeUnit

actual object MppProcess : IMppProcess {
    actual override fun executeCommand(
        command: String,
        redirectStderr: Boolean
    ): String? {
        return runCatching {
            ProcessBuilder(command.split(Regex("(?<!(\"|').{0,255}) | (?!.*\\1.*)")))
                //.directory(workingDir)
                .redirectOutput(ProcessBuilder.Redirect.PIPE)
                .apply { if (redirectStderr) this.redirectError(ProcessBuilder.Redirect.PIPE) }
                .start().apply { waitFor(60L, TimeUnit.SECONDS) }
                .inputStream.bufferedReader().readText()
        }.onFailure { it.printStackTrace() }.getOrNull()
    }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57123836

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档