Skip to main content

Shell

"Strict mode" bash

set -euo pipefail
IFS=$'\n\t'

See: Unofficial bash strict mode

Modifier les couleurs dans un shell (RedHat)

Editer le fichier /etc/DIR_COLORS et remplacer DIR 00;34 # directory par DIR 00;32 # directory

note

Les répertoires vont apparaitre en vert au lieu de bleu

Clear screen "like" en shell

echo -en "\ec"

Faire un calcul en shell

echo $((0+0))
$((a + 200)) # Add 200 to $a
$(($RANDOM%200)) # Random number 0..199
note

Permet de faire un calcul , notamment dans ce cas précis, ou un résultat égale à 0 fait planter expr

Lire une entrée STDIN

echo -n "Proceed? [y/n]: "
read ans
echo $ans
tip

lecture STDIN si option, sinon, fichier

test() {
if [ "$1" == "-" ]; then
cat <&0
else
cat "$1"
fi
}

Gestion des options dans un menu

while [[ "$1" =~ ^- && ! "$1" == "--" ]]; do case $1 in
-V | --version )
echo $version
exit
;;
-s | --string )
shift; string=$1
;;
-f | --flag )
flag=1
;;
esac; shift; done
if [[ "$1" == '--' ]]; then shift; fi

Redirections

python hello.py > output.txt # stdout to (file)
python hello.py >> output.txt # stdout to (file), append
python hello.py 2> error.log # stderr to (file)
python hello.py 2>&1 # stderr to stdout
python hello.py > output.txt 2>&1 # stderr to stdout (file and no screen output)
python hello.py 2>/dev/null # stderr to (null)
python hello.py &>/dev/null # stdout and stderr to (null)

Variables spécifique

Environnement

ExpressionDescription
$HOMEchemin du répertoire personnel de l'utilisateur
$OLDPWDchemin du répertoire précédent
$PATHliste des chemins de recherche des commandes exécutables
$PPIDPID du processus père du shell
$PS1invite principale du shell
$PS2invite secondaire du shell
$PS3invite de la structure shell "select"
$PS4invite de l'option shell de débogage "xtrace"
$PWDchemin du répertoire courant
$RANDOMnombre entier aléatoire compris entre 0 et 32767
$REPLYvariable par défaut de la commande "read" et de la structure shell "select"
$SECONDSnombre de secondes écoulées depuis le lancement du shell

Status

ExpressionDescription
$?Exit status de la dernière tache
$!PID de la dernière tache en BG
$$PID du shell
$0Nom du script shell

Arguments

ExpressionDescription
$#Nombre d'arguments
$*Tous les arguments
$@Tous les arguments, en commencant par le premier
$1Premier argument
$_Argument de la dernière commande

Variable avec valeur par défaut

ExpressionDescription
${FOO:-val}$FOO, or val if unset (or null)
${FOO:=val}Set $FOO to val if unset (or null)
${FOO:+val}val if $FOO is set (and not null)
${FOO:?message}Show error message and exit if $FOO is unset (or null)

Omitting the : removes the (non)nullity checks, e.g. ${FOO-val} expands to val if unset otherwise $FOO.

Substrings

ExpressionDescription
${FOO:0:3}Substring (position, length)
${FOO:(-3):3}Substring from the right

Longueur

