Skip to content

Instantly share code, notes, and snippets.

@mlbright
Created July 15, 2022 03:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mlbright/e9e0e2b64c36e499b1d42ad45ffca25d to your computer and use it in GitHub Desktop.
Save mlbright/e9e0e2b64c36e499b1d42ad45ffca25d to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"os"
"os/exec"
"strings"
"unicode"
"mvdan.cc/sh/v3/syntax"
)
func main() {
src, err := os.ReadFile("your-bash-script.sh")
if err != nil {
panic(err)
}
r := strings.NewReader(string(src))
s, err := syntax.NewParser().Parse(r, "")
if err != nil {
panic(err)
}
nonKeywords := make(map[string]struct{})
syntax.Walk(s, func(node syntax.Node) bool {
switch x := node.(type) {
case *syntax.Word:
s := x.Lit()
if !syntax.IsKeyword(s) {
nonKeywords[s] = struct{}{}
}
}
return true
})
for k := range nonKeywords {
if IsLetter(k) {
p, err := exec.LookPath(k)
if err == nil {
fmt.Printf("%s: %s\n", k, p)
}
}
}
}
func IsLetter(s string) bool {
for _, r := range s {
if !unicode.IsLetter(r) {
return false
}
}
return true
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment