Number Guessing Game step 8

step 8 never passes, step 13 passes sometimes

8: Welcome back, <username>! You have played <games_played> games, and your best game took <best_game> guesses.

13: You guessed it in <number_of_guesses> tries. The secret number was <secret_number>. Nice job!

#!/bin/bash

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

echo "Enter your username:"
read username

user=$($PSQL "select username, games_played, best_game from users where username = '$username'")

if [[ -z $user ]]; then
  echo "Welcome, $username! It looks like this is your first time here."
  insert_user=$($PSQL "insert into users (username) values ('$username');") 
else
  echo $user | while IFS="|" read username games_played best_game
  do
  # l: might be issue here with using 1k as default
  echo "Welcome back, $username! You have played $games_played games, and your best game took $best_game guesses."
  done
fi

rand_num=$((1 + $RANDOM % 1000))
try=0
echo "Guess the secret number between 1 and 1000:"

while  
  read num
  
  if [[ $num =~ ^[0-9]+$ ]]; then
    ((try=try+1))
  fi 

  if [[ ! $num =~ ^[0-9]+$ ]]; then
    echo "That is not an integer, guess again:"
  # if higher
  elif [[ $num > $rand_num ]]; then
    echo "It's lower than that, guess again:"
  elif [[ $num < $rand_num ]]; then
    echo "It's higher than that, guess again:"
  # if correct 
  elif [[ $num == $rand_num ]]; then
    echo "You guessed it in $try tries. The secret number was $rand_num. Nice job!"
  fi


  [[ $num != $rand_num ]]
do true; done

games=$($PSQL "select games_played from users where username = '$username'")
((games=games+1))
best_game=$($PSQL "select best_game from users where username = '$username'")

# update with new game amount
update_user=$($PSQL "update users set games_played = '$games'") 

if [[ $try < $best_game ]]; then
  update_user=$($PSQL "update users set best_game = '$try'") 
fi
--
-- PostgreSQL database dump
--

-- Dumped from database version 12.17 (Ubuntu 12.17-1.pgdg22.04+1)
-- Dumped by pg_dump version 12.17 (Ubuntu 12.17-1.pgdg22.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: users; Type: TABLE; Schema: public; Owner: freecodecamp
--

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


ALTER TABLE public.users OWNER TO freecodecamp;

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

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


ALTER TABLE public.users_user_id_seq OWNER TO freecodecamp;

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

ALTER SEQUENCE public.users_user_id_seq OWNED BY public.users.user_id;


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

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


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

INSERT INTO public.users VALUES ('bob', 2, 0, 1000);


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

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


--
-- Name: users users_pkey; Type: CONSTRAINT; Schema: public; Owner: freecodecamp
--

ALTER TABLE ONLY public.users
    ADD CONSTRAINT users_pkey PRIMARY KEY (user_id);


--
-- PostgreSQL database dump complete
--

Oh, have some errors I found that I need to clean up

edit: passes now!