# example you need wget and your PATH is okay then:
# bash/ksh/.. will return exit code 127 if command not found
#
# redirect stdin and stderr to the /dev/null = if exist,
# output is not interesting
wget --help >/dev/null 2>&1
stat=$? # variable ? include last command exit status
echo "exit status:$stat"
if ((stat == 127 )) ; then # not exist/found
echo "install wget"
exit 1
fi
echo "wget exist, continue"
You can use also if before command, but that handle all exit codes which are not 0.
You can do any command and test exit code using if
# if command ; then
# works fine
# else
# not so fine
# fi
# negative testing ! = if not exit code 0 then
if ! wget --help >/dev/null 2>&1 ; then
# give err msg to the stderr and exit
echo "install wget / wget didn't work correctly" >&2
exit 1
fi
echo "wget works fine"
Before test it with if, look first working exit code
wget --help
echo $?
# will echo 0 = ok. not 0 is not ok
# if command return not 0, then you can't test using if, you need
# test using exit value = 127