0

How do I write logic for

Test total=100
Test scored=80 
Tests failed=0

I need to write in a bash file the script to check if Test total, Test scored and Tests failed=0 exists in a file. I need to use grep command.. but how do i check for new line in this case?

Also the number 100,80, I don't need to look for. Just the text before that.

3 Answers 3

1
    grep -e "Test total=[0-9][0-9]*$" -e "Test scored=[0-9][0-9]*$" -e "Tests failed=[0-9][0-9]*$" foofile

Considering you only intend to match the first 3 lines for the input given below:

 Test total=100
 Test scored=80
 Tests failed=0

 Test total=100 alpha
 Test scored=80 beta
 Tests failed=0 gamma

 Test total=
 Test scored=
 Tests failed=
2
  • How does this answer the question? Jun 19, 2017 at 3:16
  • @StephenRauch, thanks for pointing my mistake. I misread the question. Now I amended my answer.
    – sob
    Jun 19, 2017 at 8:52
0

Using bash and grep, you can test the return status of multiple grep call in a function like:

#!/usr/bin/env bash
failed_zero () {
    grep -q "Test total" "$1" || grep -q "Test scored" "$1" || grep -q "Test failed=0" "$1"
    return $?
}

# test first file name on command line
failed_zero $1
echo $?
0

try

found=$( egrep  "Test total|Test scored|Tests failed" -c  file )
if [ $found -eq 3 ]
then
  ## all three test
else
  ## some failed
fi

where

  • egrep allow extended grep, that is the use of | (or)
  • -c give count of matched lines.
  • $( ) return the result of command as a variable.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .