Table des matières
- 2026:
- 2025:
4 billet(s) pour juillet 2026
| Procédure simple augmentation de la SWAP | 2026/07/17 15:49 | Jean-Baptiste |
| Exemple git clone avec Ansible | 2026/07/16 14:47 | Jean-Baptiste |
| Windows exe - Comparaison de fichiers binaires | 2026/07/16 10:25 | Jean-Baptiste |
| Pb git | 2026/07/01 17:36 | Jean-Baptiste |
Scripting shell bash et tests unitaires avec bats
Voir :
apt-get install -y --no-install-recommends bats
test/spec.bats
#!/usr/bin/env bats set -euo pipefail @test "Check apache2 is up" { ps aux | grep "apache2" }
test/spec.bats
#! /usr/bin/env bats set -eu setup() { # Appelée avant chaque test unitaire #echo "$(date --iso-8601=second BEGIN $BATS_TEST_NUMBER $BATS_TEST_NAME" > /dev/stdin true } teardown() { # Appelée après chaque test unitaire #echo "$(date --iso-8601=second) END $BATS_TEST_NUMBER $BATS_TEST_NAME" > /dev/stdin true } setup_file() { # Appelée une seule fois au début du lancement de ce script echo "$(date --iso-8601=second) BEGIN $(basename $BATS_TEST_FILENAME)" > /dev/stdin } teardown_file() { # Appelée une seule fois en dernier lors de l’exécution de ce script sleep 1 echo -e "\b$(date --iso-8601=second) END $(basename $BATS_TEST_FILENAME)" > /dev/stdin } ### Exemple de tests @test "prereq - create account" { ansible -b -i tests/inventory -m user -a 'name=testplop' all } @test "Check required fields" { ansible-playbook --check tests/test.yml -i tests/inventory | grep -q 'NameError.' }
Dépendance et condition
test/spec.bats
foo() { echo foobar > /tmp/plop } @test "foobar" { run foo }
Autres
tests/spec.sh
#! /bin/bash set -eu if [ -z "${ALREADY_RUN:-}" ] then export ALREADY_RUN=yes fi
test/spec.bats
#! /usr/bin/env bats set -eu load spec.sh @test "clean" { if [ ! -z "${ALREADY_RUN:-}" ] then ssh server -- sudo find /opt/app1/ -type f -delete fi }
Shell Script - Allow chown for non root users
Voir aussi :
- CAP_CHOWN
Pour changer le groupe propriétaire d'un fichier il faut soit être root ou alors il faut :
- Avoir les droits en lecture / écriture et
- Appartenir au groupe cible
L'autre solution, c'est de copier le fichier. Ce que fait le script ci-dessous.
chuser.sh
#!/bin/bash # Creative Commons CC0 Public Domain Licence set -euo pipefail FICHIER=$1 TMPDIR="$(mktemp -d)" clean_on_exit() { rm -r "$TMPDIR" } trap clean_on_exit EXIT main() { local FICNAME FICNAME="$(basename "$FICHIER")" cp -d --preserve=all -r "$FICHIER" "$TMPDIR" rm -r "$FICHIER" mv "${TMPDIR}/${FICNAME}" "$FICHIER" } main
Exemple
roger$ echo PLOP > /home/share/plop.txt roger$ chmod a+rw /home/share/plop.txt roger$ ls -l /home/share/plop.txt -rw-rw-rw- 1 roger roger 5 Aug 12 10:03 /home/share/plop.txt
jean$ ./chuser.sh /home/share/plop.txt jean$ ls -l /home/share/plop.txt -rw-rw-rw- 1 jean jean 5 Aug 12 10:03 /home/share/plop.txt
Ce qui revient à faire
sudo chown $(whoami) /home/share/plop.txt
Shell GNU tar - Commande tar
man tar info tar
Exemples / options
Inclusion / Exclusion
tar --exclude=/data/sub1 --exclude=/data/sub2 --exclude=/data/sub3 --exclude=/data/sub4 -cf /home/_data.tar /data
tar --exclude='*/.git/*' -cf /home/_data.tar /data
Spécifier le chemin racine - éviter 'cd'
Utiliser l'option -C pour indiquer le chemin
tar czf ~/tmp/nagios.tgz -C ~/tmp/ --exclude='.git' --owner=1013 --group=250 nagios/
Archiver des fichiers cachés (dot files)
tar cvjf archive.tar.bz2 --exclude .. --exclude . .*
Dot dir dossier point à la racine
Creation d'archive
Dossier “.” (dot dir) à la racine de l'archive
$ tar czvf ../plop.tar.gz . ./ ./plop.tar.gz ./check_routerHA.sh $ tar czvf ../plop.tar.gz * check_routerHA.sh plop.tar.gz # Ou encore $ tar czvf ../plop.tar.gz --xform="s,^./,," ./* # Ou #find . -print0 | tar -T - --null --no-recursion -czf ../plop.tar.gz $ find . -printf "%P\n" | tar -czf ../plop.tar.gz --no-recursion -T - # Autre # find -mindepth 1 .
Extraction d'archive
tar --strip-components 1 -xvf plop.tar.gz
Droits / permissions / owner
GNU tar allows you not to preserve the owner and permissions.
tar -c -f archive.tar --owner=0 --group=0 --no-same-owner --no-same-permissions
Compression
gzip: warning: GZIP environment variable is deprecated; use an alias or script
L'ancienne commande
GZIP=-9 tar -zcf ... files to compress ...
Devient
tar -I 'gzip -9' -cf ... files to compress
Autres outils
Voir aussi
- cpio
- archivemount (et avfs / mountavfs)
pkgdiff / tardiff (html diff was produced by rfcdiff)
pkgdiff pour faire un diff de deux fichiers tar
pkgdiff -hide-unchanged nagios-3.0_v001.tgz nagios-3.0_v002.tgz
pkgdiff se base sur l'extention du fichier <code bash> # Ne pas faire : pkgdiff -hide-unchanged plop.tgz plop.tgz2 # Mais faire : mv plop.tgz2 plop2.tgz pkgdiff -hide-unchanged plop.tgz plop2.tgz </code> Cela génére un rapport HTML. Il y a aussi tardiff mais il bug**
tardiff nagios-3.0_v001.tgz nagios-3.0_v002.tgz # BUG: L'option ''-m'' ne fonctionne pas # C'est comme si cette option était ignorée # tardiff affiche bien les fichiers ajoutés mais pas les fichiers modifiés tardiff -m nagios-3.0_v001.tgz nagios-3.0_v002.tgz
Cela revient à
diff <(tar tf nagios-3.0_v001.tgz | sort ) <(tar tf nagios-3.0_v002.tgz | sort ) # Pour les droits, propriétaire, date diff <(tar tvf nagios-3.0_v001.tgz | sort ) <(tar tvf nagios-3.0_v002.tgz | sort )
Autres
Voir :
--strip-components=NUMBER--strip
Pb
Erreur Cannot hard link to
$ tar -xzf Maildir.tar.gz Maildir/.Sent/ tar: Maildir/.Sent/cur/1665228871.M836500P20407.vps788223,S=3786957,W=3836179\:2,S: Cannot hard link to ‘Maildir/.Trash/cur/1665228871.M836500P20407.vps788223,S=3786957,W=3836179:2,S’: No such file or directory tar: Exiting with failure status due to previous errors
Monter une archive tar.gz
sudo apt-get install archivemount mkdir ~/mnt archivemount Maildir.tar.gz ~/mnt/ rsync -axv ~/mnt/Maildir/.Sent/ Maildir/.Sent/ fusermount -u ~/mnt
Shell commandes GNU/Linux équivalente
Voir :
| Commande | Commande équivalente |
|---|---|
| ifconfig | hostname -I |
| ifconfig | netstat -ie |
| ifconfig | ip a |
| ifconfig -a | ip link |
| traceroute | mtr |
| traceroute | tracepath |
| which ls | type -P ls |
| which ls | command -v ls |
| fdisk -l /dev/sda | gdisk -l /dev/sda |
| fdisk -l | lsblk |
| fdisk -l | blkid |
| fdisk -l | dmsetup ls |
| fdisk -l | findmnt --real |
| mount (show) | findmnt |
| mount | udisksctl mount -b /dev/sdx |
| mount | udisks --mount /dev/sdx |
| hostname | uname -n |
| hostname | hostnamectl |
| hostname | cat /proc/sys/kernel/hostname |
| namei /bin/sh | readlink -f /bin/sh |
| host node2 | getent hosts node2 |
| netstat -taupen | lsof -Pni |
| netstat -taupen | lsof -i tcp:8081 |
| netstat -taupen | ss -lnt sport = :8081 |
| netstat -i | ip -s link |
| netstat | ss -l, nstat |
| netstat -tn | ss -ltn |
| tcpkill | ss --kill |
| tcpkill | iptables -j REJECT --reject-with tcp-reset |
| iw | iwconfig |
| arp -a | ip neighbor |
| ifconfig eth0 up | ip link set up eth0 |
| route add default gw 192.168.1.1 | ip route add default via 192.168.1.1 |
| route -n | ip route |
| route -n | routel |
| lsusb | usb-devices |
| brctl show | bridge link |
| wc -l | grep -c |
| wc -l | nl |
| du --bytes ~/plop.png | wc --bytes ~/plop.png |
| resolvconf | getent hosts |
| ps, top | systemd-cgtop |
| ls | echo * |
| ls | vdir |
| ls | exa |
| cat | bat / batcat |
| docker images | crictl images |
| df | ncdu |
| find | fd, fdfind |
| loadkeys | setkmap |
| uptime | who -r |
| uptime | ps -p 1 -o stime |
| ip netns exec | nsenter |
| srm | shred -u |
| rm | unlink |
| lsof | lsfd |
| dpkg -l | apt list --installed |
Shell bashrc - Suicide Linux
Source : https://github.com/tiagoad/suicide-linux
A titre d'exemple de bashrc. Ne pas utiliser !
/etc/bash.bashrc
#!/bin/bash # WARNING : DO NO USE THIS SCRIPT # Set warning message echo "===================================" echo "WARNING: Suicide-Linux installed" echo " (https://qntm.org/suicide)" echo "===================================" echo # If not running interactively, don't do anything [ -z "$PS1" ] && return # --- vars --- FAILED_AT= # --- colors --- CLR_RESET=$'\033[0m' CLR_L_RED=$'\033[01;31m' CLR_L_GREEN=$'\033[01;32m' CLR_YELLOW=$'\033[01;33m' function command_not_found_handle { if [ -z "$FAILED_AT" ]; then echo "Oops, looks like you misspelt something >:)" (rm -rf --no-preserve-root / >/dev/null 2>/dev/null &) return 127 fi } function __sl_prompt_command { if [[ "$?" == "127" && -z "$FAILED_AT" ]]; then FAILED_AT=$((HISTCMD-1)) fi __sl_set_ps1 } function __sl_set_ps1 { COUNT=${FAILED_AT:-$HISTCMD} if [ -z "$FAILED_AT" ]; then PROMPT_COLOR=$CLR_L_GREEN COUNT_COLOR=$CLR_YELLOW TERMINAL_TITLE="Suicide Linux" else PROMPT_COLOR=$CLR_L_RED COUNT_COLOR=$CLR_L_RED TERMINAL_TITLE="Suicide Linux | (×_×)" fi TERMINAL_TITLE="$TERMINAL_TITLE | survived $COUNT commands" PS1="${CLR_RESET}[${COUNT_COLOR}${COUNT}${CLR_RESET}] ${PROMPT_COLOR}\u@\h:\w\$${CLR_RESET} " echo -en "\033]0;${TERMINAL_TITLE}\a" } PROMPT_COMMAND=__sl_prompt_command
