Skip to content

Projects#

Let's play a little game. You're going to create some directories and then play a guessing game. Run this one liner script:

1
game() { GAME=$(date +%s%N); GUESS=$((1 + $RANDOM % 5)); echo "Game ${GAME}: ${GUESS}" | tee "number/${GUESS}_${GAME}" ; }

Note

Look don't worry about the echo command we're using here, the weird syntax, the use of $RANDOM, and all the other weird stuff that's going on here - that's all for later.

  1. Create one directory: number_game
    1. Inside of that directory, I want you to create three more number, winner and loser
      1. Can you do this without using cd to go inside the directory first?
  2. Move into the number_game directory
  3. Now I want you to guess a number between 1 and 5 (including themselves)
  4. Then run our game: game
  5. Did you guess the number correctly? Move the file from number/<number>_<game> to winner/
    1. (sneaky tip: type number then hit Tab+Tab)
  6. You guessed wrong? Move the file from number/<number>_<game> to loser/
  7. Player a few rounds
  8. When you're done, learn how-to count the number of results you got!
Solution

Here's what my session looked like:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
michael@develop:/$ game() { GAME=$(date +%s%N); GUESS=$((1 + $RANDOM % 5)); echo "Game ${GAME}: ${GUESS}" | tee "number/${GUESS}_${GAME}" ; }
michael@develop:/$ cd ~
michael@develop:~$ mkdir number_game
michael@develop:~$ mkdir number_game/winner
michael@develop:~$ mkdir number_game/number
michael@develop:~$ mkdir number_game/loser
michael@develop:~$ cd number_game/
michael@develop:~/number_game$ game
Game 1647841105104584572: 3
michael@develop:~/number_game$ mv number/3_1647841105104584572 loser/
michael@develop:~/number_game$ game
Game 1647841115843845025: 4
michael@develop:~/number_game$ mv number/4_1647841115843845025 loser/
michael@develop:~/number_game$ game
Game 1647841146946517633: 1
michael@develop:~/number_game$ mv number/1_1647841146946517633 winner/
michael@develop:~/number_game$ tree
.
├── loser
│   ├── 3_1647841105104584572
│   └── 4_1647841115843845025
├── number
└── winner
    └── 1_1647841146946517633

3 directories, 3 files

But what if I played hundreds of games? How do I count the results?

Finalise this section project by deleting everything with a single command. You can do it!