Tran Nghi
Tran Nghi

Reputation: 53

How to handle "command not found" in bash

I have a script to check my environment, and I would like to ask your advice on how to handle "command not found" errors.

echo "...RVM Version      : $(rvm -v)"
echo "...Node Version     : $(node -v)"
echo "...Ruby Version     : $(ruby -v)"
echo "...Bundler Version  : $(bundle -v)"
echo "...Passenger Version: $(passenger -v)"

It shows "command not found" for the node and passenger commands:

...RVM Version      : rvm 1.29.7 (latest) by Michal Papis
deploy_confirm.sh: line 10: node: command not found
...Node Version     : 
...Ruby Version     : ruby 2.6.0p0 (2018-12-25 revision 66547)
...Bundler Version  : Bundler version 2.0.1
deploy_confirm.sh: line 13: passenger: command not found
...Passenger Version: 

Instead of showing an error I would like to show "Not found", like this:

...RVM Version      : rvm 1.29.7 (latest) by Michal Papis
...Node Version     : Not found
...Ruby Version     : ruby 2.6.0p0 (2018-12-25 revision 66547)
...Bundler Version  : Bundler version 2.0.1
...Passenger Version: Not found

Upvotes: 5

Views: 3569

Answers (3)

Cyrus
Cyrus

Reputation: 88583

Insert this before your first echo line if you use bash >= 4.0:

command_not_found_handle() { echo "not found"; return 127; }

And then insert this after your last echo line to get rid of this function:

unset command_not_found_handle

Output, e.g.:

...RVM Version      : not found
...Node Version     : v4.2.6
...Ruby Version     : ruby 2.3.1p112 (2016-04-26) [x86_64-linux-gnu]
...Bundler Version  : not found
...Passenger Version: not found

Upvotes: 5

John Kugelman
John Kugelman

Reputation: 361585

Use 2>/dev/null to suppress the error message and || as short hand for else. a || b will run b if command a fails.

echo "...Login Shell User : $(whoami)"
echo "...RVM Version      : $(rvm -v 2>/dev/null || echo 'Not found')"
echo "...Node Version     : $(node -v 2>/dev/null || echo 'Not found')"
echo "...Ruby Version     : $(ruby -v 2>/dev/null || echo 'Not found')"
echo "...Bundler Version  : $(bundle -v 2>/dev/null || echo 'Not found')"
echo "...Passenger Version: $(passenger -v 2>/dev/null || echo 'Not found')"

Upvotes: 3

Charles Duffy
Charles Duffy

Reputation: 295354

A wrapper function is an easy way to get customized behavior, especially in a scenario where changing that behavior globally would be undesirable.

or_not_found() {
  if type "$1" >/dev/null 2>&1; then # if given a valid command
    "$@"                             # run that command with original arguments
  else
    echo "Not found"  # write to stdout so it's captured by the command substitution
  fi
}

echo "...Login Shell User : $USER"  # much more efficient than calling whoami
echo "...RVM Version      : $(or_not_found rvm -v)"
echo "...Node Version     : $(or_not_found node -v)"
echo "...Ruby Version     : $(or_not_found ruby -v)"
echo "...Bundler Version  : $(or_not_found bundle -v)"
echo "...Passenger Version: $(or_not_found passenger -v)"

Upvotes: 4

Related Questions