Okay, so I'm kinda stumped here.
I'm in the midst of a deep-dive into BASH, for the purposes of writing some automation scripts for my employer. Also, full disclosure here: I'm a web guy; hitherto I've known JUST enough CLI to operate git.
My issue is, about half the staff are using MacOS, the other half are using GitBash, and it seems there are different flags supported (or, more relevantly, not supported) on the two different BASH instantiations. Further conundrum: what with everyone working from home and the world ending and all, I cannot reliably demand every one of our staff "switch to distro X"/"upgrade to version Y".
Now, I know how to test if a given program is installed (though, I confess: I'm not crystal-clear why one is preferable to the other, and please: correct me if either is a terrible way of handling this), in the forms of:
type foo >/dev/null 2>&1 || { echo >&2 "COMMAND NOT FOUND"; }
...and...
[ -x "$(command -v foo)" ] || echo 'COMMAND NOT FOUND'
...but in my specific case, BOTH platforms HAVE foo
installed, but, as a for-instance, only MacOS has a -b
/--bar
modifier flag. So, how do I test to see if a given FEATURE of a command is supported? Every time I try to pass a flag to one of the tests, the program PERFORMING the test seems to believe it's directed at IT, ignores it entirely, or errors out.
How can I test to see if foo -b
/foo --bar
is a valid/installed/accessible command?
Update/Answer
Since the general consensus presented in the well-reasoned and honestly excellent answers received below appears to be a hybrid of "no, one cannot", punctuated with "there's simply too many factors to be able to reliably glean anything resembling conclusive, useful data", I'm closing out the question.
My intention here was to ascertain what the correct approach to employ was to verify the presence of a potentially non-existent flag between two different platforms - known platforms, fortunately, in my case - and the appropriate tact in this case would seem to be the one I'm currently taking: maintain two separate scripts, one for each platform.
Thank you to all who took the time to respond! It's much appreciated.
foo -b
, if it's supported by both architectures, means the same thing? IMHO, it would be better to write a script for macOS, and another for whatever GitBash is, rather than trying to dynamically change the behavior of a single script. Alternatively, encapsulate the relevant behaviors in shell function libraries that you load depending on architecture. – Kusalananda Aug 12 '20 at 06:03whoops! You're missing this feature. Wanna upgrade your shell version at this time? >Y/n $1
It's simply the knowledge I lack right now, and I cannot seem to find any sources that suggest, "Oh, sure! That's possible!" – NerdyDeeds Aug 13 '20 at 03:24