Skip to content

Commit b4b77ec

Browse files
Add exercises for individual shell tools and shell pipelines (#1)
Achieving these exercises meets all of the shell tools learning objectives of the first two sprints. --------- Co-authored-by: Alasdair Smith <[email protected]>
1 parent 9360927 commit b4b77ec

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

91 files changed

+598
-0
lines changed

Diff for: individual-shell-tools/README.md

+24

Diff for: individual-shell-tools/awk/README.md

+9

Diff for: individual-shell-tools/awk/scores-table.txt

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
Ahmed London 1 10 4
2+
Basia London 22 9 6
3+
Mehmet Birmingham 3 12 17
4+
Leila London 1
5+
Piotr Glasgow 15 2 25 11 8
6+
Chandra Birmingham 12 6

Diff for: individual-shell-tools/awk/script-01.sh

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#!/bin/bash
2+
3+
set -euo pipefail
4+
5+
# TODO: Write a command to output just the names of each player in `scores-table.txt`.
6+
# Your output should contain 6 lines, each with just one word on it.

Diff for: individual-shell-tools/awk/script-02.sh

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#!/bin/bash
2+
3+
set -euo pipefail
4+
5+
# TODO: Write a command to output the names of each player, as well as their city.
6+
# Your output should contain 6 lines, each with two words on it, separated by a space.

Diff for: individual-shell-tools/awk/script-03.sh

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!/bin/bash
2+
3+
set -euo pipefail
4+
5+
# TODO: Write a command to output just the names of each player along with the score from their first attempt.
6+
# Your output should contain 6 lines, each with one word and one number on it.
7+
# The first line should be "Ahmed 1".

Diff for: individual-shell-tools/awk/script-04.sh

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!/bin/bash
2+
3+
set -euo pipefail
4+
5+
# TODO: Write a command to output just the names of each player in London along with the score from their last attempt.
6+
# Your output should contain 3 lines, each with one word and one number on it.
7+
# The first line should be "Ahmed 4".

Diff for: individual-shell-tools/awk/script-05.sh

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!/bin/bash
2+
3+
set -euo pipefail
4+
5+
# TODO: Write a command to output just the names of each player along with the number of times they've played the game.
6+
# Your output should contain 6 lines, each with one word and one number on it.
7+
# The first line should be "Ahmed 3".

Diff for: individual-shell-tools/awk/script-06-stretch.sh

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#!/bin/bash
2+
3+
set -euo pipefail
4+
5+
# NOTE: This is a stretch exercise - it is optional.
6+
7+
# TODO: Write a command to output the total of adding together all players' first scores.
8+
# Your output should be exactly the number 54.

Diff for: individual-shell-tools/awk/script-07-stretch.sh

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#!/bin/bash
2+
3+
set -euo pipefail
4+
5+
# NOTE: This is a stretch exercise - it is optional.
6+
7+
# TODO: Write a command to output just the names of each player along with the total of adding all of that player's scores.
8+
# Your output should contain 6 lines, each with one word and one number on it.
9+
# The first line should be "Ahmed 15". The second line should be "Basia 37"

Diff for: individual-shell-tools/cat/script-01.sh

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#!/bin/bash
2+
3+
set -euo pipefail
4+
5+
# TODO: Write a command to output the contents of the helper-1.txt file inside the helper-files directory to the terminal.
6+
# The output of this command should be "Once upon a time...".

Diff for: individual-shell-tools/cat/script-02.sh

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#!/bin/bash
2+
3+
set -euo pipefail
4+
5+
# TODO: Write a command to output the contents of all of the files inside the helper-files directory to the terminal.
6+
# Make sure you are only calling `cat` once.
7+
#
8+
# The output of this command should be:
9+
# Once upon a time...
10+
# There was a house made of gingerbread.
11+
# It looked delicious.
12+
# I was tempted to take a bite of it.
13+
# But this seemed like a bad idea...

Diff for: individual-shell-tools/cat/script-03.sh

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#!/bin/bash
2+
3+
set -euo pipefail
4+
5+
# TODO: Write a command to output the contents of the file `helper-3.txt` inside the helper-files directory to the terminal.
6+
# This time, we also want to see the line numbers in the output.
7+
#
8+
# The output of this command should be something like:
9+
# 1 It looked delicious.
10+
# 2 I was tempted to take a bite of it.
11+
# 3 But this seemed like a bad idea...

Diff for: individual-shell-tools/cat/script-04-stretch.sh

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#!/bin/bash
2+
3+
set -euo pipefail
4+
5+
# NOTE: This is a stretch exercise - it is optional.
6+
7+
# TODO: Write a command to output the contents of all of the files in the helper-files directory to the terminal.
8+
# We also want to see the line numbers in the output, but we want line numbers not to reset at the start of each file.
9+
#
10+
# The output of this command should be something like:
11+
# 1 Once upon a time...
12+
# 2 There was a house made of gingerbread.
13+
# 3 It looked delicious.
14+
# 4 I was tempted to take a bite of it.
15+
# 5 But this seemed like a bad idea...

Diff for: individual-shell-tools/grep/dialogue-2.txt

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Doctor: Hello
2+
Patient: Hello Doctor
3+
Patient: Thank you so much for your help last week
4+
Doctor: No problem!

Diff for: individual-shell-tools/grep/dialogue-3.txt

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Patient: That doctor is so nice!
2+
Spouse: I'm glad he could help.
3+
Patient: Me too!

Diff for: individual-shell-tools/grep/dialogue.txt

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
Doctor: Hello
2+
Patient: Hello Doctor
3+
Doctor: What's wrong today?
4+
Patient: Hello, I can't stop saying hello
5+
Doctor: That sounds frustrating. When did this start?
6+
Patient: Hello, about two days ago.
7+
Doctor: Say "Hi".
8+
Patient: Hi
9+
Doctor: You didn't say hello
10+
Patient: I seem to be cured!
11+
Doctor: You're welcome, goodbye
12+
13+
Patient: I went to the doctor!
14+
Spouse: I'm glad you saw the Doctor: did they cure you?
15+
Patient: Yes!

Diff for: individual-shell-tools/grep/script-01.sh

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#!/bin/bash
2+
3+
set -euo pipefail
4+
5+
# TODO: Write a command to output every line in dialogue.txt said by the Doctor.
6+
# The output should contain 6 lines.

Diff for: individual-shell-tools/grep/script-02.sh

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#!/bin/bash
2+
3+
set -euo pipefail
4+
5+
# TODO: Write a command to output every line in dialogue.txt that contains the word Doctor (regardless of case).
6+
# The output should contain 9 lines.

Diff for: individual-shell-tools/grep/script-03.sh

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#!/bin/bash
2+
3+
set -euo pipefail
4+
5+
# TODO: Write a command to output the number of lines in dialogue.txt that contain the word Doctor (regardless of case).
6+
# The output should be exactly the number 9.

Diff for: individual-shell-tools/grep/script-04.sh

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#!/bin/bash
2+
3+
set -euo pipefail
4+
5+
# TODO: Write a command to output every line in dialogue.txt that does not contain the word "Hello" (regardless of case).
6+
# The output should contain 10 lines.

Diff for: individual-shell-tools/grep/script-05.sh

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#!/bin/bash
2+
3+
set -euo pipefail
4+
5+
# TODO: Write a command to output every line in dialogue.txt that contains the string "cure", as well as the line before that line.
6+
# The output should contain two pairs of two lines of text (with a separator between them).

Diff for: individual-shell-tools/grep/script-06.sh

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#!/bin/bash
2+
3+
set -euo pipefail
4+
5+
# TODO: Write a command to output the name of every `.txt` file in this directory which contains a line of dialogue said by the Doctor.
6+
# The output should contain two filenames.

Diff for: individual-shell-tools/grep/script-07.sh

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#!/bin/bash
2+
3+
set -euo pipefail
4+
5+
# TODO: Write a command to output, for each `.txt` file in this directory, how many lines of dialogue the Doctor has.
6+
# The output should show that dialogue.txt contains 6 lines, dialogue-2.txt contains 2, and dialogue-3.txt contains 0.

Diff for: individual-shell-tools/helper-files/helper-1.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Once upon a time...

Diff for: individual-shell-tools/helper-files/helper-2.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
There was a house made of gingerbread.

Diff for: individual-shell-tools/helper-files/helper-3.txt

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
It looked delicious.
2+
I was tempted to take a bite of it.
3+
But this seemed like a bad idea...
+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Once upon a time...
+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
There was a house made of gingerbread.
+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
It looked delicious.
2+
I was tempted to take a bite of it.
3+
But this seemed like a bad idea...

Diff for: individual-shell-tools/ls/script-01.sh

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#!/bin/bash
2+
3+
set -euo pipefail
4+
5+
# Do not change this part of the script - only change after the TODO comment.
6+
7+
script_dir="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" &> /dev/null && pwd)"
8+
if [[ "${script_dir}" != "$(pwd)" ]]; then
9+
echo >&2 "ERROR: You haven't cd'd into the correct directory."
10+
echo >&2 "For each exercise, you should cd to the directory containing the script before running it."
11+
exit 1
12+
fi
13+
14+
# TODO: Write a command to list the files and folders in this directory.
15+
# The output should be a list of names including child-directory, script-01.sh, script-02.sh, and more.

