{"id":22821912,"url":"https://github.com/z-bj/number-guessing-script","last_synced_at":"2026-04-13T21:02:15.066Z","repository":{"id":135482026,"uuid":"600380513","full_name":"z-bj/Number-Guessing-Script","owner":"z-bj","description":"Number — GUESS INC. —  \"guessing\"  😂 Script runs in the terminal and saves the user's inputs in PostgreSQL DB until it finds the randomly generated number. ","archived":false,"fork":false,"pushed_at":"2023-03-03T15:45:27.000Z","size":102,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-06-11T03:35:50.348Z","etag":null,"topics":["bash","guessinc","number","postgresql","script","vim"],"latest_commit_sha":null,"homepage":"","language":"Shell","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/z-bj.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-02-11T10:07:15.000Z","updated_at":"2023-03-04T03:25:05.000Z","dependencies_parsed_at":null,"dependency_job_id":"d59b3ccf-3f68-4453-a995-f2b45ed78678","html_url":"https://github.com/z-bj/Number-Guessing-Script","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/z-bj/Number-Guessing-Script","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/z-bj%2FNumber-Guessing-Script","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/z-bj%2FNumber-Guessing-Script/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/z-bj%2FNumber-Guessing-Script/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/z-bj%2FNumber-Guessing-Script/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/z-bj","download_url":"https://codeload.github.com/z-bj/Number-Guessing-Script/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/z-bj%2FNumber-Guessing-Script/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":31770726,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-04-13T20:17:16.280Z","status":"ssl_error","status_checked_at":"2026-04-13T20:17:08.216Z","response_time":93,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["bash","guessinc","number","postgresql","script","vim"],"created_at":"2024-12-12T16:09:45.271Z","updated_at":"2026-04-13T21:02:15.030Z","avatar_url":"https://github.com/z-bj.png","language":"Shell","funding_links":[],"categories":[],"sub_categories":[],"readme":"![Banner](https://github.com/z-bj/Number-Guessing-Script/blob/master/NUMBER_GUESSING_GAME_BANNER.jpg)\n\n![Shell Script](https://img.shields.io/badge/shell_script-%23121011.svg?style=for-the-badge\u0026logo=gnu-bash\u0026logoColor=white)![postgreSQL](https://camo.githubusercontent.com/281c069a2703e948b536500b9fd808cb4fb2496b3b66741db4013a2c89e91986/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f506f737467726553514c2d3331363139323f7374796c653d666f722d7468652d6261646765266c6f676f3d706f737467726573716c266c6f676f436f6c6f723d7768697465)![vim](https://img.shields.io/badge/Vim-019733.svg?style=for-the-badge\u0026logo=Vim\u0026logoColor=white)\n\n\n# Number — Guess inc. — Script\n\nThis is a simple bash script that allows a user to play a number guessing game. The script prompts the user to enter their username, checks if the user exists in the database, and adds them to the database if they are new. Then, it generates a random number between 1 and 1000 and prompts the user to guess the number. The user is given hints and asked to guess again until they guess correctly. Once the user guesses the number correctly, their guess count and the secret number are stored in the database.\n\n### Usage\n\nTo run the game, execute the following command in your terminal:\n\nbashCopy code\n\n`./guessing_game.sh`\n\nYou must have PostgreSQL installed and running, and you need to set up a database with the name \"number_guess\". The script assumes that the database has a \"players\" table with \"user_id\" (serial), \"username\" (text), and \"timestamp\" (timestamp) columns, and a \"games\" table with \"game_id\" (serial), \"user_id\" (integer), \"secret_number\" (integer), \"number_of_guesses\" (integer), and \"timestamp\" (timestamp) columns.\n\n### Script\n\n``` bash\n\n#!/bin/bash\n\n# variable to query the database\nPSQL=\"psql --username=freecodecamp --dbname=number_guess -t --no-align -c\"\n\n\n# promp player for username\necho -e \"\\nEnter your username:\"\nread USERNAME\n\n# get username data\nUSERNAME_RESULT=$($PSQL \"SELECT username FROM players WHERE username='$USERNAME'\")\n# get user id\nUSER_ID_RESULT=$($PSQL \"SELECT user_id FROM players WHERE username='$USERNAME'\")\n\n# if player was not found\nif [[ -z $USERNAME_RESULT ]]\n  then\n    # greet gamer \n    echo -e \"\\nWelcome, $USERNAME! It looks like this is your first time here.\\n\"\n    # add player to database\n    INSERT_USERNAME_RESULT=$($PSQL \"INSERT INTO players(username) VALUES ('$USERNAME')\")\n    \n  else\n    \n    GAMES_PLAYED=$($PSQL \"SELECT COUNT(game_id) FROM games LEFT JOIN players USING(user_id) WHERE username='$USERNAME'\")\n    BEST_GAME=$($PSQL \"SELECT MIN(number_of_guesses) FROM games LEFT JOIN players USING(user_id) WHERE username='$USERNAME'\")\n\n    echo Welcome back, $USERNAME\\! You have played $GAMES_PLAYED games, and your best game took $BEST_GAME guesses.\nfi\n\n# generate random number between 1 and 1000\nSECRET_NUMBER=$(( RANDOM % 1000 + 1 ))\n\n# variable to store number of guesses/tries\nGUESS_COUNT=0\n\n# prompt first guess\necho \"Guess the secret number between 1 and 1000:\"\nread USER_GUESS\n\n\n# loop to prompt user to guess until correct\nuntil [[ $USER_GUESS == $SECRET_NUMBER ]]\ndo\n  \n  # check guess is valid/an integer\n  if [[ ! $USER_GUESS =~ ^[0-9]+$ ]]\n    then\n      # request valid guess\n      echo -e \"\\nThat is not an integer, guess again:\"\n      read USER_GUESS\n      # update guess count\n      ((GUESS_COUNT++))\n    \n    # if its a valid guess\n    else\n      # check inequalities and give hint\n      if [[ $USER_GUESS \u003c $SECRET_NUMBER ]]\n        then\n          echo \"It's higher than that, guess again:\"\n          read USER_GUESS\n          # update guess count\n          ((GUESS_COUNT++))\n        else \n          echo \"It's lower than that, guess again:\"\n          read USER_GUESS\n          #update guess count\n          ((GUESS_COUNT++))\n      fi  \n  fi\n\ndone\n\n# loop ends when guess is correct so, update guess\n((GUESS_COUNT++))\n\n# get user id\nUSER_ID_RESULT=$($PSQL \"SELECT user_id FROM players WHERE username='$USERNAME'\")\n# add result to game history/database\nINSERT_GAME_RESULT=$($PSQL \"INSERT INTO games(user_id, secret_number, number_of_guesses) VALUES ($USER_ID_RESULT, $SECRET_NUMBER, $GUESS_COUNT)\")\n\n# winning message\necho You guessed it in $GUESS_COUNT tries. The secret number was $SECRET_NUMBER. Nice job\\!\n\n# some comment\n\n```\n\n\n\n\u003cimg src=\"https://github.com/z-bj/Number-Guessing-Script/blob/master/deployparrot.gif\" width=\"36\"\u003e\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fz-bj%2Fnumber-guessing-script","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fz-bj%2Fnumber-guessing-script","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fz-bj%2Fnumber-guessing-script/lists"}