Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am writing a script to automate the configuration of software. I want to begin by checking to see if the script needs to install the software first, then configure it. If I was to check $ software --version and I get bash: command: command not found, then I know that I will want to install it first.

Does bash: command: command not found return false?

Edit: For any answers, could the answer be explained?

share|improve this question
up vote 8 down vote accepted

Yes.

$ spamegg
spamegg: command not found

$ echo $?
127

You could just do:

if software --version &>/dev/null; then
   ## True, do something
else
   ## False, do something
fi
share|improve this answer
4  
Odd, this answer was in the review queue as "flagged as low-quality because of its length and content." Looks long enough to me! (+1) – Sparhawk yesterday
    
Hey @heemayl could you explain your answer for me? – N Altun yesterday
    
@NAltun The basic answer is if a command is not found it, bash will call a function in some systems or simply shows that the command is not found..in both cases the exit status will be greater than 0 i.e. truth value will be false.. – heemayl yesterday
    
@Sparhawk, you might be interested in the Meta post on that subject. :) – Wildcard yesterday
    
@Wildcard I agree that high-rep users often post low-rep (and too-short) answer. However, this particular answer seems that it should be long enough to skip that review queue. – Sparhawk yesterday

# 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
share|improve this answer

If your intent is to check whether a particular command is available, you should do so instead of trying to execute it:

if command -v spamegg >/dev/null; then
  echo spamegg is available
else
  apt-get install spamegg
fi

Trying to execute spamegg to see if it's available is a bad approach. First, it obfuscates your code, making it look like spamegg is used to install something. Second (and more importantly), the command your're checking might exist but simply fail for some reason:

if grep >/dev/null 2>&1; then
  echo grep is available
else
  echo grep is not available
fi

This will output grep is not available even when it is.

share|improve this answer

IMHO, I don't think your approach is the best way to tackle this problem. The reason being is just because a command returns not found, doesn't mean the program isn't installed. It could simply indicate that the program is not located in any of your PATH locations.

Perhaps, a better way would be to this is actually check against the list of installed packages:

RHEL/CentOS:

grep PROGRAM_NAME <(rpm -qa --qf "%{NAME}\n")

Debian/Ubuntu:

grep PROGRAM_NAME <(dpkg --get-selections | awk '{ print $1}')
share|improve this answer
1  
While I like the solution, I don't agree with your argument. If command is installed but not found in PATH, it's unlikely to be usable anyway. – Dmitry Grigoryev yesterday
    
I see your point, however, a program could be installed in /sbin (restricted to root by default) and this user could simply have limited sudo access. If the user mistakenly does a blind install with a -y flag assuming that application is not installed, it could lead to an unintentional upgrade. – Paul Calabro yesterday
1  
But doesn't handle self compiled programs, whilst the path method does... – Martijn yesterday
    
@PaulCalabro In that case the installation script should simply run with root privileges (why whould a simple user need to install software only root can run?). In fact, the installation script is likely to run with root privileges anyway. – Dmitry Grigoryev yesterday
1  
@DmitryGrigoryev: You're assuming this is an end-user. This could be a service account used for configuration management. You might want said account to have the ability to install software (with privs granted via sudo), but not necessarily want it to be able to run /sbin/fdisk on your system. And you should avoid running as root when possible. sudo is more auditable. – Paul Calabro yesterday

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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