Skip to content
Extraits de code Groupes Projets
Valider 2290002c rédigé par Pierre-Yves Barriat's avatar Pierre-Yves Barriat
Parcourir les fichiers

Merge branch 'master' into 'master'

Exercice 5 + quelques changements de formulation

See merge request !4
parents f636851a fdc3835a
Aucune branche associée trouvée
Aucune étiquette associée trouvée
1 requête de fusion!4Exercice 5 + quelques changements de formulation
...@@ -34,7 +34,7 @@ It's called the **C**ommand **L**ine **U**ser **I**nterface ...@@ -34,7 +34,7 @@ It's called the **C**ommand **L**ine **U**ser **I**nterface
**CLUI** is one of the many strengths of Linux : **CLUI** is one of the many strengths of Linux :
- allows to be independent of distros (or UNIX systems like OSX) - allows to be independent of distros (or UNIX systems like OSX)
- allows to easily work at distance (SSH) - allows to easily work remotely (SSH)
- allows to join together simple (and less simple) commands to do complex things and automate **= scripting** - allows to join together simple (and less simple) commands to do complex things and automate **= scripting**
In Linux, process automation relies heavily on scripting. This involves creating a file containing a series of commands that can be executed together In Linux, process automation relies heavily on scripting. This involves creating a file containing a series of commands that can be executed together
...@@ -43,7 +43,7 @@ In Linux, process automation relies heavily on scripting. This involves creating ...@@ -43,7 +43,7 @@ In Linux, process automation relies heavily on scripting. This involves creating
# Linux Shell # Linux Shell
A **shell** is a program that takes commands from the keyboard and gives them to the operating system to perform A **shell** is a program that takes commands from the keyboard and transmits them to the operating system to perform
The main function is to interpret your commands **= language** The main function is to interpret your commands **= language**
...@@ -92,8 +92,6 @@ In a Bash shell many things constitute your environment ...@@ -92,8 +92,6 @@ In a Bash shell many things constitute your environment
Environment includes many variables that may have been set **by bash** or **by you** 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 `$`**
--- ---
# Environment variables # Environment variables
...@@ -106,6 +104,8 @@ Environment includes many variables that may have been set **by bash** or **by y ...@@ -106,6 +104,8 @@ Environment includes many variables that may have been set **by bash** or **by y
| `SHELL` | the name of the shell | | `SHELL` | the name of the shell |
| `UID` | the numeric user id of the logged-in user | | `UID` | the numeric user id of the logged-in user |
**Access the value of a variable by prefixing its name with `$`**
So to get the value of `USER` you would use `$USER` in bash code 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` > You can use special files to control bash variables : `$HOME/.bashrc`
...@@ -142,7 +142,7 @@ echo "A comment will follow." # Comment here. ...@@ -142,7 +142,7 @@ echo "A comment will follow." # Comment here.
- Use something your editor makes easy (**Vim** uses `Tab`) - Use something your editor makes easy (**Vim** uses `Tab`)
--- ---
<!-- JDF: je pnseque c'est trop tôt pour ceci <!-- JDF: je pense que c'est trop tôt pour ceci
### Command separators ### Command separators
Commands can be combined using **meta-characters** and **control operators** Commands can be combined using **meta-characters** and **control operators**
...@@ -335,7 +335,7 @@ echo $b # 16 ...@@ -335,7 +335,7 @@ echo $b # 16
Use: Use:
* `if condition; then` to start conditional block * `if condition; then` to start conditional block
* `else` to start alternative block * `else` to start alternative block
* `elif` to start alternative conition block * `elif` to start alternative condition block
* `fi` to close conditional block * `fi` to close conditional block
The following operaors can be used beween conditions: The following operaors can be used beween conditions:
...@@ -442,7 +442,7 @@ esac ...@@ -442,7 +442,7 @@ esac
# Hands-on exercise # Hands-on exercise
1. In your `bash_exercises` folder create a new bash file called `exercise_2.sh` and make it executable 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` 2. Ask the user for two numbers smaller than 100 and put them in variables `NUMBER1` and `NUMBER2`
> ```bash > ```bash
> #!/bin/bash > #!/bin/bash
...@@ -452,7 +452,7 @@ esac ...@@ -452,7 +452,7 @@ esac
3. Check if the numbers are smaller than 100 3. Check if the numbers are smaller than 100
- If yes, check if both numbers are even and tell the user - If yes, check if both numbers are even and tell the user
- If not, tell the user with `echo 'my message'` - If not, tell the user (use `echo`)
--- ---
...@@ -690,8 +690,6 @@ Shells use 3 standard I/O streams ...@@ -690,8 +690,6 @@ Shells use 3 standard I/O streams
Shell has several **meta-characters** and **control operators** Shell has several **meta-characters** and **control operators**
> `|`, `&`, `>`, `;`, `<`, etc.
--- ---
# Control operators # Control operators
...@@ -814,7 +812,7 @@ _files="$@" ...@@ -814,7 +812,7 @@ _files="$@"
* "small script within a script" that you may call multiple times * "small script within a script" that you may call multiple times
* great way to reuse code * great way to reuse code
* a function is most reuseable when it performs a single task * a function is most reuseable when it performs a single task
* good to put ancillary tasks within functions : logically separate from main * good to put ancillary tasks within functions : logically separate from main code
```bash ```bash
#!/bin/bash #!/bin/bash
...@@ -883,6 +881,9 @@ my_function ...@@ -883,6 +881,9 @@ my_function
echo $func_result echo $func_result
``` ```
<!-- Note JDF: est-ce que la partie ci-dessus est utile ? Autant montrer directement la bonne manière pour moi
-->
* Better way is to send the value to `stdout` using `echo` * Better way is to send the value to `stdout` using `echo`
```bash ```bash
...@@ -908,18 +909,18 @@ print_something () { ...@@ -908,18 +909,18 @@ print_something () {
print_something Mars print_something Mars
``` ```
> Be careful in case of "overriding commands": plaese never use an existing linux command name. > Athough it is possible, you should try to avoid having functions using the name of existing linux commands.
--- ---
# Hands-on exercise # Hands-on exercise
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. 1. Write a script called `exercise_5.sh` expecting **2 arguments**. If not exactly two arguments are provided:
2. Write a function taking a **folder path** and an **extension** as arguments and giving the list of matching files to the user * Echo an error message
<!-- 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**. * Exit with a non-zero error code
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--> 2. Write a function taking a **folder path** (e.g `/home/ucl/elic/xxxx`) and an **extension** (e.g `py`) as arguments
3. Use the `ls` command to list the files in the given path having with the given extension. Write this list to a file called `files_found.txt`.
<!-- Créer un petit exercice raisonnable--> 4. Bonus : if there are no files, Exit with a non-zero error code
--- ---
...@@ -929,8 +930,8 @@ Consider the script `test.sh` below : ...@@ -929,8 +930,8 @@ Consider the script `test.sh` below :
```bash ```bash
#!/bin/bash #!/bin/bash
echo $var1 echo "var1 = ${var1}"
echo $var2 echo "var2 = ${var2}"
``` ```
Then run this script : Then run this script :
...@@ -940,6 +941,7 @@ var1=23 ...@@ -940,6 +941,7 @@ var1=23
export var2=12 export var2=12
bash test.sh bash test.sh
``` ```
> By default, variables from the main interpreter are not available in scripts, unless you `export` them.
--- ---
...@@ -956,7 +958,7 @@ bash test.sh ...@@ -956,7 +958,7 @@ bash test.sh
### Syntax ### Syntax
A command list embedded between parentheses runs as a subshell : A command list embedded **between parentheses** runs as a subshell :
```bash ```bash
#!/bin/bash #!/bin/bash
...@@ -976,8 +978,8 @@ bash -c "command1; command2; command3" ...@@ -976,8 +978,8 @@ bash -c "command1; command2; command3"
## Differences between **Sourcing** and **Executing** a script ## Differences between **Sourcing** and **Executing** a script
- source a script = execution in the current shell - source a script = execution **in the current shell**
> variables and functions are valid in the current shell after sourcing > variables and functions are valid in the current shell after sourcing even if not `export`ed
- execute a script = execution in a new shell (in a subshell of the current shell) - 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 > all new variables and functions created by the script will only live in the subshell
...@@ -1041,7 +1043,7 @@ fi ...@@ -1041,7 +1043,7 @@ fi
### use `echo` ### use `echo`
"classical" but useful technique : insert `echo` throughout your code Classical but useful technique : insert `echo` throughout your code to check variable content
```bash ```bash
#!/bin/bash #!/bin/bash
......
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