<!-- 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
> 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