Skip to content

Instantly share code, notes, and snippets.

@lamvann
Created September 9, 2020 17:06
Show Gist options
  • Save lamvann/46e8d3834f9e2b055ccaa1aad65335de to your computer and use it in GitHub Desktop.
Save lamvann/46e8d3834f9e2b055ccaa1aad65335de to your computer and use it in GitHub Desktop.
Run a shell command from Kotlin
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