This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
program_lang:bash [2022/11/07 13:05] manu [trap] |
program_lang:bash [2024/04/17 16:06] (current) manu |
||
---|---|---|---|
Line 1: | Line 1: | ||
====== BASH ====== | ====== BASH ====== | ||
+ | |||
+ | ===== IF ===== | ||
+ | |||
+ | if or compare text | ||
+ | if [ "$fname" = "a.txt" ] || [ "$fname" = "c.txt" ] | ||
+ | if [ $a == $b -o $b == $c -o $a == $c ] | ||
+ | | ||
+ | if and | ||
+ | if [ $a == $b -a $b == $c -a $a == $c ] | ||
+ | if [ "$fname" = "a.txt" ] && [ "$fname" = "c.txt" ] | ||
+ | |||
+ | ===== Use CSV file ===== | ||
+ | |||
+ | ==== Change column order ==== | ||
+ | |||
+ | Switch column 1 and 3 serared by ; | ||
+ | <cli prompt='#'> | ||
+ | [root@lnx01 ~]# echo "1;2;3;4;5" | awk -F';' '{ t = $3 ; $3 = $1; $1 = t; print; }' OFS=';' | ||
+ | 3;2;1;4;5 | ||
+ | </cli> | ||
===== backup files and folders permissions ===== | ===== backup files and folders permissions ===== | ||
Line 99: | Line 119: | ||
arr=( $(ls) ) Save ls output as an array of files | arr=( $(ls) ) Save ls output as an array of files | ||
${arr[@]:s:n} Retrieve n elements starting at index s | ${arr[@]:s:n} Retrieve n elements starting at index s | ||
+ | |||
+ | Exemple | ||
+ | <cli prompt='#'> | ||
+ | #!/usr/bin/bash | ||
+ | declare -a Users=('user01' '1001' '1000' '' '' '/bin/bash') | ||
+ | Users+=('user02' '1002' '1003' '' '' '/bin/bash') | ||
+ | Users+=('user03' '1003' '4' 'Recovery user' '' '/bin/bash') | ||
+ | |||
+ | UserNbProps=6 | ||
+ | let NbUsers=${#Users[@]}/$UserNbProps | ||
+ | |||
+ | for (( i=0; i<$NbUsers; i++ )) | ||
+ | do | ||
+ | let ArrayIndex=i*UserNbProps | ||
+ | Line=$(cat /etc/passwd | grep "^${Users[$ArrayIndex]}:") | ||
+ | if [ -z "$Line" ] | ||
+ | then | ||
+ | echo "# ${Users[$ArrayIndex]} user was not found" | ||
+ | else | ||
+ | Uid=$(echo $Line | cut -d: -f3) | ||
+ | Gid=$(echo $Line | cut -d: -f4) | ||
+ | UidRef=${Users[$ArrayIndex+1]} | ||
+ | GidRef=${Users[$ArrayIndex+2]} | ||
+ | if [ $Uid != $UidRef -o $Gid != $GidRef ] | ||
+ | then | ||
+ | echo "# User: ${Users[$ArrayIndex]} uid:$Uid gid:$Gid does not match reference uid:$UidRef gid:$GidRef" | ||
+ | fi | ||
+ | fi | ||
+ | done | ||
+ | </cli> | ||
| | ||
===== Read files ===== | ===== Read files ===== | ||
Line 128: | Line 178: | ||
</cli> | </cli> | ||
- | Example | + | **Example** |
<cli> | <cli> | ||
- | # A control-c was trapped | + | #!/bin/bash |
- | ctrlc() | + | #trapoff_on |
- | { | + | |
- | log "Control-C trapped" | + | |
- | quit -1 | + | |
- | } | + | |
- | # Trap the control-c | + | trap '' SIGINT SIGQUIT |
- | trap ctrlc INT | + | echo "you cannot terminate using ctrl‑c or ctrl‑\, " |
+ | #heavy pressing go on here, cannot interrupt ! | ||
+ | sleep 10 | ||
+ | |||
+ | trap 'echo terminated; exit' SIGINT SIGQUIT | ||
+ | #user can now interrupt | ||
+ | echo "ok you can now terminate me using those keystrokes" | ||
+ | sleep 10 | ||
+ | </cli> | ||
+ | |||
+ | Example, to remove a file | ||
+ | <cli> | ||
+ | trap 'rm /tmp/hold∗.$$; exit' SIGNHUP SIGINT SIGQUIT SIGTERM | ||
</cli> | </cli> | ||
Line 156: | Line 214: | ||
... | ... | ||
</cli> | </cli> | ||
+ | |||
+ | You can use either signal name or signal number. | ||
+ | |||
+ | === I have received a signal. Now what? === | ||
+ | |||
+ | When a signal has been received by the script, the script can do one of three actions: | ||
+ | * Ignore it and do nothing. This is probably what most scripts do without the script authors realising it. | ||
+ | * Catch the signal using trap and take appropriate action. | ||
+ | * Take the default action. | ||
+ | |||
+ | All the above is true except for the following signals: | ||
+ | * SIGKILL (signal 9) | ||
+ | * SIGSTOP(signal 17) | ||
+ | * SIGCONT (signal 19) |