ExpressionDescription
${#FOO}Taille de la variable $FOO

Braket expension

echo {A,B}.js
ExpressionDescription
{A,B}Same as A B
{A,B}.jsSame as A.js B.js
{1..5}Same as 1 2 3 4 5

Manipulation

variable

STR="HELLO WORLD!"
echo ${STR,} #=> "hELLO WORLD!" (lowercase 1st letter)
echo ${STR,,} #=> "hello world!" (all lowercase)

STR="hello world!"
echo ${STR^} #=> "Hello world!" (uppercase 1st letter)
echo ${STR^^} #=> "HELLO WORLD!" (all uppercase)
name="John"
echo ${name}
echo ${name/J/j} #=> "john" (substitution)
echo ${name:0:2} #=> "Jo" (slicing)
echo ${name::2} #=> "Jo" (slicing)
echo ${name::-1} #=> "Joh" (slicing)
echo ${name:(-1)} #=> "n" (slicing from right)
echo ${name:(-2):1} #=> "h" (slicing from right)
echo ${food:-Cake} #=> $food or "Cake"

répertoire

STR="/path/to/foo.cpp"
echo ${STR%.cpp} # /path/to/foo
echo ${STR%.cpp}.o # /path/to/foo.o
echo ${STR%/*} # /path/to

echo ${STR##*.} # cpp (extension)
echo ${STR##*/} # foo.cpp (basepath)

echo ${STR#*/} # path/to/foo.cpp
echo ${STR##*/} # foo.cpp

echo ${STR/foo/bar} # /path/to/bar.cpp
STR="Hello world"
echo ${STR:6:5} # "world"
echo ${STR: -5:5} # "world"
SRC="/path/to/foo.cpp"
BASE=${SRC##*/} #=> "foo.cpp" (basepath)
DIR=${SRC%$BASE} #=> "/path/to/" (dirpath)

Substitution

CodeDescription
${FOO%suffix}Supprimer suffix
${FOO#prefix}Supprimer prefix
------
${FOO%%suffix}Supprimer long suffix
${FOO##prefix}Supprimer long prefix
------
${FOO/from/to}Remplacer premier match
${FOO//from/to}Remplacer tout
------
${FOO/%from/to}Remplacer suffix
${FOO/#from/to}Remplacer prefix

Tableau

Definition d'un tableau

Fruits=('Apple' 'Banana' 'Orange')
Fruits[0]="Apple"
Fruits[1]="Banana"
Fruits[2]="Orange"

Gestion d'un tableau

echo ${Fruits[0]} # Element #0
echo ${Fruits[-1]} # Last element
echo ${Fruits[@]} # All elements, space-separated
echo ${#Fruits[@]} # Number of elements
echo ${#Fruits} # String length of the 1st element
echo ${#Fruits[3]} # String length of the Nth element
echo ${Fruits[@]:3:2} # Range (from position 3, length 2)
echo ${!Fruits[@]} # Keys of all elements, space-separated

Operations sur un tableau

Fruits=("${Fruits[@]}" "Watermelon") # Push
Fruits+=('Watermelon') # Also Push
Fruits=( ${Fruits[@]/Ap*/} ) # Remove by regex match
unset Fruits[2] # Remove one item
Fruits=("${Fruits[@]}") # Duplicate
Fruits=("${Fruits[@]}" "${Veggies[@]}") # Concatenate
lines=(`cat "logfile"`) # Read from file

Utilisation d'un tableau en shell

#!/bin/bash

declare -A dict
dict=( [‘a’]=1
[‘b’]=2
[‘c’]=3 )

for item in “${!dict[@]}”
do
echo “$item => ${dict[$item]}”
done

Dictionnaire

Définition

On déclare sound en tant que dictionnaire (couple clé:valeur)

declare -A sounds
sounds[dog]="bark"
sounds[cow]="moo"
sounds[bird]="tweet"
sounds[wolf]="howl"
echo ${sounds[dog]} # Dog's sound
echo ${sounds[@]} # All values
echo ${!sounds[@]} # All keys
echo ${#sounds[@]} # Number of elements
unset sounds[dog] # Delete dog

Iteration sur les valeurs

for val in "${sounds[@]}"; do
echo $val
done

Iteration sur les clés

for key in "${!sounds[@]}"; do
echo $key
done

Boucles (loop)


Boucle simple

for i in /etc/rc.*; do
echo $i
done

Boucle incrémentale

for ((i = 0 ; i < 100 ; i++)); do
echo $i
done

Boucle "range"

for i in {1..5}; do
echo "Welcome $i"
done

Boucle "etape"

for i in {5..50..5}; do
echo "Welcome $i"
done

Boucle avec lecture (read)

cat file.txt | while read line; do
echo $line
done

Boucle while

while true; do
···
done

Grep

Afficher un ou des process sans le process grep himself

ps - elf | grep squid | grep -v grep
ou
ps -elf | grep [s]quid

Affichier un fichier sans les commentaires

grep -E -v '^(#|$)' <fichier>
ou
egrep -v '^(#|$)' <fichier>

Grep avec multiple patterns

egrep -i "apache|lsyncd" <file>
ou
grep -i -e apache -e lsyncd <file>

Grep d'un pattern

if grep -q 'foo' ~/.bash_history; then
echo "You appear to have typed 'foo' in the past"
fi

Gestion des pipe

Variable PIPESTATUS

ls toto 2>&1 | tee -a toto.log;
echo ${PIPESTATUS[0]} #RC Avant pipe
echo $? #RC après pipe

AWK

Supprimer les caractères ^M

awk 'sub(/^M/, "");1' <ficsource> <ficmaj>

Afficher champ selon séparateur

awk -F"\t" '{ print $56 }'
note

Afficher le premier champ de chaque ligne avec tabulation en séparateur de champ

Remplacer EXPR par AWK

awk 'BEGIN{print 1024 / 1000}'

Selectioner la première ou la dernière ligne + imprimer un champ

awk 'BEGIN{print $2}'
awk 'END{print $2}'

Sed

Supprimer les caractères ASCII ^M

sed 's/^M//g' <ficsource> <ficmaj>
ou
sed -i 's/^M//g' <ficsource>

Critère de recherche

sed -e " //{N;s/Hxxxxx/Pxxxxx/g;} "\

Supprimer les caractères "Newline" (LF)

sed -i '{:q;N;s/\n//g;t q}' days.txt

Supprimer les 10 premiers caractères de chaque ligne

sed -i 's/^.\{10\}//g' fichier.txt

Affichier une ligne en particulier

<code>sed -n 1p check_toto.txt
note

Ici la première ligne

Savoir si une variable est numérique

if [ -n "$(echo $var | sed 's/[0-9]//g')" ]; then
echo 'is not numeric'
else
echo 'is numeric'
fi

Basename

Lister les fichiers sans extensions

for i in "$@";
do
BN=$(basename "${i%.*}")
...
done

Directory of script

DIR="$(readlink -f "$(dirname "$0")/../data/transfers")"
note

readlink : permet le suivi des liens

Afficher le nom du script exécuté

Le script $(basename ${0}) est correctement exécuté

Find

Renommer l'extension de plusieurs fichiers

find . -name '*.mp4' | rename mp4 txt *

Recherche récursive avec "mot-clé"

find / -xdev -type f -exec grep -i toto {} /dev/null \;
note

recherche récursive de tous les fichiers qui contiennent le mot « toto » (ou TOTO… ou ToTo…)

caution

Attention : « ; » obligatoire à la fin de la commande

tip

L’option xdev permet de ne pas franchir les limites du systeme de fichiers

Purge de fichiers

find . -type f -mtime +7 -name « *.trc » -exec rm -f {} \;
find . -type f -mtime +2 -size xx -exec rm {}; avec xx = 10M ou 10K par ex
find . -type f -mtime +2 -name /*/backup/*/ -exec rm {}; (enlever les / )
find . -type f -mtime +1 -exec rm

Recherche "mot-clé" avec redirection log

find . -type f -print | xargs grep -i « my_string_to_search »> /tmp/search.log 2>/dev/null;

Recherche des fichiers sans arborescence répertoire

find . -name '*.txt' -exec basename {} \;

Test

Condition d'execution

git commit && git push
git commit || echo "Commit failed"
if [[ -z "$string" ]]; then
echo "String is empty"
elif [[ -n "$string" ]]; then
echo "String is not empty"
fi
ConditionDescription
[[ -z STRING ]]Empty string
[[ -n STRING ]]Not empty string
[[ STRING == STRING ]]Equal
[[ STRING != STRING ]]Not Equal
------
[[ NUM -eq NUM ]]Equal
[[ NUM -ne NUM ]]Not equal
[[ NUM -lt NUM ]]Less than
[[ NUM -le NUM ]]Less than or equal
[[ NUM -gt NUM ]]Greater than
[[ NUM -ge NUM ]]Greater than or equal
------
[[ STRING =~ STRING ]]Regexp
------
(( NUM < NUM ))Numeric conditions

More conditions

ConditionDescription
[[ -o noclobber ]]If OPTIONNAME is enabled
------
[[ ! EXPR ]]Not
[[ X && Y ]]And
`[[ X

File conditions

ConditionDescription
[[ -e FILE ]]Exists
[[ -r FILE ]]Readable
[[ -h FILE ]]Symlink
[[ -d FILE ]]Directory
[[ -w FILE ]]Writable
[[ -s FILE ]]Size is > 0 bytes
[[ -f FILE ]]File
[[ -x FILE ]]Executable
------
[[ FILE1 -nt FILE2 ]]1 is more recent than 2
[[ FILE1 -ot FILE2 ]]2 is more recent than 1
[[ FILE1 -ef FILE2 ]]Same files

Example

# String
if [[ -z "$string" ]]; then
echo "String is empty"
elif [[ -n "$string" ]]; then
echo "String is not empty"
else
echo "This never happens"
fi
# Combinations
if [[ X && Y ]]; then
...
fi
# Equal
if [[ "$A" == "$B" ]]
# Regex
if [[ "A" =~ . ]]
if (( $a < $b )); then
echo "$a is smaller than $b"
fi
if [[ -e "file.txt" ]]; then
echo "file exists"
fi

Fonctions

Gestion des erreurs

log_and_die() {
>&2 echo "[FATAL] $*"
exit 1
}