Skip to content
Extraits de code Groupes Projets
Valider 15629a0c rédigé par Jérôme de Favereau de Jeneret's avatar Jérôme de Favereau de Jeneret
Parcourir les fichiers

first part of updates

parent f59befd8
Aucune branche associée trouvée
Aucune étiquette associée trouvée
1 requête de fusion!1Add exercises, some edits
......@@ -80,6 +80,7 @@ echo $STRING
---
<!-- JDF: je pense qu c'est trop tôt pur cette info
# Shell syntax rules
Shells use 3 **"standard I/O streams"**
......@@ -93,19 +94,21 @@ Shell has several **meta-characters** and **control operators**
> `|`, `&`, `>`, `;` , etc.
---
-->
# Bash environment
In a Bash shell many things constitute your environment
- the form of your prompt
- the form of your 'prompt' (what comes left of your commands)
- your home directory and your working directory
- the name of your shell
- functions that you have defined
- etc.
Environment includes many variables that may have been set **by bash** or **by you**
> Access the value of a variable by prefixing its name with `$`
**Access the value of a variable by prefixing its name with `$`**
---
......@@ -119,8 +122,11 @@ Environment includes many variables that may have been set **by bash** or **by y
| `SHELL` | the name of the shell |
| `UID` | the numeric user id of the logged-in user |
So to get the value of `USER` you would use `$USER` in bash code
> You can use special files to control bash variables : `$HOME/.bashrc`
---
# Bash Scripting basics
......@@ -131,13 +137,15 @@ By naming convention, bash scripts end with `.sh`
A good practice is to define a `shebang` : first line of the script, `shebang` is simply an absolute path to the shell interpreter (see `echo $SHELL` result)
> combination of `bash #` and `bang !`
The usual shebang for bash is `#!/bin/bash`
---
### Comments start with `#`
On a line, any characters after `#` will be ignored -- *with the exception of* `#!`
On a line, any characters after `#` will be ignored (with the exception of `#!`)
```bash
echo "A comment will follow." # Comment here.
......@@ -150,7 +158,7 @@ echo "A comment will follow." # Comment here.
- Use something your editor makes easy (**Vim** uses `Tab`)
---
<!-- JDF: je pnseque c'est trop tôt pour ceci
### Command separators
Commands can be combined using **meta-characters** and **control operators**
......@@ -167,11 +175,11 @@ $ cd myfolder || ls # if failed cd to myfolder, `ls` will run
```
---
-->
# Permissions and execution
- Bash script is nothing else just a **text file** containing instructions to be executed sequentially
> by default in Linux, a new text file is **-rw-r--r--** (or 644)
> by default in Linux, a new text file's permissons are **-rw-r--r--** (or 644)
- You can run the script `hello_world.sh` using
* `sh hello_world.sh`
* `bash hello_world.sh`
......@@ -182,6 +190,15 @@ $ cd myfolder || ls # if failed cd to myfolder, `ls` will run
# Hands-on exercise
Your first bash script:
1. create a folder `bash_exercises` and go there
1. use your favourite editor (vim, obviously) to create a new file called `exercise_1.sh`
1. write some code in it to display the current working directory as:
> The current directory is : /home/me/bash_exercises
4. make the file executable
5. run it !
---
# Variables and data types in Bash
......@@ -190,22 +207,29 @@ Variables let you store data : **numeric values** or **character(s)**
You can use variables to read, access, and manipulate data throughout your script
**There are no data types in Bash**
Set the variable values in the following ways :
**You don't specify data types in Bash**
- assign directly : `greeting="Welcome"` or `a=4`
- access the variable value using `$`: `echo $greeting`
- assign based on variable: `b=$a`
---
And then access using `$`: `echo $greeting`
**!!!** no space before or after `=` in the assignation **!!!**
> `myvar=Hello World` :boom: `-bash: World: command not found`
`myvar = "Hello World"` :boom:
---
### Quotes for character(s) `" '`
Double will do variable substitution, single will not
Double will do **variable substitution**, single will not:
```
$ echo "my home is $HOME"
my home is /home/me
$ echo 'my home is $HOME'
my home is $HOME
```
### Command Substitution
......@@ -231,7 +255,7 @@ myvar=$( ls )
* Avoid using **reserved keywords**, such as `if`, `then`, `else`, `fi`, and so on...
* **Never** name your private variables using only **UPPERCASE** characters
* **Never** name your private variables using only **UPPERCASE** characters to avod conflicts with builtins
---
......@@ -242,13 +266,16 @@ myvar=$( ls )
| `+` `-` `\*` `/` | addition, subtraction, multiply, divide |
| `var++` | increase the variable var by 1 |
| `var--` | decrease the variable var by 1 |
| `%` | modulus (Return the remainder after division) |
| `%` | modulus (remainder after division) |
Several ways to go about arithmetic in Bash scripting :
`let`, `expr` or using **double parentheses**
<!-- JDF: probablement pas la bonne place pour ça
Return the length of a variable : `${#var}`
-->
<!-- JDF: a mon avis pas utile
---
- `let` make a variable equal to an expression
......@@ -273,7 +300,7 @@ a=$( expr 10 - 3 )
- double parentheses : return the result of the expression
> it is the **preferred method**
-->
---
```bash
......@@ -298,6 +325,23 @@ echo $b # 16
# Conditional statements
Use:
* `if condition; then` to start conditional block
* `else` to start alternative block
* `elif` to start alternative conition block
* `fi` to close conditional block
The following operaors can be used beween conditions:
* `||` means **OR**
* `&&` mean **AND**
---
# Conditional exemple
(Dont pay too much attention to the details yet)
```bash
#!/bin/bash
echo "Please enter a number: "
......@@ -313,14 +357,28 @@ else
fi
```
> read the **standard input stream** with the `read` command
Read the **standard input stream** with the `read` command
---
### `test` command
| Operator | Description |
| ------------ | --------------- |
| `! EXPRESSION` | The EXPRESSION is false|
| `-n STRING` | The length of STRING is greater than zero |
| `-z STRING` | The lengh of STRING is zero (ie it is empty) |
| `STR1 = STR2` | STRING1 is equal to STRING2 |
| `STR1 != STR2` | STRING1 is not equal to STRING2 |
| `INT1 -eq INT2` | INTEGER1 is numerically equal to INTEGER2 (or `==`) |
| `INT1 -gt INT2` | INTEGER1 is numerically greater than INTEGER2 |
| `INT1 -lt INT2` | INTEGER1 is numerically less than INTEGER2 |
| `INT1 -ne INT2` | INTEGER1 is numerically not equal to INTEGER2 |
---
### Build conditions with the `test` command
```bash
test -s /etc/hosts && echo "Exists and not empty"
test -s /etc/hosts
```
| Operator | Description |
......@@ -334,20 +392,6 @@ test -s /etc/hosts && echo "Exists and not empty"
---
| Operator | Description |
| ------------ | --------------- |
| `! EXPRESSION` | The EXPRESSION is false|
| `-n STRING` | The length of STRING is greater than zero |
| `-z STRING` | The lengh of STRING is zero (ie it is empty) |
| `STR1 = STR2` | STRING1 is equal to STRING2 |
| `STR1 != STR2` | STRING1 is not equal to STRING2 |
| `INT1 -eq INT2` | INTEGER1 is numerically equal to INTEGER2 (or `==`) |
| `INT1 -gt INT2` | INTEGER1 is numerically greater than INTEGER2 |
| `INT1 -lt INT2` | INTEGER1 is numerically less than INTEGER2 |
| `INT1 -ne INT2` | INTEGER1 is numerically not equal to INTEGER2 |
---
### Conditional: light variation
Check an expression in the `if` statement ?
......@@ -394,6 +438,13 @@ esac
# Hands-on exercise
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`
3. Do the following only if the number are smaller than 100:
4. Check if at least one of the numbers is a multiple of 3, and tell the user
5. Otherwise, check if both numbers are even, and tell the user
6. If the user's numbers ar too big, tell them
---
# Arrays
......@@ -401,7 +452,6 @@ esac
### Indexed arrays
```bash
#!/bin/bash
# Declare an array with 4 elements
my_array=( 'Debian Linux' 'Redhat Linux' Ubuntu OpenSUSE )
# get number of elements in the array
......@@ -423,16 +473,16 @@ echo ${my_array[@]}
By default, a bash array is an indexed array : need to use the `declare` command
```bash
#!/bin/bash
declare -A acronyms
declare -A acronyms # -A for associative array
acronyms[ACK]=Acknowledgement
acronyms[EOF]="End of Frame"
echo ${acronyms[ACK]}
if [ ${acronyms[EOF]+_} ]; then echo "Found"; else echo "Not found"; fi
```
The variable expansion `${MYVAR+ABC}` expands to "ABC" is `MYVAR` is set and to nothing otherwise.
```bash
#!/bin/bash
declare -A countries=( [ALB]=Albania [BHR]=Bahrain [CMR]=Cameroon [DNK]=Denmark [EGY]=Egypt )
echo ${countries[@]}
echo ${!countries[@]}
......@@ -484,7 +534,6 @@ Basic loop structures in Bash scripting :
### Examples
```bash
#!/bin/bash
# Basic while loop
counter=0
while [ $counter -lt 3 ]; do
......@@ -493,7 +542,6 @@ while [ $counter -lt 3 ]; do
done
```
```bash
#!/bin/bash
# Basic until loop
counter=1
until [ $counter -gt 10 ]; do
......@@ -530,31 +578,6 @@ for i in $( cat file.txt )
---
### How to Read a File Line By Line
```bash
#!/bin/bash
# How to Read a File Line By Line
input="/path/to/txt/file"
while IFS= read -r line
do
echo "$line"
done < "$input"
```
> by default `read` removes all leading and trailing whitespace characters such as spaces and tabs
<!--
`while IFS= read -r line`
The internal field separator (`IFS`) is set to the empty string to preserve whitespace issues
The `-r` option is used not to allow backslashes to escape any characters
-->
---
```bash
#!/bin/bash
# How to iterate over keys or values of an Array
......@@ -575,6 +598,15 @@ done
# Hands-on exercise
Speed game:
1. Use the following webiste to get a list of random words: https://randomwordgenerator.com and put them 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
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.
---
# Arguments - Positional Parameters
......@@ -660,6 +692,31 @@ cat $1 2>&1
---
### How to Read a File Line By Line : input redirection
```bash
#!/bin/bash
# How to Read a File Line By Line
input="/path/to/txt/file"
while IFS= read -r line
do
echo "$line"
done < "$input"
```
> by default `read` removes all leading and trailing whitespace characters such as spaces and tabs
<!--
`while IFS= read -r line`
The internal field separator (`IFS`) is set to the empty string to preserve whitespace issues
The `-r` option is used not to allow backslashes to escape any characters
-->
---
# Return codes
Linux command returns a status when it terminates normally or abnormally
......
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Terminez d'abord l'édition de ce message.
Veuillez vous inscrire ou vous pour commenter