I am trying to check if a program is installed on a linux system by comparing command output with an expected output. I expect the program not to be present and so if program is abc then I should see:
bash: abc: command not found
But instead I am getting this output:
Shell for this session: bash
./checkprogs.sh: line 17: [: too many arguments
addgroup feature enabled test 1
./checkprogs.sh: line 15: tftp: command not found
./checkprogs.sh: line 17: [: too many arguments
tftp feature enabled test 2
2 tests performed
Here is the bash code:
ourshell="/bin/bash"
ourshell=${ourshell#*bin/}
echo "Shell for this session: $ourshell"
counter=1
# list of programs we want to test if available
progs='addgroup tftp'
for prog in $progs
do
expected="$ourshell: $prog: command not found"
#echo "$expected"
progoutput=$($prog -h)
#echo "prog output is: $progoutput"
if [ $expected != $progoutput ]
then
echo "test failed"
fi
echo "$prog feature enabled test $counter"
((counter++))
done
((counter--))
echo "$counter tests performed"
How can I fix this?
On the system I am working on, the shell is bash and addgroup is present but tftp is not.
Please note that I am on an embedded platform and "expect" is not installed. Assume minimal linux installation.
EDIT: had to add space after [ and before ] - but even after fixing that still doesn't work as expected.
EDIT: sorry I am new to bash scripting...
If I quote the strings inside [ ] and add 2>&1 to progoutput then code looks like this:
progoutput=$($prog -h 2>&1) # line 15
if [ "$expected" != "$progoutput" ] # line 17
and just so anyone can try this:
progs='ls invalidxxx' # line 9
then output is:
Shell for this session: bash
test failed
ls feature enabled test 1
test failed
invalidxxx feature enabled test 2
2 tests performed