Skip to content
Extraits de code Groupes Projets

Comparer les révisions

Les modifications sont affichées comme si la révision source était fusionnée avec la révision cible. En savoir plus sur la comparaison des révisions.

Source

Sélectionner le projet cible
No results found

Cible

Sélectionner le projet cible
  • dfrancois/learning-bash
  • barriat/learning-bash
2 résultats
Afficher les modifications
Validations sur la source (3)
......@@ -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 :
- 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**
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
# 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**
......@@ -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**
**Access the value of a variable by prefixing its name with `$`**
---
# Environment variables
......@@ -106,6 +104,8 @@ 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 |
**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
> You can use special files to control bash variables : `$HOME/.bashrc`
......@@ -142,7 +142,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
<!-- JDF: je pense que c'est trop tôt pour ceci
### Command separators
Commands can be combined using **meta-characters** and **control operators**
......@@ -335,7 +335,7 @@ echo $b # 16
Use:
* `if condition; then` to start conditional block
* `else` to start alternative block
* `elif` to start alternative conition block
* `elif` to start alternative condition block
* `fi` to close conditional block
The following operaors can be used beween conditions:
......@@ -442,7 +442,7 @@ 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`
2. Ask the user for two numbers smaller than 100 and put them in variables `NUMBER1` and `NUMBER2`
> ```bash
> #!/bin/bash
......@@ -452,7 +452,7 @@ esac
3. Check if the numbers are smaller than 100
- 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
Shell has several **meta-characters** and **control operators**
> `|`, `&`, `>`, `;`, `<`, etc.
---
# Control operators
......@@ -814,7 +812,7 @@ _files="$@"
* "small script within a script" that you may call multiple times
* great way to reuse code
* 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
#!/bin/bash
......@@ -883,6 +881,9 @@ my_function
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`
```bash
......@@ -908,18 +909,18 @@ print_something () {
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
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.
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-->
<!-- Créer un petit exercice raisonnable-->
1. Write a script called `exercise_5.sh` expecting **2 arguments**. If not exactly two arguments are provided:
* Echo an error message
* Exit with a non-zero error code
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`.
4. Bonus : if there are no files, Exit with a non-zero error code
---
......@@ -929,8 +930,8 @@ Consider the script `test.sh` below :
```bash
#!/bin/bash
echo $var1
echo $var2
echo "var1 = ${var1}"
echo "var2 = ${var2}"
```
Then run this script :
......@@ -940,6 +941,7 @@ var1=23
export var2=12
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
### Syntax
A command list embedded between parentheses runs as a subshell :
A command list embedded **between parentheses** runs as a subshell :
```bash
#!/bin/bash
......@@ -976,8 +978,8 @@ bash -c "command1; command2; command3"
## 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
- source a script = execution **in the current shell**
> 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)
> all new variables and functions created by the script will only live in the subshell
......@@ -1041,7 +1043,7 @@ fi
### 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
#!/bin/bash
......