Executables as a general term for:

  • common binaries
  • scripts

I suppose it can be done with some fat regex, matching start of new lines, conjunctions like && and ||, inline after ; etc...but I was wondering is there a simpler way?

  • How do you define 'executables'? – garethTheRed Jun 2 '16 at 20:12
  • anything called outside of the script, so other scripts and binaries pretty much, regex alone can't handle aliases though but let's be sane and compromise on just binaries and other scripts/wrappers – untore Jun 2 '16 at 20:26
  • You'd probably have to write a parser that understands bash. For example, using regex etc, the line echo cat would pick up cat as an executable, when in fact you wouldn't want that to happen. – garethTheRed Jun 2 '16 at 20:33
  • Parsing a shell script is tricky to do right. See unix.stackexchange.com/q/228547/117549 – Jeff Schaller Jun 2 '16 at 20:41
  • You could just use strace -f, assuming of course you don't mind running the script.... – Wildcard Jun 2 '16 at 23:23

You might be happy enough just collating the "words", then pass them through the ``which` command. Perhaps like the following:

cat $SCRIPT | tr -c '[[:alnum:]-_]' '\\n' | sort -u | xargs which

The word collation in the tr phrase excludes the more "esoteric" commands like [ and such. That's where you might need to experiment unless you're just looking for "ordinary" command words. The above of course also finds command words that perhaps are not used as such in the script, and to make that distinction you'll indeed need more of a parser. And you might as well want an initial sed 's/#.*// rather than cat to exclude the comments in the script (although that fails to pass where # is included in strings).

  • this requires all the execs to be available. – untore Jun 3 '16 at 10:24
  • Indeed. That kind of was the question, wasn't it. If you rather want a list of expected but missing executables it's a notch trickier. But you might end it with xargs -I + zsh -c "which +" instead, using the which command of zsh to gain "not found" notes for all words that are not found as executables. – Ralph Rönnquist Jun 3 '16 at 11:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.