Created
July 15, 2022 03:28
-
-
Save mlbright/e9e0e2b64c36e499b1d42ad45ffca25d to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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