/dev/null

discard to the void

Outils pour utilisateurs

Outils du site


tech:bash_scripts

Différences

Ci-dessous, les différences entre deux révisions de la page.

Lien vers cette vue comparative

Prochaine révision
Révision précédente
tech:bash_scripts [2024/10/25] – créée Mathieutech:bash_scripts [2025/04/12] (Version actuelle) – supprimée Mathieu
Ligne 1: Ligne 1:
-====== Bash Scripting ====== 
  
-===== Table of Contents ===== 
-  * [[#basics|Basics]] 
-    * [[#exit_codes|Exit codes]] 
-    * [[#variables_and_data_types|Variables and Data Types]] 
-    * [[#command_substitution|Command substitution]] 
-    * [[#input_output_redirection|Input/output redirection]] 
-  * [[#control_structures|Control Structures]] 
-    * [[#conditionals|Conditionals]] 
-      * [[#if_statements|If statements]] 
-      * [[#case_statements|Case statements]] 
-    * [[#loops|Loops]] 
-      * [[#for_loops|For loops]] 
-      * [[#while_loops|While loops]] 
-    * [[#functions|Functions]] 
-  * [[#comparisons_and_tests|Comparisons and Tests]] 
-    * [[#string_comparison|String comparison]] 
-    * [[#numerical_comparison|Numerical comparison]] 
-    * [[#variable_existence|Variable existence]] 
-    * [[#file_checks|File checks]] 
-    * [[#permissions_checks|Permissions checks]] 
-  * [[#logical_operations_and_combinations|Logical Operations and Combinations]] 
-    * [[#internal_combinations|Internal combinations]] 
-    * [[#external_combinations|External combinations]] 
-    * [[#conditional_execution|Conditional execution]] 
-  * [[#useful_commands_and_concepts|Useful Commands and Concepts]] 
-    * [[#sleep|sleep]] 
-    * [[#read|read]] 
-    * [[#set_options|set options]] 
-    * [[#mktemp|mktemp]] 
-    * [[#trap|trap]] 
-    * [[#arrays|Arrays]] 
-  * [[#best_practices|Best Practices]] 
- 
-===== 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="John" 
-age=30 
-</code> 
- 
-==== 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) 
-</code> 
- 
-==== Input/output redirection ==== 
-Allows you to control where input comes from and where output goes. 
-Example: 
-<code bash> 
-echo "Hello" > output.txt  # Redirect output to a file 
-cat < input.txt            # Read input from a file 
-</code> 
- 
-===== Control Structures ===== 
- 
-==== Conditionals ==== 
- 
-=== If statements === 
-Used for conditional execution of code. 
-Example: 
-<code bash> 
-if [[ condition ]]; then 
-  echo "condition met" 
-elif [[ condition_2 ]]; then 
-  echo "condition_2 met" 
-else 
-  echo "no condition met" 
-fi 
-</code> 
- 
-=== Case statements === 
-Used for multiple conditional branches. 
-Example: 
-<code bash> 
-case "$variable" in 
-  pattern1) command1 ;; 
-  pattern2) command2 ;; 
-  *) default_command ;; 
-esac 
-</code> 
- 
-==== 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 "$i" 
-done 
- 
-# Range 
-for i in {1..10}; do 
-  echo "$i" 
-done 
- 
-# Pattern matching 
-for item in ./content/*.md; do 
-  echo "$item" 
-done 
- 
-# Command result 
-for item in $(ls ~/Notes/); do 
-  echo $item 
-done 
-</code> 
- 
-=== 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 
-</code> 
- 
-==== Functions ==== 
-Reusable blocks of code. They operate like mini-scripts. 
-Example: 
-<code bash> 
-greet() { 
-  echo "Hello, $1!" 
-} 
-greet "World" 
-</code> 
- 
-===== Comparisons and Tests ===== 
- 
-==== String comparison ==== 
-<code bash> 
-val="a" 
-[[ "$val" == "a" ]] 
-[[ "$val" != "b" ]] 
-</code> 
- 
-==== Numerical comparison ==== 
-<code bash> 
-num=1 
-[[ "$num" -eq 1 ]]  # equal 
-[[ "$num" -ne 2 ]]  # not equal 
-[[ "$num" -lt 2 ]]  # less than 
-[[ "$num" -le 2 ]]  # less than or equal 
-[[ "$num" -gt 1 ]]  # greater than 
-[[ "$num" -ge 1 ]]  # greater than or equal 
-</code> 
- 
-==== Variable existence ==== 
-<code bash> 
-val="" 
-[[ -z $val ]]  # var is null 
-[[ -n $val ]]  # var is not null 
-</code> 
- 
-==== File checks ==== 
-<code bash> 
-file="./hello" 
-[[ -f $file ]]  # file exists 
-[[ -d $file ]]  # directory exists 
-[[ -e $file ]]  # file or directory exists 
-</code> 
- 
-==== Permissions checks ==== 
-<code bash> 
-file="./hello" 
-[[ -r $file ]]  # readable 
-[[ -w $file ]]  # writable 
-[[ -x $file ]]  # executable 
-</code> 
- 
-===== 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 
-</code> 
- 
-==== External combinations ==== 
-<code bash> 
-[[ $val -gt 5 ]] && [[ $val -lt 10 ]]  # AND 
-[[ $val -gt 5 ]] || [[ $val -lt 3 ]]   # OR 
-</code> 
- 
-==== Conditional execution ==== 
-<code bash> 
-command1 && command2  # Execute command2 if command1 was successful 
-command1 || command2  # Execute command2 if command1 failed 
-</code> 
- 
-===== 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 != "Y" ]]; then 
-  exit 1 
-fi 
-echo "Continuing..." 
-</code> 
- 
-==== set options ==== 
-Activates strict mode in bash: 
-<code bash> 
-set -euo pipefail 
-</code> 
-  * 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=("apple" "banana" "cherry") 
-echo ${fruits[0]}  # Outputs: apple 
-</code> 
- 
-===== 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.1729866053.txt.gz · Dernière modification : 2024/10/25 de Mathieu

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki