Search code examples
bashshellsh

Multiple if conditions in shell script


I am using shell script and trying below where I searching for two strings in a file if they are passing then I need to print "PASS" as shown in below code but getting issue when executing below code.

#!/bin/bash
if [[ (grep -q "Build of target from_sms.compfiles.xf : PASS" failed.log) && (grep -q "All tests fine" test.log) ]]; then
  echo "PASS"
fi

Solution

  • You dont need to put square brackets around grep command as it already returns "0" or "1" which is what exactly if needs.

    here for example:

    $ grep -q PASS /tmp/file
    $ echo $?
    0 
    $ grep -q FOO /tmp/file
    $ echo $?
    1
    

    So your code could be:

    $ if grep -q PASS /tmp/file && grep -q "All tests fine"  /tmp/file2 ; then echo "PASS"; fi
    PASS
    $ cat /tmp/file
    PASS
    $ cat /tmp/file2
    All tests fine
    

    let me know if this is not what you are looking for.