tech:bash_scripts
Différences
Ci-dessous, les différences entre deux révisions de la page.
| Les deux révisions précédentesRévision précédente | |||
| tech:bash_scripts [2024/10/25] – [Table of Contents] Mathieu | tech:bash_scripts [2025/04/12] (Version actuelle) – supprimée Mathieu | ||
|---|---|---|---|
| Ligne 1: | Ligne 1: | ||
| - | ====== Bash Scripting ====== | ||
| - | |||
| - | ===== Basics ===== | ||
| - | |||
| - | ==== Exit codes ==== | ||
| - | In bash, exit codes indicate the success or failure of a command or script. | ||
| - | * 0 -> success | ||
| - | * anything else -> failure | ||
| - | |||
| - | ==== Variables and Data Types ==== | ||
| - | Bash primarily deals with strings, but can handle numbers and arrays. | ||
| - | Example: | ||
| - | <code bash> | ||
| - | name=" | ||
| - | age=30 | ||
| - | </ | ||
| - | |||
| - | ==== Command substitution ==== | ||
| - | Allows you to use the output of a command as part of another command. | ||
| - | Example: | ||
| - | <code bash> | ||
| - | current_date=$(date +%Y-%m-%d) | ||
| - | </ | ||
| - | |||
| - | ==== Input/ | ||
| - | Allows you to control where input comes from and where output goes. | ||
| - | Example: | ||
| - | <code bash> | ||
| - | echo " | ||
| - | cat < input.txt | ||
| - | </ | ||
| - | |||
| - | ===== Control Structures ===== | ||
| - | |||
| - | ==== Conditionals ==== | ||
| - | |||
| - | === If statements === | ||
| - | Used for conditional execution of code. | ||
| - | Example: | ||
| - | <code bash> | ||
| - | if [[ condition ]]; then | ||
| - | echo " | ||
| - | elif [[ condition_2 ]]; then | ||
| - | echo " | ||
| - | else | ||
| - | echo "no condition met" | ||
| - | fi | ||
| - | </ | ||
| - | |||
| - | === Case statements === | ||
| - | Used for multiple conditional branches. | ||
| - | Example: | ||
| - | <code bash> | ||
| - | case " | ||
| - | pattern1) command1 ;; | ||
| - | pattern2) command2 ;; | ||
| - | *) default_command ;; | ||
| - | esac | ||
| - | </ | ||
| - | |||
| - | ==== Loops ==== | ||
| - | |||
| - | === For loops === | ||
| - | Used to iterate over a list of items. | ||
| - | Examples: | ||
| - | <code bash> | ||
| - | # Regular | ||
| - | my_array=(1 2 3 4 5) | ||
| - | for item in ${my_array[@]} | ||
| - | do | ||
| - | echo $item | ||
| - | done | ||
| - | |||
| - | # C-style | ||
| - | for ((i = 0; i < 10; i++)); do | ||
| - | echo " | ||
| - | done | ||
| - | |||
| - | # Range | ||
| - | for i in {1..10}; do | ||
| - | echo " | ||
| - | done | ||
| - | |||
| - | # Pattern matching | ||
| - | for item in ./ | ||
| - | echo " | ||
| - | done | ||
| - | |||
| - | # Command result | ||
| - | for item in $(ls ~/Notes/); do | ||
| - | echo $item | ||
| - | done | ||
| - | </ | ||
| - | |||
| - | === While loops === | ||
| - | Executes a block of code as long as a condition is true. | ||
| - | Example: | ||
| - | <code bash> | ||
| - | counter=0 | ||
| - | while [[ $counter -lt 5 ]]; do | ||
| - | echo $counter | ||
| - | ((counter++)) | ||
| - | done | ||
| - | </ | ||
| - | |||
| - | ==== Functions ==== | ||
| - | Reusable blocks of code. They operate like mini-scripts. | ||
| - | Example: | ||
| - | <code bash> | ||
| - | greet() { | ||
| - | echo " | ||
| - | } | ||
| - | greet " | ||
| - | </ | ||
| - | |||
| - | ===== Comparisons and Tests ===== | ||
| - | |||
| - | ==== String comparison ==== | ||
| - | <code bash> | ||
| - | val=" | ||
| - | [[ " | ||
| - | [[ " | ||
| - | </ | ||
| - | |||
| - | ==== Numerical comparison ==== | ||
| - | <code bash> | ||
| - | num=1 | ||
| - | [[ " | ||
| - | [[ " | ||
| - | [[ " | ||
| - | [[ " | ||
| - | [[ " | ||
| - | [[ " | ||
| - | </ | ||
| - | |||
| - | ==== Variable existence ==== | ||
| - | <code bash> | ||
| - | val="" | ||
| - | [[ -z $val ]] # var is null | ||
| - | [[ -n $val ]] # var is not null | ||
| - | </ | ||
| - | |||
| - | ==== File checks ==== | ||
| - | <code bash> | ||
| - | file=" | ||
| - | [[ -f $file ]] # file exists | ||
| - | [[ -d $file ]] # directory exists | ||
| - | [[ -e $file ]] # file or directory exists | ||
| - | </ | ||
| - | |||
| - | ==== Permissions checks ==== | ||
| - | <code bash> | ||
| - | file=" | ||
| - | [[ -r $file ]] # readable | ||
| - | [[ -w $file ]] # writable | ||
| - | [[ -x $file ]] # executable | ||
| - | </ | ||
| - | |||
| - | ===== Logical Operations and Combinations ===== | ||
| - | |||
| - | ==== Internal combinations ==== | ||
| - | <code bash> | ||
| - | [[ $val -gt 5 -a $val -lt 10 ]] # -a -> AND | ||
| - | [[ $val -gt 5 -o $val -lt 3 ]] # -o -> OR | ||
| - | </ | ||
| - | |||
| - | ==== External combinations ==== | ||
| - | <code bash> | ||
| - | [[ $val -gt 5 ]] && [[ $val -lt 10 ]] # AND | ||
| - | [[ $val -gt 5 ]] || [[ $val -lt 3 ]] # OR | ||
| - | </ | ||
| - | |||
| - | ==== Conditional execution ==== | ||
| - | <code bash> | ||
| - | command1 && command2 | ||
| - | command1 || command2 | ||
| - | </ | ||
| - | |||
| - | ===== Useful Commands and Concepts ===== | ||
| - | |||
| - | ==== sleep ==== | ||
| - | Pauses the script for a specified amount of time. | ||
| - | |||
| - | ==== read ==== | ||
| - | Reads input from the user. | ||
| - | Example: | ||
| - | <code bash> | ||
| - | read -p "Do you want to continue (Y/n) " resp | ||
| - | if [[ $resp != " | ||
| - | exit 1 | ||
| - | fi | ||
| - | echo " | ||
| - | </ | ||
| - | |||
| - | ==== set options ==== | ||
| - | Activates strict mode in bash: | ||
| - | <code bash> | ||
| - | set -euo pipefail | ||
| - | </ | ||
| - | * set -e: exit on error | ||
| - | * set -u: exit on unset var | ||
| - | * set -o pipefail: exit on pipe fail | ||
| - | |||
| - | ==== mktemp ==== | ||
| - | Creates a temporary file or directory. | ||
| - | |||
| - | ==== trap ==== | ||
| - | Sets up a function to be called when the script receives specific signals. | ||
| - | |||
| - | ==== Arrays ==== | ||
| - | Store multiple values in a single variable. | ||
| - | Example: | ||
| - | <code bash> | ||
| - | fruits=(" | ||
| - | echo ${fruits[0]} | ||
| - | </ | ||
| - | |||
| - | ===== Best Practices ===== | ||
| - | * Use meaningful variable names | ||
| - | * Comment your code | ||
| - | * Use functions for repeated code | ||
| - | * Always quote variables when using them | ||
| - | * Use set -euo pipefail for safer scripts | ||
tech/bash_scripts.1729867940.txt.gz · Dernière modification : de Mathieu