Diff for: individual-shell-tools/ls/script-02.sh

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#!/bin/bash
2+
3+
set -euo pipefail
4+
5+
# TODO: Write a command which lists all of the files in the directory named child-directory.
6+
# The output should be a list of names: helper-1.txt, helper-2.txt, helper-3.txt.

Diff for: individual-shell-tools/ls/script-03.sh

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!/bin/bash
2+
3+
set -euo pipefail
4+
5+
# TODO: Write a command which _recursively_ lists all of the files and folders in this directory _and_ all of the files inside those folders.
6+
# The output should be a list of names including: child-directory, script-01.sh, helper-1.txt (and more).
7+
# The formatting of the output doesn't matter.

Diff for: individual-shell-tools/ls/script-04.sh

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#!/bin/bash
2+
3+
set -euo pipefail
4+
5+
# Do not change this part of the script - only change after the TODO comment.
6+
7+
script_dir="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" &> /dev/null && pwd)"
8+
touch "${script_dir}/child-directory/helper-2.txt"
9+
sleep 1
10+
touch "${script_dir}/child-directory/helper-1.txt"
11+
sleep 1
12+
touch "${script_dir}/child-directory/helper-3.txt"
13+
14+
echo "First exercise (sorted newest to oldest):"
15+
16+
# TODO: Write a command which lists the files in the child-directory directory, one per line, sorted so that the most recently modified file is first.
17+
# The output should be a list of names in this order, one per line: helper-3.txt, helper-1.txt, helper-2.txt.
18+
19+
20+
echo "Second exercise (sorted oldest to newest):"
21+
22+
# TODO: Write a command which does the same as above, but sorted in the opposite order (oldest first).
23+
# The output should be a list of names in this order, one per line: helper-2.txt, helper-1.txt, helper-3.txt.

