英文:
Go: How to spawn a bash shell
问题
我正在寻找一种在Go中实现的方法,可以在登录时将用户启动到另一个shell中。因此,这个Go应用程序将在登录时执行一些工作,然后将用户放入一个bash shell中,在退出时执行更多工作然后退出。我似乎无法进入一个shell。
package main
import (
"fmt"
"os/exec"
)
func main() {
proc := exec.Command("/bin/bash")
out, e := proc.StdoutPipe()
proc.Start()
fmt.Println("Ran a shell in go")
fmt.Println(out)
fmt.Println(e)
}
这段代码会立即退出。
英文:
I'm looking to do something in Go where I launch the user into another shell on login. So this go app will do some work on login and then drop the user in a bash shell and then on exit do more work then quit. I can't seem to get it into a shell.
package main
import (
"fmt"
"os/exec"
)
func main() {
proc := exec.Command("/bin/bash")
out, e := proc.StdoutPipe()
proc.Start()
fmt.Println("Ran a shell in go")
fmt.Println(out)
fmt.Println(e)
}
That just exits right away.
答案1
得分: 2
好的,以下是代码的中文翻译:
去弄清楚.. 发布后弄清楚了,但这是有效的代码
package main
import (
"fmt"
"os"
"os/exec"
)
func main() {
shell := exec.Command("/bin/bash")
shell.Stdout = os.Stdout
shell.Stdin = os.Stdin
shell.Stderr = os.Stderr
shell.Run()
fmt.Println("在Go中运行了一个shell")
}
希望对你有帮助!
英文:
Go figure.. figure it out after I post but here's what worked
package main
import (
"fmt"
"os"
"os/exec"
)
func main() {
shell := exec.Command("/bin/bash")
shell.Stdout = os.Stdout
shell.Stdin = os.Stdin
shell.Stderr = os.Stderr
shell.Run()
fmt.Println("Ran a shell in go")
}
答案2
得分: 1
我必须承认,我并不完全理解这个问题。你需要一个新的 shell,在 shell 中?
如果我理解正确,下面的代码可能会有所帮助:
syscall.Exec(os.Getenv("SHELL"), []string{os.Getenv("SHELL")}, syscall.Environ())
英文:
I must admit, I don't fully understand the problem. You need a new shell, in the shell?
If I understand correctly, something like the following might help:
syscall.Exec(os.Getenv("SHELL"), []string{os.Getenv("SHELL")}, syscall.Environ())
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论