0

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
3
  • Update the error logs you see after you add the spaces around [ and ] Commented Oct 3, 2018 at 9:16
  • 1
    progoutput=$($prog -h) should be progoutput=$($prog -h 2>&1) Commented Oct 3, 2018 at 9:20
  • 1
    Could you also quote the variables as if [ "$expected" != "$progoutput" ] Commented Oct 3, 2018 at 9:21

1 Answer 1

1

actual method of string comparison

if [ "$expected" != "$progoutput" ]

or

if [[ $expected != $progoutput ]]
Sign up to request clarification or add additional context in comments.

2 Comments

That is a partial fix. But if you run yourself with eg line : progs='ls invalidxxx' and add 2>&1 to progoutput= line still both tests fail.
That was helpful so accepting. Also my expected line had to be changed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.