echo "Key is '$key' => Value is '${fruits[$key]}'"
done
```
-->
---
...
...
@@ -616,10 +615,9 @@ Speed game:
1. Use the following website to get a list of random words: https://randomwordgenerator.com and put them together in a variable
2. Register the start time with `date +%s` and put it in a variable `TSTART`
3. Loop over the words and ask the user to give the number of letters. Put the answers in an associative array using the words as keys and the answers as values
3. Loop over the words and ask the user to give the number of letters. Echo the answers.
4. Register the end time in `TEND`
5. Display the total run time
6. Loop over the associative array to compute the score (number of good answers) and show it to the user
5. Display the total run time and the score.
---
...
...
@@ -661,6 +659,8 @@ a b c d e
---
<!--
### Flags
```bash
...
...
@@ -684,6 +684,8 @@ bash test_arg.sh -f 'John Smith' -a 25 -u john
---
-->
# Input/Output streams
Shells use 3 standard I/O streams
...
...
@@ -804,6 +806,13 @@ _files="$@"
---
# Hands-on exercise
<!-- Repartir de l'exercise précédent. Stocker les résultatst dans un fichier au lieu de stdin.
Puis; lire ce fichier pour compter le score. -->
---
# Functions
* "small script within a script" that you may call multiple times
> Be careful in case of "overriding commands": plaese never use an existing linux command name.
---
# Hands-on exercise
1. Write a script called `exercise_3.sh` expecting **2 arguments**. If not exactly two arguments are provided, exit with an error and show a "usage" message to the user.
1. Write a script called `exercise_4.sh` expecting **2 arguments**. If not exactly two arguments are provided, exit with an error and show a "usage" message to the user.
2. Write a function taking a **folder path** and an **extension** as arguments and giving the list of matching files to the user
3. Imagine you are running jobs taking data from two folders, each with a dedicated extension. Use the two arguments of the script as the name of the two folders and **get the two lists of files**.
4. Since your work needs to read these files, **check that all files can be read**. If some files cannot be read, display their name to the user
<!-- 3. Imagine you are running jobs taking data from two folders, each with a dedicated extension. Use the two arguments of the script as the name of the two folders and **get the two lists of files**.
4. Since your work needs to read these files, **check that all files can be read**. If some files cannot be read, display their name to the user-->
<!-- Créer un petit exercice raisonnable-->
---
...
...
@@ -1022,6 +1025,45 @@ greeting $COUNTRY
---
# Debug
Tips and techniques for debugging and troubleshooting Bash scripts
### use `set -x`
enables debugging mode : print each command that it executes to the terminal, preceded by a `+`
### check the exit code
```bash
#!/bin/bash
if[$?-ne 0 ];then
echo"Error occurred"
fi
```
---
### use `echo`
"classical" but useful technique : insert `echo` throughout your code
```bash
#!/bin/bash
echo"Value of variable x is: $x"
```
### use `set -e`
this option will cause Bash to exit with an error if any command in the script fails
---
Thank you for your attention<!--fit-->
===
---
### Running parallel processes in subshells
Processes may execute in parallel within different subshells
...
...
@@ -1069,41 +1111,4 @@ time ./manager_seq.sh
time ./manager_par.sh
```
---
# Debug
Tips and techniques for debugging and troubleshooting Bash scripts
### use `set -x`
enables debugging mode : print each command that it executes to the terminal, preceded by a `+`
### check the exit code
```bash
#!/bin/bash
if[$?-ne 0 ];then
echo"Error occurred"
fi
```
---
### use `echo`
"classical" but useful technique : insert `echo` throughout your code
```bash
#!/bin/bash
echo"Value of variable x is: $x"
```
### use `set -e`
this option will cause Bash to exit with an error if any command in the script fails