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

second part of updates

parent 15629a0c
Aucune branche associée trouvée
Aucune étiquette associée trouvée
1 requête de fusion!1Add exercises, some edits
...@@ -80,22 +80,6 @@ echo $STRING ...@@ -80,22 +80,6 @@ echo $STRING
--- ---
<!-- 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
...@@ -758,6 +770,7 @@ command && echo "success" || echo "failed" ...@@ -758,6 +770,7 @@ command && echo "success" || echo "failed"
_files="$@" _files="$@"
[[ "$_files" == "" ]] && { echo "Usage: $0 file1.png file2.png"; exit 1; } [[ "$_files" == "" ]] && { echo "Usage: $0 file1.png file2.png"; exit 1; }
``` ```
<!-- Attention utilisatin du [[ ]] pas encore expliqué ? -->
--- ---
...@@ -785,7 +798,6 @@ Functions must be declared **before** they are used ...@@ -785,7 +798,6 @@ Functions must be declared **before** they are used
## Variables Scope ## Variables Scope
```bash ```bash
#!/bin/bash
# Define bash global variable # Define bash global variable
# This variable is global and can be used anywhere in this bash script # This variable is global and can be used anywhere in this bash script
var="global variable" var="global variable"
...@@ -811,13 +823,11 @@ echo $var ...@@ -811,13 +823,11 @@ echo $var
Bash functions don’t allow you to return a value when called Bash functions don’t allow you to return a value when called
After completion, the return value is the status of the last statement (so 0-255) After completion, the return value is the **status** of the last statement (so 0-255)
Can be specified by using `return` : It can also be specified manually by using `return` :
```bash ```bash
#!/bin/bash
my_function () { my_function () {
echo "some result" echo "some result"
return 55 return 55
...@@ -828,10 +838,10 @@ echo $? ...@@ -828,10 +838,10 @@ echo $?
--- ---
Return an arbitrary value from a function : assign the result of the function Return an arbitrary value (different from a return code) from a function :
* Assign the result of the function
```bash ```bash
#!/bin/bash
my_function () { my_function () {
func_result="some result" func_result="some result"
} }
...@@ -839,10 +849,9 @@ my_function ...@@ -839,10 +849,9 @@ my_function
echo $func_result echo $func_result
``` ```
Better way is to send the value to `stdout` using `echo` * Better way is to send the value to `stdout` using `echo`
```bash ```bash
#!/bin/bash
my_function () { my_function () {
local func_result="some result" local func_result="some result"
echo "$func_result" echo "$func_result"
...@@ -870,7 +879,7 @@ print_something Mars ...@@ -870,7 +879,7 @@ print_something Mars
```bash ```bash
#!/bin/bash #!/bin/bash
ls () { ls () {
command ls -lh command ls -lh # 'command' keyword prevents ambiguity
} }
ls ls
``` ```
...@@ -879,6 +888,11 @@ ls ...@@ -879,6 +888,11 @@ ls
# Hands-on exercise # 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.
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
......
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