<!-- JDF: je pense qu c'est trop tôt pur cette info
# Shell syntax rules
Shells use 3 **"standard I/O streams"**
-`stdin` is the standard input stream, which provides input to commands
-`stdout` is the standard output stream, which displays output from commands
-`stderr` is the standard error stream, which displays error output from commands
Shell has several **meta-characters** and **control operators**
> `|`, `&`, `>`, `;` , etc.
---
-->
# Bash environment
# Bash environment
In a Bash shell many things constitute your environment
In a Bash shell many things constitute your environment
...
@@ -600,7 +584,7 @@ done
...
@@ -600,7 +584,7 @@ done
Speed game:
Speed game:
1. Use the following webiste to get a list of random words: https://randomwordgenerator.com and put them in a variable
1. Use the following webiste 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`
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. Put the answers in an associative array using the words as keys and the answers as values
4. Register the end time in `TEND`
4. Register the end time in `TEND`
...
@@ -670,6 +654,34 @@ bash test_arg.sh -f 'John Smith' -a 25 -u john
...
@@ -670,6 +654,34 @@ bash test_arg.sh -f 'John Smith' -a 25 -u john
---
---
# Input/Output streams
Shells use 3 standard I/O streams
-`stdin` is the standard input stream, which provides input to commands
-`stdout` is the standard output stream, which displays output from commands
-`stderr` is the standard error stream, which displays error output from commands
Shell has several **meta-characters** and **control operators**
> `|`, `&`, `>`, `;`, `<`, etc.
---
# Control operators
| Character | Effect |
| ---- | --- |
| `;` | Normal separator between commands |
| `&&` | Execute next command only if command succeeds |
| `\|\|` | Execute next command only if command fails |
| `&` | Don't wait for result of command before starting next command |
| `\|` | Use output of command as input for the next command |
| `> file_desc` | Send stdandard output of command to file descriptor |
| `< file_desc` | Use content of file descriptor as input |
---
# Redirections
# Redirections
Use the meta-character `>` in order to control the output streams `stdout` and `stderr` for a command or a bash script
Use the meta-character `>` in order to control the output streams `stdout` and `stderr` for a command or a bash script
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.
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.
---
---
# Shell vs Environment Variables
# Shell vs Environment Variables
...
@@ -903,12 +917,12 @@ bash test.sh
...
@@ -903,12 +917,12 @@ bash test.sh
# Subshells
# Subshells
* subshell is a "child shell" spawned by the main shell ("parent shell")
*A subshell is a "child shell" spawned by the main shell ("parent shell")
* subshell is separate instance of the command process, run as a new process
*A subshell is a separate instance of the command process, run as a new process
*unlike calling a shell script, subshells inherit the same variables as the original process
*Unlike calling a shell script, subshells inherit the same variables as the original process
*a subshell allow you to execute commands within a separate shell environment = *Subshell Sandboxing*
*A subshell allows you to execute commands within a separate shell environment = *Subshell Sandboxing*
> useful to set temporary variables or change directories without affecting the parent shell's environment
> useful to set temporary variables or change directories without affecting the parent shell's environment
*subshells can be used for parallel processing
*Subshells can be used for parallel processing
---
---
...
@@ -928,11 +942,30 @@ Or :
...
@@ -928,11 +942,30 @@ Or :
bash -c"command1; command2; command3"
bash -c"command1; command2; command3"
```
```
> Reminder : variables in a subshell are not visible outside the block of code in the subshell
> Reminder : variables in a subshell are **not** visible outside the block of code in the subshell
---
## Differences between **Sourcing** and **Executing** a script
- source a script = execution in the current shell
> variables and functions are valid in the current shell after sourcing
- execute a script = execution in a new shell (in a subshell of the current shell)
> all new variables and functions created by the script will only live in the subshell
Source a script using `source` or `.`
```bash
source myScript.sh
. myScript.sh
```
> official one is `.` Bash defined `source` as an alias to the `.`
---
---
### Sourcing Bash scripts
## Example
```bash
```bash
#!/bin/bash
#!/bin/bash
...
@@ -951,6 +984,7 @@ greeting $COUNTRY
...
@@ -951,6 +984,7 @@ greeting $COUNTRY
```
```
```bash
```bash
COUNTRY="France"
source myScript.sh
source myScript.sh
echo$COUNTRY
echo$COUNTRY
greeting $COUNTRY
greeting $COUNTRY
...
@@ -958,25 +992,6 @@ greeting $COUNTRY
...
@@ -958,25 +992,6 @@ greeting $COUNTRY
---
---
Differences between **Sourcing** and **Executing** a script
- source a script = execution in the current shell
> variables and functions are valid in the current shell after sourcing
- execute a script = execution in a new shell (in a subshell of the current shell)
> all new variables and functions created by the script will only live in the subshell
Source a script using `source` or `.`
```bash
source myScript.sh
. myScript.sh
```
> official one is `.` Bash defined `source` as an alias to the `.`
---
### Running parallel processes in subshells
### Running parallel processes in subshells
Processes may execute in parallel within different subshells
Processes may execute in parallel within different subshells
...
@@ -990,7 +1005,7 @@ job() {
...
@@ -990,7 +1005,7 @@ job() {
i=0
i=0
while[$i-lt 10 ];do
while[$i-lt 10 ];do
echo"${i}: job $job_id"
echo"${i}: job $job_id"
i=$[$i+1]
(( i++ ))
sleep 0.2
sleep 0.2
done
done
}
}
...
@@ -1026,10 +1041,6 @@ time ./manager_par.sh
...
@@ -1026,10 +1041,6 @@ time ./manager_par.sh
---
---
# Hands-on exercise
---
# Debug
# Debug
Tips and techniques for debugging and troubleshooting Bash scripts
Tips and techniques for debugging and troubleshooting Bash scripts