Reputation: 597361
To check if a command is available in a bash shell, I usually do:
command -v $COMMAND >/dev/null 2>&1 || {
echo >&2 "Error: this script requires the command '$COMMAND' to be available"
exit 1
}
What is the equivalent in Windows?
Upvotes: 5
Views: 1102
Reputation: 27505
You can use something very similar
%command% >nul 2>&1 || (
echo "Error: command not found"
exit /b 1
)
Granted, this will actually execute the command, but most commands will do nothing without proper parameters. If you want to be more sure, you can use %command% /? >nul 2&1
. This will try to bring the help page for the command, without executing it
Update:
To avoid executing the command entirely, consider where.exe
. It comes bundled with Vista and Windows 7. For other OSes, you can download it. Please refer to this post on how to download it
Once you have it, you can run it with /Q
switch to avoid extra output. Return code of 0 means command was found. Once thing that I found is that the where
command requires you to supply the extension.
One the other end, there is a lengthy discussion here about a batch "one-liner" that works without an extension specified (but actually fails when you do specify an extension)
Upvotes: 3