Search code examples
bashterminalpsql

Echo statement not showing in bash output


I wrote a script to automatically insert data from a csv file into two separate SQL tables. The script is working but it is not printing the echo statements after the validity of a test.

echo $($PSQL "TRUNCATE TABLE games, teams")

cat games.csv | while IFS=',' read YEAR ROUND WINNER OPPONENT WINNER_G OPPONENT_G
do
    TEAMS=$($PSQL "SELECT name FROM teams WHERE name='$WINNER'")
    if [[ $WINNER != "winner" ]]
      then
      if [[ -z $TEAMS ]]
        then
        INSERT_TEAM=$($PSQL "INSERT INTO teams(name) VALUES('$WINNER')")
        if [[ INSERT_TEAM == "INSERT 0 1" ]]
        then
        echo Inserted into teams, $WINNER
        fi
      fi
    fi

    TEAMS2=$($PSQL "SELECT name FROM teams WHERE name='$OPPONENT'")
    if [[ $OPPONENT != "opponent" ]]
      then
      if [[ -z $TEAMS2 ]]
        then
        INSERT_TEAM2=$($PSQL "INSERT INTO teams(name) VALUES('$OPPONENT')")
        if [[ INSERT_TEAM2 == "INSERT 0 1" ]]
        then
        echo Inserted into teams, $OPPONENT
        fi
      fi
    fi
done

after iseerting teams, its supposed to print the team names inserted for example "Inserted into teams, Brazil"


Solution

  • [[ INSERT_TEAM == "INSERT 0 1" ]] will always be false, because the string INSERT_TEAM is not equal to the string INSERT 0 1. You want to compare the content of the variable INSERT_TEAM to the string INSERT 0 1. Therefore you want to do a

    [[ $INSERT_TEAM == "INSERT 0 1" ]]

    instead. Note that this compares for an exact match. If you want to test that the string is a substring of what is stored in the variable INSERT_TEAM, the comparison becomes

    [[ $INSERT_TEAM == *"INSERT 0 1"* ]]