From fdc3835a19abc58f2da2bb8a9a5009194a9c0906 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20de=20Favereau=20de=20Jeneret?= <jerome.defavereau@uclouvain.be> Date: Wed, 25 Sep 2024 12:50:02 +0000 Subject: [PATCH] petits changements de formulations principalement --- Bash.md | 38 ++++++++++++++++++++------------------ 1 file changed, 20 insertions(+), 18 deletions(-) diff --git a/Bash.md b/Bash.md index fd11aa4..597256b 100644 --- a/Bash.md +++ b/Bash.md @@ -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,7 +909,7 @@ 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. --- @@ -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 -- GitLab