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

Last changes before session

parent 2ce93a8c
Aucune branche associée trouvée
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
......@@ -244,6 +244,34 @@ myvar=$( ls )
---
## String manipulation
Consider `string=abcABC123ABCabc` and `filename=myfile.txt`
* string length : `${#string}` is `15`
* substring extraction :
* `${string:7}` is `23ABCabc`
* `${string:7:3}` is `23A`
* `${string:(-4)}` or `${string: -4}` is `Cabc`
* substring removal from front :
* `${string#a*C}` is `123ABCabc`
* `${filename##myfile}` is `.txt`
---
* substring removal from back :
* `${string%b*c}` is `abcABC123ABCa`
* `${filename%%.txt}` is `myfile`
## Variable expansion
* `${variable-default}` :
if `variable` is unset or null, the expansion of `default` is substituted
* `${variable+default}` :
if `variable` is unset or null, nothing is substituted, otherwise the expansion of `default` is substituted
---
# Arithmetic
| Operator | Operation |
......@@ -326,7 +354,7 @@ The following operaors can be used beween conditions:
# Conditional exemple
(Dont pay too much attention to the details yet)
(Don't pay too much attention to the details yet)
```bash
#!/bin/bash
......@@ -449,6 +477,7 @@ my_array[0]=56.45
my_array[1]=568
echo Number of elements: ${#my_array[@]}
# echo array's content
echo ${my_array[2]}
echo ${my_array[@]}
```
......@@ -466,7 +495,7 @@ echo ${acronyms[ACK]}
if [ ${acronyms[EOF]+_} ]; then echo "Found"; else echo "Not found"; fi
```
The variable expansion `${MYVAR+ABC}` expands to "ABC" is `MYVAR` is set and to nothing otherwise.
> the variable expansion `${MYVAR+ABC}` expands to `ABC` is `MYVAR` is set and to nothing otherwise
```bash
declare -A countries=( [ALB]=Albania [BHR]=Bahrain [CMR]=Cameroon [DNK]=Denmark [EGY]=Egypt )
......@@ -919,11 +948,11 @@ bash test.sh
# Subshells
* A subshell is a "child shell" spawned by the main shell ("parent shell")
* 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
* A subshell is a **separate** instance of the command process, run as a new process
* **Unlike calling a shell script** (slide before), subshells inherit the **same** variables as the original process
* 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
* Subshells can be used for parallel processing
* Subshells can be used for **parallel processing**
---
......@@ -981,7 +1010,7 @@ greeting $COUNTRY
COUNTRY="France"
./myScript.sh # or bash or exec
echo $COUNTRY
greeting $COUNTRY
greeting $COUNTRY # !!
```
```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