2

I am reading a text file line by line and taking the count of all lines as a part of my requirement.

When there is blank line then it get messed up. I tried with if condition for [ -z "$line" ] , however not able to succeed.

Here is my current code:

countNumberOfCases() {
echo "2.  Counting number of test cases -----------"
    cd $SCRIPT_EXECUTION_DIR
    FILE_NAME=Features
    while read line || [[ -n "$line" ]]
    do
        TEST_CASE="$line"                       
        if [ "${TEST_CASE:0:1}" != "#" ] ; then
            cd $MVN_EXECUTION_DIR
                runTestCase     
        fi
    done < $FILE_NAME

    echo " v_ToalNoOfCases : = " $v_ToalNoOfCases
}

And below is Features file

web/sprintTwo/TC_002_MultipleLoginScenario.feature
#web/sprintOne/TC_001_SendMoneyTransaction_Spec.feature

web/sprintTwo/TC_003_MultipleLoginScenario.feature
#web/sprintOne/TC_004_SendMoneyTransaction_Spec.feature

When there is blank line it wont work properly so my requirement is that if there is blank line then it should be skipped and should not get considered.

codeforester
  • 39,467
  • 16
  • 112
  • 140
Durgesh
  • 585
  • 4
  • 12
  • 33

1 Answers1

1

You can write your loop in a little more robust way:

#!/bin/bash

while read -r line || [[ $line ]]; do                                 # read lines one by one
  cd "$mvn_execution_dir" # make sure this is an absolute path
                          # or move it outside the loop unless "runTestCase" function changes the current directory
  runTestCase "$line"     # need to pass the argument?
done < <(sed -E '/^[[:blank:]]*$/d; /^[[:blank:]]+#/d' "$file_name")  # strip blanks and comments

A few things:

codeforester
  • 39,467
  • 16
  • 112
  • 140
  • 2
    Should the `cd "$mvn_execution_dir"` not be done outside of the loop? I'm thinking of the case where `mvn_execution_dir` holds a relative path. – user1934428 May 04 '18 at 06:07
  • 2
    It's working fine. Thanks for sharing important information as well. – Durgesh May 04 '18 at 06:17