package termenvimport ()var ( cmdList = []string{"cmd", "cmd.exe"} pwshList = []string{"powershell", "powershell.exe", "pwsh", "pwsh.exe"})// IsTerminal 检查是否为终端设备中func () bool { := int(os.Stdout.Fd())returnterm.IsTerminal()}// CurrentShell get current used shell env file.//// eg "/bin/zsh" "/bin/bash".// if onlyName=true, will return "zsh", "bash"func ( bool, ...string) string {returncomfunc.CurrentShell(, ...)}// HasShellEnv has shell env check.//// Usage://// HasShellEnv("sh")// HasShellEnv("bash")func ( string) bool {// can also use: "echo $0" , := shellExec("echo OK", )if != nil {returnfalse }returnstrings.TrimSpace() == "OK"}// IsShellSpecialVar reports whether the character identifies a special// shell variable such as $*.func ( uint8) bool {switch {case'*', '#', '$', '@', '!', '?', '-', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':returntrue }returnfalse}func shellExec(, string) (string, error) {// "-c" for bash,sh,zsh shell := "-c"if == "" { = CurrentShell(true, "sh") }// special for Windows shellifruntime.GOOS == "windows" {// use cmd.exe, mark is "/c"ifcheckfn.StringsContains(cmdList, ) { = "/c" } elseifcheckfn.StringsContains(pwshList, ) {// "-Command" for powershell = "-Command" } } := exec.Command(, , ) , := .CombinedOutput()returnstring(), }
The pages are generated with Goldsv0.8.4. (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu.
PR and bug reports are welcome and can be submitted to the issue list.
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds.