Skip to content

Commit

Permalink
Fix #7 - add missing practice for loops
Browse files Browse the repository at this point in the history
  • Loading branch information
bertvv committed Sep 16, 2024
1 parent e48a178 commit 8cd0594
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 18 deletions.
17 changes: 12 additions & 5 deletions modules/scripting_loops/040_scripting_loops_practice.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
## practice: scripting arrays
## practice: scripting tests and loops

1.In Bash Shell:
1. Write a script that uses a `for` loop to count from 3 to 7.

\* create variable named `my_arr` with empty value.
2. Write a script that uses a `for` loop to count from 1 to 17000.

\* add to my_arr values apple banana pineapple watermelon melon
strawberry
3. Write a script that uses a `while` loop to count from 3 to 7.

4. Write a script that uses an `until` loop to count down from 8 to 4.

5. Write a script that counts the number of files ending in `.txt` in
the current directory.

6. Wrap an `if` statement around the script so it is also correct when
there are zero files ending in `.txt`.

37 changes: 24 additions & 13 deletions modules/scripting_loops/050_scripting_loops_solution.md
Original file line number Diff line number Diff line change
@@ -1,61 +1,71 @@
## solution: scripting tests and loops

1\. Write a script that uses a `for` loop to count from 3 to 7.
1. Write a script that uses a `for` loop to count from 3 to 7.

```bash
#!/bin/bash

for i in 3 4 5 6 7
do
echo Counting from 3 to 7, now at $i
echo "Counting from 3 to 7, now at ${i}"
done
```

2\. Write a script that uses a `for` loop to count from 1 to 17000.
2. Write a script that uses a `for` loop to count from 1 to 17000.

```bash
#!/bin/bash
for i in `seq 1 17000`
do
echo Counting from 1 to 17000, now at $i
echo "Counting from 1 to 17000, now at ${i}"
done
```

3\. Write a script that uses a `while` loop to count from 3 to 7.
3. Write a script that uses a `while` loop to count from 3 to 7.

```bash
#!/bin/bash
i=3
while [ $i -le 7 ]
do
echo Counting from 3 to 7, now at $i
echo "Counting from 3 to 7, now at ${i}"
let i=i+1
done
```

4\. Write a script that uses an `until` loop to count down from 8 to 4.
4. Write a script that uses an `until` loop to count down from 8 to 4.

```bash
#!/bin/bash
i=8
until [ $i -lt 4 ]
do
echo Counting down from 8 to 4, now at $i
echo "Counting down from 8 to 4, now at ${i}"
let i=i-1
done
```

5\. Write a script that counts the number of files ending in `.txt` in
5. Write a script that counts the number of files ending in `.txt` in
the current directory.

```bash
#!/bin/bash
let i=0
for file in *.txt
do
let i++
done
echo "There are $i files ending in .txt"
echo "There are ${i} files ending in .txt"
```

6\. Wrap an `if` statement around the script so it is also correct when
6. Wrap an `if` statement around the script so it is also correct when
there are zero files ending in `.txt`.

```bash
#!/bin/bash
ls *.txt > /dev/null 2>&1
Expand All @@ -67,6 +77,7 @@ there are zero files ending in `.txt`.
do
let i++
done
echo "There are $i files ending in .txt"
echo "There are ${i} files ending in .txt"
fi
```

0 comments on commit 8cd0594

Please sign in to comment.