diff --git a/Bash.md b/Bash.md index 16c73f08216c6e2970ba00307c721492c0d54399..2b6f1ddba4dc1bc655a7a2e249c0b5f94b721d05 100644 --- a/Bash.md +++ b/Bash.md @@ -5,7 +5,7 @@ author: P.Y. Barriat description: Introduction to Bash Scripting backgroundImage: url('assets/back.png') _backgroundImage: url('assets/garde.png') -footer: 17/10/2023 | Introduction to Bash Scripting +footer: 10/10/2024 | Introduction to Bash Scripting _footer: "" paginate: true _paginate: false @@ -254,25 +254,14 @@ Consider `string=abcABC123ABCabc` and `filename=myfile.txt` * `${string:7:3}` is `23A` * `${string:(-4)}` or `${string: -4}` is `Cabc` * substring removal from front : - * `${string#a*C}` is `123ABCabc` * `${filename##myfile}` is `.txt` - -<!-- WARNING --> - ---- - * substring removal from back : - * `${string%b*c}` is `abcABC123ABCa` * `${filename%%.txt}` is `myfile` -## Variable expansion +<!--## Variable expansion * `${variable-default}` : - if `variable` is unset or null, the expansion of `default` is substituted -* `${variable+default}` : - if `variable` is unset or null, nothing is substituted, otherwise the expansion of `default` is substituted - -<!-- WARNING --> + if `variable` is unset or null, the expansion of `default` is substituted--> --- @@ -454,12 +443,16 @@ esac 1. In your `bash_exercises` folder create a new bash file called `exercise_2.sh` and make it executable 2. Ask the user for two numbers smaller or equal to 100, put them in variables `NUMBER1` and `NUMBER2` -> `read NUMBER1` -> `read NUMBER2` -3. Do the following only if the numbers are smaller than 100 -4. Otherwise, check if both numbers are even, and tell the user -5. If the user's numbers ar too big, tell them -> `echo 'message'` + + > ```bash + > #!/bin/bash + > read NUMBER1 + > read NUMBER2 + >``` + +3. Check if the numbers are smaller than 100 + - If yes, check if both numbers are even and tell the user + - If not, tell the user with `echo 'my message'` --- @@ -552,6 +545,8 @@ Basic loop structures in Bash scripting : ### Examples ```bash +#!/bin/bash + # Basic while loop counter=0 while [ $counter -lt 3 ]; do @@ -611,13 +606,12 @@ done # Hands-on exercise -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. Echo the answers. -4. Register the end time in `TEND` -5. Display the total run time and the score. +1. In your `bash_exercises` folder create a new bash file called `exercise_3.sh` and make it executable +2. Use the following website to get a list of 10 random words: https://randomwordgenerator.com and put them together in an array +3. Register the start time with `date +%S` and put it in a variable `tstart` +4. Loop over the words and ask the user to give the number of letters. Echo the answers. +5. Register the end time in `tend` +6. Display the total run time and the total number of letters. --- @@ -808,8 +802,10 @@ _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. --> +1. In your `bash_exercises` folder, copy `exercise_3.sh` to `exercise_4.sh` +2. In this new file, loop over the words and write the number of letters of each word in a new file called `output.txt` +3. Now loop over the created file `output.txt` to get the total number of letters +4. Display the total run time and the total number of letters --- @@ -827,11 +823,10 @@ hello_world () { } hello_world ``` +Functions must be declared **before** they are used > defining a function doesn’t execute it -Functions must be declared **before** they are used - --- ## Variables Scope @@ -919,7 +914,7 @@ print_something Mars # Hands-on exercise -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. +1. Write a script called `exercise_5.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--> @@ -1109,6 +1104,4 @@ echo "done" ```bash time ./manager_seq.sh time ./manager_par.sh -``` - ---- \ No newline at end of file +``` \ No newline at end of file