Diff for: individual-shell-tools/sed/input.txt

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
This is a sample file for experimenting with sed.
2+
3+
It contains many lines, and there are some things you may want to do with each of them.
4+
5+
We'll include some score information:
6+
37 Alisha
7+
15 Jacob
8+
7 Pietro
9+
3 Katya
10+
11+
We also should remember, when we go shopping, to get 4 items: oranges,cheese,bread,olives.

Diff for: individual-shell-tools/sed/script-01.sh

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!/bin/bash
2+
3+
set -euo pipefail
4+
5+
# TODO: Write a command to output input.txt with all occurrences of the letter `s` replaced with `S`.
6+
# The output should contain 11 lines.
7+
# The first line of the output should be: "ThiS iS a Sample file for experimenting with Sed.".

Diff for: individual-shell-tools/sed/script-02.sh

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!/bin/bash
2+
3+
set -euo pipefail
4+
5+
# TODO: Write a command to output input.txt with numbers removed.
6+
# The output should contain 11 lines.
7+
# Line 6 of the output should be " Alisha".

Diff for: individual-shell-tools/sed/script-03.sh

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#!/bin/bash
2+
3+
set -euo pipefail
4+
5+
# TODO: Write a command to output input.txt removing any line which contains a number.
6+
# The output should contain 6 lines.

Diff for: individual-shell-tools/sed/script-04.sh

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#!/bin/bash
2+
3+
set -euo pipefail
4+
5+
# TODO: Write a command to output input.txt replacing every occurrence of the string "We'll" with "We will".
6+
# The output should contain 11 lines.

Diff for: individual-shell-tools/sed/script-05.sh

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#!/bin/bash
2+
3+
set -euo pipefail
4+
5+
# TODO: Write a command to output input.txt with one change:
6+
# If a line starts with a number and a space, make the line instead end with a space and the number.
7+
# So line 6 which currently reads "37 Alisha" should instead read "Alisha 37".
8+
# The output should contain 11 lines.

Diff for: individual-shell-tools/sed/script-06.sh

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#!/bin/bash
2+
3+
set -euo pipefail
4+
5+
# TODO: Write a command to output input.txt with one fix:
6+
# If a comma in input.txt is not followed by a space, add a space after.
7+
# If there is already a space after a comma, do not add an additional space.
8+
# The output should contain 11 lines.
9+
# Line 3 should be "It contains many lines, and there are some things you may want to do with each of them.".
10+
# Line 11 should be "We also should remember, when we go shopping, to get 4 items: oranges, cheese, bread, olives.".

Diff for: individual-shell-tools/wc/script-01.sh

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#!/bin/bash
2+
3+
set -euo pipefail
4+
5+
# TODO: Write a command to output the number of words in the file helper-files/helper-3.txt.
6+
# The output should include the number 19. The output should not include the number 92.

Diff for: individual-shell-tools/wc/script-02.sh

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#!/bin/bash
2+
3+
set -euo pipefail
4+
5+
# TODO: Write a command to output the number of lines in the file helper-files/helper-3.txt.
6+
# The output should include the number 3. The output should not include the number 19.

Diff for: individual-shell-tools/wc/script-03.sh

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#!/bin/bash
2+
3+
set -euo pipefail
4+
5+
# TODO: Write a command to output the number of lines, words, and characters in all of the files inside the helper-files directory.
6+
# The output should be something like:
7+
# 1 4 20 ../helper-files/helper-1.txt
8+
# 1 7 39 ../helper-files/helper-2.txt
9+
# 3 19 92 ../helper-files/helper-3.txt
10+
# 5 30 151 total

0 commit comments

Comments
 (0)