Created
September 9, 2020 17:06
-
-
Save lamvann/46e8d3834f9e2b055ccaa1aad65335de to your computer and use it in GitHub Desktop.
Run a shell command from Kotlin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.ByteArrayOutputStream | |
fun runCommand(command: String): String { | |
return runCatching { | |
val stdout = ByteArrayOutputStream() | |
exec { | |
commandLine(*command.split(' ').toTypedArray()) | |
standardOutput = stdout | |
} | |
stdout.toString().trim() | |
}.getOrElse { "" } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment