Recap - Number Guesser
Part of the Fundamentals section of Coddy's Terminal journey — lesson 73 of 82.
Challenge
EasyCreate a shell script called guesser.sh that simulates a number guessing game with a fixed sequence of guesses.
Your script should:
- Start with the shebang line (
#!/bin/bash) - Set a variable called
secretwith the value5 - Set a variable called
guesswith the initial value0 - Use a while loop that continues as long as
guessdoes not equalsecret - Inside the loop, increment
guessby 1 - Use an if statement to check the guess:
- If
guessis less thansecret, print:Guess [guess]: Too low! - If
guessequalssecret, print:Guess [guess]: Correct!
- If
Make the script executable and run it. Your output should be:
Guess 1: Too low!
Guess 2: Too low!
Guess 3: Too low!
Guess 4: Too low!
Guess 5: Correct!Hint: Use
-nefor "not equal" in the while condition,-ltfor "less than" in the if statement, and-eqfor "equal to". Increment the guess at the start of each loop iteration withguess=$((guess + 1)).
Try it yourself
All lessons in Fundamentals
4Directories
Create A DirectoryCopy A DirectoryMove And Rename A DirectoryDelete A DirectoryRecap - Directory Operations7File Content
Head And TailWord CountSort CommandUnique CommandGrep BasicsGrep With FlagsRecap - Text Detective10Log Analyzer Project
Project OverviewViewing The Log File13Shell Scripting Basics
What Is A Shell ScriptCreate And Run A ScriptVariables In ScriptsUser Input In ScriptsIf StatementFor LoopWhile LoopRecap - Number Guesser2Navigation
Print Working DirectoryList FilesChange DirectoryAbsolute vs Relative PathsHome And Root DirectoryRecap - Find Your Way8Redirection
Standard OutputOverwrite To A FileAppend To A FileStandard InputStandard ErrorRecap - Log Builder6Wildcards And Patterns
The Star WildcardThe Question Mark WildcardBracket WildcardsCombining WildcardsRecap - Selective Operations9Piping
What Is A PipeChaining Two CommandsChaining Multiple CommandsPipe With GrepRecap - Data Pipeline