Generating a report from bash output Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of Generating a report from bash output without wasting too much if your time.

The question is published on by Tutorial Guruji team.

I am currently trying to write a script in Bash that can run a set of unit/integration tests. I have that part working, but what I need now is to be able to output a summary at the end. I have been trying to use grep ot grab every line which contains a number followed by the string failing, save it in a variable, and print it at the end.

So an example of a failing output is

224 passing (3m)
47 failing

What I would like to be printed at the end is something along the lines of

service01 4 failing
service03 1 failing

What I have so far is:

for service in ${array[*]}
  echo $service
  if [ $DO_API == 'true' ]
    echo 'running API tests'
    OUTPUT+="$service API $(docker exec -it $service npm run api_test | tee /dev/tty | grep -e '[0-9]+ failing' )"

  if [ $DO_CI == 'true' ]
    echo 'running CI tests'
    OUTPUT+="$service CI $(docker exec -it $service npm run ci | tee /dev/tty | grep -e '[0-9]+ failing' )"
  echo $'n'

The above script shows no output when there are failing tests, which leads me to believe it is my grep syntax at fault.

What am I doing wrong?


The following grep command will return the required output:

grep -E '[0-9]+ failing'

The pattern [0-9]+ matches one or more digits. The -E option must be used so that grep can interpret the pattern as an extended regular expression. The + quantifier is not defined as part of the basic regular expressions.

Sample run (using a file):

$ cat testfile
224 passing (3m)
47 failing

$ grep -E '[0-9]+ failing' testfile
47 failing

As an aside, this pattern can also be implemented using basic regular expressions (BRE) in the following manner:

$ grep '[0-9]{1,} failing' testfile
47 failing

In this case, the {n,m} quantifier is used to match one or more digits. The backslashes are required to give the curly braces their special meaning.

We are here to answer your question about Generating a report from bash output - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji