Number guessing game bash script test validation not working

Hi guys,

The bash script does not seem to be passing any tests, but all other tests pass. I assume it has something to do with the script timing out or not exiting properly.

I’ll post by SQL dump and number_guess.sh below:

#!/bin/bash

PSQL="psql -X --username=freecodecamp --dbname=number_guess -t --no-align -c"

# START OF SCRIPT
# prompt user for username

echo -e "\nEnter your username:"
read USERNAME

# if username then check database
GET_USER=$($PSQL "SELECT username FROM players WHERE username = '$USERNAME'")
    # if no user
    if [[ -z $GET_USER ]]
    then
    # add user to database
    ADD_USER=$($PSQL "INSERT INTO players (username) VALUES ('$USERNAME')")
    # if successful print confirmation
      if [[ $ADD_USER == "INSERT 0 1" ]] 
      then
      echo "Welcome, $USERNAME! It looks like this is your first time here."
      fi
      else
      # get number of games played and best guess information
      GET_GAMES=$($PSQL "SELECT p.username, p.games_played, 
                          (SELECT MIN(g.guesses) FROM games g WHERE g.user_id = p.user_id)
                 FROM players p
                 WHERE p.username = '$USERNAME'")
      
      while IFS="|" read -r username games_played guesses; do
      # print welcome
      echo "Welcome back, $username! You have played $games_played games, and your best game took $guesses guesses."
      done <<< $GET_GAMES
    fi


## START OF GAME
GAME_RANDOM_NUMBER=$(( $RANDOM % 1000 + 1 ))
GUESS=0
NUM_GUESSES=1

  echo "Guess the secret number between 1 and 1000:"
  read GUESS

  while [ "$GUESS" -ne "$GAME_RANDOM_NUMBER" ] 
  do
  # increment count
  (( NUM_GUESSES++ ))
  # if guess is not a number
  if [[ ! $GUESS =~ ^[0-9]+$ ]]
  then
  echo "That is not an integer, guess again:"
  else
    # if GUESS is lower than random number
    if [ "$GUESS" -lt "$GAME_RANDOM_NUMBER" ]; then
    echo "It's higher than that, guess again:"
    # echo and read
    else
    echo "It's lower than that, guess again:"
    # echo and read
    fi
  fi
  read GUESS
  done
  
  ` # get user_id
  USER_ID=$($PSQL "SELECT user_id FROM players WHERE username = '$USERNAME'")
  # add guesses to DB
  ADD_NUM_GUESSES_TO_DB=$($PSQL "INSERT INTO games (user_id, guesses) VALUES($USER_ID, $NUM_GUESSES)")

  # increment games
  INCREMENT_GAMES_TOTAL=$($PSQL "UPDATE players SET games_played = games_played + 1 WHERE user_id = $USER_ID")

  #winning message
  echo "You guessed it in $NUM_GUESSES tries. The secret number was $GAME_RANDOM_NUMBER. Nice job!"
`

`--
-- PostgreSQL database dump
--

-- Dumped from database version 12.9 (Ubuntu 12.9-2.pgdg20.04+1)
-- Dumped by pg_dump version 12.9 (Ubuntu 12.9-2.pgdg20.04+1)

SET statement_timeout = 0;
SET lock_timeout = 0;
SET idle_in_transaction_session_timeout = 0;
SET client_encoding = 'UTF8';
SET standard_conforming_strings = on;
SELECT pg_catalog.set_config('search_path', '', false);
SET check_function_bodies = false;
SET xmloption = content;
SET client_min_messages = warning;
SET row_security = off;

DROP DATABASE number_guess;
--
-- Name: number_guess; Type: DATABASE; Schema: -; Owner: freecodecamp
--

CREATE DATABASE number_guess WITH TEMPLATE = template0 ENCODING = 'UTF8' LC_COLLATE = 'C.UTF-8' LC_CTYPE = 'C.UTF-8';


ALTER DATABASE number_guess OWNER TO freecodecamp;

\connect number_guess

SET statement_timeout = 0;
SET lock_timeout = 0;
SET idle_in_transaction_session_timeout = 0;
SET client_encoding = 'UTF8';
SET standard_conforming_strings = on;
SELECT pg_catalog.set_config('search_path', '', false);
SET check_function_bodies = false;
SET xmloption = content;
SET client_min_messages = warning;
SET row_security = off;

SET default_tablespace = '';

SET default_table_access_method = heap;

--
-- Name: games; Type: TABLE; Schema: public; Owner: freecodecamp
--

CREATE TABLE public.games (
    game_id integer NOT NULL,
    guesses integer DEFAULT 0 NOT NULL,
    user_id integer NOT NULL
);


ALTER TABLE public.games OWNER TO freecodecamp;

--
-- Name: games_game_id_seq; Type: SEQUENCE; Schema: public; Owner: freecodecamp
--

CREATE SEQUENCE public.games_game_id_seq
    AS integer
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;


ALTER TABLE public.games_game_id_seq OWNER TO freecodecamp;

--
-- Name: games_game_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: freecodecamp
--

ALTER SEQUENCE public.games_game_id_seq OWNED BY public.games.game_id;


--
-- Name: players; Type: TABLE; Schema: public; Owner: freecodecamp
--

CREATE TABLE public.players (
    user_id integer NOT NULL,
    username character varying(22) NOT NULL,
    games_played integer DEFAULT 0 NOT NULL
);


ALTER TABLE public.players OWNER TO freecodecamp;

--
-- Name: players_user_id_seq; Type: SEQUENCE; Schema: public; Owner: freecodecamp
--

CREATE SEQUENCE public.players_user_id_seq
    AS integer
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;


ALTER TABLE public.players_user_id_seq OWNER TO freecodecamp;

--
-- Name: players_user_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: freecodecamp
--

ALTER SEQUENCE public.players_user_id_seq OWNED BY public.players.user_id;


--
-- Name: games game_id; Type: DEFAULT; Schema: public; Owner: freecodecamp
--

ALTER TABLE ONLY public.games ALTER COLUMN game_id SET DEFAULT nextval('public.games_game_id_seq'::regclass);


--
-- Name: players user_id; Type: DEFAULT; Schema: public; Owner: freecodecamp
--

ALTER TABLE ONLY public.players ALTER COLUMN user_id SET DEFAULT nextval('public.players_user_id_seq'::regclass);


--
-- Data for Name: games; Type: TABLE DATA; Schema: public; Owner: freecodecamp
--

INSERT INTO public.games VALUES (1, 0, 2);
INSERT INTO public.games VALUES (2, 9, 9);


--
-- Data for Name: players; Type: TABLE DATA; Schema: public; Owner: freecodecamp
--

INSERT INTO public.players VALUES (2, 'Sam', 0);
INSERT INTO public.players VALUES (3, '0', 0);
INSERT INTO public.players VALUES (4, '1', 0);
INSERT INTO public.players VALUES (5, '0', 0);
INSERT INTO public.players VALUES (9, 'Dave', 0);


--
-- Name: games_game_id_seq; Type: SEQUENCE SET; Schema: public; Owner: freecodecamp
--

SELECT pg_catalog.setval('public.games_game_id_seq', 2, true);


--
-- Name: players_user_id_seq; Type: SEQUENCE SET; Schema: public; Owner: freecodecamp
--

SELECT pg_catalog.setval('public.players_user_id_seq', 9, true);


--
-- Name: games games_pkey; Type: CONSTRAINT; Schema: public; Owner: freecodecamp
--

ALTER TABLE ONLY public.games
    ADD CONSTRAINT games_pkey PRIMARY KEY (game_id);


--
-- Name: players players_pkey; Type: CONSTRAINT; Schema: public; Owner: freecodecamp
--

ALTER TABLE ONLY public.players
    ADD CONSTRAINT players_pkey PRIMARY KEY (user_id);


--
-- PostgreSQL database dump complete
--
`