In this tutorial, you'll learn step by step how to do a little game in Shell Script
- Open a terminal on your computer
This step is for Linux users only
- Press
Ctrl
+Alt
+T
This step is for MacOS users only
- Press
Command
+Space
, then typeterm
and click on the app you want to launch
You should end up with something like this:
In the terminal, we are using this syntax:
command --options arguments
- Let's use the command
nano
, which is a basic text editor - We will give as arguments the name of the file we want to create
mas_o_menos.sh
- The full command line will look like that:
nano mas_o_menos.sh
- Press
enter
to execute it
- Your file should contain this line, type it:
echo 'Hello 42 !'
It's time to see what you did
- Run your program with the command
bash mas_o_menos.sh
Well done, you did your first Hello World !
- Indicate the interpretor to use to execute your script, the first line should be exactly:
#!/usr/bin/env bash
Your file should look like that:
#!/usr/bin/env bash
echo 'Hello 42 !'
- Tell the computer to treat your file as a program by giving execution permission
- Run
chmod +x mas_o_menos.sh
Your file is now executable, you can run it typing: ./mas_o_menos.sh
You will modify your file, in order to print in the terminal the content of the variable MESSAGE, you will be able to change the message printed by changing the value of the variable
- In your file, replace
'Hello 42 !'
by$MESSAGE
- Add a line just above to set the value of the variable MESSAGE:
MESSAGE='Hello 42 !'
- Your file to match the following:
#!/usr/bin/env bash
MESSAGE='Hello 42 !'
echo $MESSAGE
- Save & test your program by running
./mas_o_menos.sh
You will make your program able to ask something to the user and get an answer
- Add a line at the bottom of the file:
read ANSWER
- And another one to display the answer:
echo $ANSWER
- Your file should look like that:
#!/usr/bin/env bash
MESSAGE='Hello 42 !'
echo $MESSAGE
read ANSWER
echo $ANSWER
- Save & run your program to test it !
You will learn how to make your program smart
- Update the MESSAGE value to ask the age of the user:
echo 'Hello 42 ! What's the answer ?'
- Add thoses lines at the bottom of the file to check if the age is equal to 42
Be careful about special characters like spaces and the quotes
if [ "$ANSWER" = 42 ]
then
echo 'Welcome at 42 !'
else
echo 'You shall not pass !'
fi
- Save & test, you can also remove the line:
echo $ANSWER
Your file should look like this:
#!/usr/bin/env bash
MESSAGE='Hello 42 ! What's the answer ?'
echo $MESSAGE
read ANSWER
if [ "$ANSWER" = 42 ]
then
echo 'Welcome at 42 !'
else
echo 'You shall not pass !'
fi
You will learn how to do a loop to not write two times the same piece of code
- Add between
MESSAGE='Hello 42 ! What's the answer ?'
andecho $MESSAGE
the following
while true
do
- And after the line
fi
, adddone
Your file should be like that:
#!/usr/bin/env bash
MESSAGE="Hello 42 ! What's the answer ?"
while true
do
echo $MESSAGE
read ANSWER
if [ "$ANSWER" = 42 ]
then
echo 'Welcome at 42 !'
else
echo 'You shall not pass !'
fi
done
- Run your program to test it, press
Ctrl
&C
to quit
Add an exit to your program
- Between the line
echo 'Welcome at 42 !'
and the lineelse
addexit 0
You will implement the game plus or minus, the goal is to find a hidden number in the less tries possible
- Under
MESSAGE=
, addSECRET=$(date +%s | rev | cut -c1-2)
This magic command line
$(date +%s | rev | cut -c1-2)
will generate an arbitrary number between00
and99
(both included), don't try to understand it
- In the line:
if [ "$ANSWER" = 42 ]
- Replace
42
by"$SECRET"
- It should be like
if [ "$ANSWER" = "$SECRET" ]
- Replace
- Replace the line
MESSAGE=...
byMESSAGE="Hello at 42 ! What's the answer ?"
- You will add a clue:
- Add a line to check if it's greater or lower than the secret number
- Under
echo 'You shall not pass !'
, add the following:
if [ "$SECRET" -gt "$ANSWER" ]
then
echo "The secret number is greater !"
else
echo "The secret number is lower !"
fi
- You can save and try to play
You can play with a friend, tour per tour the frst one to guess it wins Your file should be like:
#!/usr/bin/env bash
MESSAGE="Hello at 42 ! What's the answer ?"
SECRET=$(date +%s | rev | cut -c1-2)
while true
do
echo $MESSAGE
read ANSWER
if [ "$ANSWER" = "$SECRET" ]
then
echo 'Welcome at 42 !'
exit 0
else
echo 'You shall not pass !'
if [ "$SECRET" -gt "$ANSWER" ]
then
echo "The secret number is greater !"
else
echo "The secret number is lower !"
fi
fi
done
You will count how many try you do
- Initialize the counter to
0
Beforewhile
: - Add
COUNT=0
Afterread
: - Add
COUNT="$(($COUNT + 1))"
Just beforeexit 0
- Add
echo "Score: - $COUNT"
Your file should look like that
#!/usr/bin/env bash
MESSAGE="Hello at 42 ! What's the password ?"
SECRET=$(date +%s | rev | cut -c1-2)
COUNT=0
while true
do
echo $MESSAGE
read ANSWER
COUNT="$(($COUNT + 1))"
if [ "$ANSWER" = "$SECRET" ]
then
echo 'Welcome at 42 !'
echo "Score: - $COUNT"
exit 0
else
echo 'You shall not pass !'
if [ "$SECRET" -gt "$ANSWER" ]
then
echo "The secret number is greater !"
else
echo "The secret number is lower !"
fi
fi
done