Quelques scripts personnels

Postez ici vos scripts Bash, Python, etc.
Répondre
Avatar du membre
Zergy
Messages : 14
Enregistré le : lun. 8 août 2022 16:23
Localisation : Villepreux
Contact :

Quelques scripts personnels

Message par Zergy »

Hop,

Comme je suis fainéant, j'utilise moi aussi quelques scripts.

Colorisation des textes et gestion des messages et erreurs :
Par vraiment des scripts complets, mais deux scripts que j'utilise souvent dans d'autres.

Colorisation des texts
  • Type : Bash
  • Droits : root:staff, 0644
  • Emplacement par défaut : /usr/local/lib/textColours

Code : Tout sélectionner

# color reset
res=$(tput sgr0)

# emphasized (bolded) text
em=$(tput bold)

# regular colors
k=$(tput setaf 0)
r=$(tput setaf 1)
g=$(tput setaf 2)
y=$(tput setaf 3)
b=$(tput setaf 4)
m=$(tput setaf 5)
c=$(tput setaf 6)
w=$(tput setaf 7)

# background colors
bgk=$(tput setab 0)
bgr=$(tput setab 1)
bgg=$(tput setab 2)
bgy=$(tput setab 3)
bgb=$(tput setab 4)
bgm=$(tput setab 5)
bgc=$(tput setab 6)
bgw=$(tput setab 7)
bglk=$(tput setab 8)
bglr=$(tput setab 9)
bglg=$(tput setab 10)
bgly=$(tput setab 11)
bglb=$(tput setab 12)
bglm=$(tput setab 13)
bglc=$(tput setab 14)
bglw=$(tput setab 15)

# emphasized (bolded) colors
emk=${em}${k}
emr=${em}${r}
emg=${em}${g}
emy=${em}${y}
emb=${em}${b}
emm=${em}${m}
emc=${em}${c}
emw=${em}${w}
Gestion des messages
  • Type : BASH
  • Droits : root:staff, 0644
  • Emplacement par défaut : /usr/local/lib/messagesFunctions

Code : Tout sélectionner

#! /usr/bin/env bash

. "/usr/local/lib/textColours"

actionMessage() {
        action_type="${1}";
        action_message="${2}";

        tput civis;

        case ${action_type} in
        "action")
                echo -en "[....] ${action_message}";
        ;;
        "info")
                echo -e "[${c}info${res}] ${action_message}";
        ;;
        esac
}

# Gestion des erreurs
errorCheck() {
        error_code="${?}";
        error_message="${2}";
        exit="${1}";
 
        tput hpa 0;
 
        if [ ${error_code} == 0 ]; then
                echo -e "[${g} ok ${res}]";
                tput cnorm;
        else
                if [ ${exit} == "1" ]; then
                        tput el;
                        echo -e "[${r}FAIL${res}] ${error_message} (${error_code})";
                        tput cnorm;
                        exit ${error_code};
                else
                        tput el;
                        echo -e "[${y}warn${res}] ${error_message} (${error_code})";
                        tput cnorm;
                fi
        fi
}

Installer ou mettre à jour DevilutionX
DevilutionX est un portage de Diablo 1 pour les OS moderne, le jeu est considéré comme un abandongiciel vu que Blizzard ne le vend plus, il vous faudra toutefois les fichiers .mpq du jeu original (disponible chez GOG) pour qu'il démarre.

Fichier de configuration :
  • Type : BASH
  • Droits : root:staff, 0644
  • Emplacement par défaut : /usr/local/etc/update-devilutionx.conf

Code : Tout sélectionner

# Packages to install
fedora_packages_list="cmake gcc-c++ glibc-devel libstdc++-static SDL2-devel libsodium-devel libpng-devel bzip2-devel gmock-devel gtest-devel libasan libubsan fmt-devel"
suse_packages_list="cmake curl gcc-c++ glibc-devel libstdc++-devel libSDL2-devel libsodium-devel libpng16-devel libbz2-devel libasan8 libubsan1 fmt-devel"
ubuntu_packages_list="cmake curl g++ libsdl2-dev libsodium-dev libpng-dev libbz2-dev libgtest-dev libgmock-dev libsdl2-image-dev libfmt-dev smpq gettext poedit"

# Git download directory
git_dir="/tmp/devilutionx.XXXXXXXXXXXX"

# Github/Gitlab repository
git_repo="diasurgical/devilutionX"

# Installation directory
install_dir="/opt/devilutionx"

# Log file
log_file="/var/log/update-devilutionx.log"

# Data file URL and name
data_file="devilutionx-data.txz"
date_url="XXX" # URL contenant l'archive
Fichier éxécutable :
  • Type : BASH
  • Droits : root:staff, 0755
  • Emplacement par défaut : /usr/local/sbin/update-devilutionx

Code : Tout sélectionner

#! /usr/bin/env bash
 
################################################################################
#                                                                              #
# LICENCE                                                                      #
#                                                                              #
# This program is free software: you can redistribute it and/or modify it      #
# under the terms of the GNU General Public License as published by the Free   #
# Software Foundation, either version 3 of the License, or (at your option)    #
# any later version.                                                           #
#                                                                              #
# This program is distributed in the hope that it will be useful, but WITHOUT  #
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or        #
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for     #
# more details.                                                                #
#                                                                              #
# You should have received a copy of the GNU General Public License along with #
# this program.  If not, see <http://www.gnu.org/licenses/>.                   #
#                                                                              #
################################################################################
 
################################################################################
#                                                                              #
# DESCRIPTION                                                                  #
#                                                                              #
# Ce programme permet la mise à jour du jeu DevilutionX (Diablo), il se        #
# contente de récupérer la dernière version via git et de l'installer via      #
# compilation.                                                                 #
#                                                                              #
#                                                                              #
# VARIABLES                                                                    #
#                                                                              #
# - fedora_packages_list : Liste des paquets à installer sur les distributions #
#   Fedora ou dérivées.                                                        #
# - ubuntu_packages_list : Listes de paquets à installer sur les distributions #
#   Ubuntu ou dérivées.                                                        #
# - git_dir : Dossier où le contenu de la commande git sera stocké.            #
# - install_dir : Dossier où le logiciel sera installé.                        #
#                                                                              #
#                                                                              #
# FONCTIONS                                                                    #
#                                                                              #
# - check_deps() : Vérifier la version de la distribution et installe les      #
#   paquets de dépendances requis.                                             #
# - install() : Utilise les commandes de compilation nécessaire à              #
#   l'installation du programme.                                               #
#                                                                              #
#                                                                              #
# AUTEUR(S)                                                                    #
#                                                                              #
# - Collyer Cédric <collyer.cedric@zergy.net>                                  #
#                                                                              #
#                                                                              #
# DERNIER(S) CHANGEMENT(S) :                                                   #
#                                                                              #
# - 02/08/2022 : Débogage                                                      #
# - 06/03/2021 : Version 1.0                                                   #
#                                                                              #
################################################################################
 
# Variables
PATH="/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin";
 
. "/usr/local/lib/messagesFunctions";
. "/usr/local/etc/update-devilutionx.conf";
 
check_deps() {
        distro=$(lsb_release -i | awk '{ print $3 }');
        case ${distro} in
                "CentOS" | "Fedora" | "RedHatEnterpriseServer" | "Scientific")
                        actionMessage "action" "Vérification des paquets installés...";
                        /usr/bin/dnf -qy install ${fedora_packages_list} > /dev/null 2> "${log_file}";
                        errorCheck 1 "Erreur lors de la vérification des paquets.";
                        ;;
                "Debian" | "Linuxmint" | "Ubuntu")
                        actionMessage "action" "Vérification des paquets installés...";
                        /usr/bin/apt --quiet=2 --yes install ${ubuntu_packages_list} > /dev/null 2> "${log_file}";
                        errorCheck 1 "Erreur lors de la vérification des paquets.";
                        ;;
                "openSUSE")
                        actionMessage "action" "Vérification des paquets installés...";
                        /usr/bin/zypper --quiet --non-interactive install ${SUSE_PACKAGES_LIST} > /dev/null 2> "${log_file}";
                        errorCheck 1 "Erreur lors de la vérification des paquets.";
                        ;;
                *)
                        echo -e "Ce programme n'est compatible qu'avec les distributions suivantes :\n- CentOS\n- Fedora\- RHEL\n- Scientific Linux\n- Debian\n- Linux Mint\n- Ubuntu";
                        exit 1;
                        ;;
        esac
}

install() {
        latest_version=$(curl --silent "https://api.github.com/repos/${git_repo}/releases/latest" | grep '"tag_name":' | sed -E 's/.*"([^"]+)".*/\1/');
        actionMessage "action" "Clonage du dépôt GIT...";
        temp_dir=$(mktemp -d "${git_dir}" 2>> "${log_file}") && git clone https://github.com/${git_repo} "${temp_dir}" > /dev/null 2>> "${log_file}";
        errorCheck 1 "Erreur lors du clonage du GIT.";
        actionMessage "action" "Compilation...";
        cd "${temp_dir}" && cmake -S. -Bbuild -DCMAKE_BUILD_TYPE=Release > /dev/null 2>> "${log_file}" && cmake --build build -j $(nproc) > /dev/null 2>> "${log_file}"; 
        errorCheck 1 "Erreur lors de la compilation.";
        [ ! -d "${install_dir}" ] && mkdir -p "${install_dir}";
        [ -f "${install_dir}/devilutionx.mpq" -a -f "${install_dir}/devilutionx" -a -f "${install_dir}/devilutionx.desktop" -a -f "${install_dir}/devilutionx-hellfire.desktop" -a -f "${install_dir}/icon.png" -a -f "${install_dir}/hellfire.png" ] && rm -f "${install_dir}/devilutionx.mpq" "${install_dir}/devilutionx" "${install_dir}/devilutionx.desktop" "${install_dir}/devilutionx-hellfire.desktop" "${install_dir}/icon.png" "${install_dir}/hellfire.png";
        actionMessage "action" "Installation...";
        mv "./build/devilutionx.mpq" "./build/devilutionx" "./Packaging/nix/devilutionx.desktop" "./Packaging/nix/devilutionx-hellfire.desktop" "./Packaging/resources/icon.png" "./Packaging/resources/hellfire.png" "${install_dir}/" > /dev/null 2>> "${log_file}" && chgrp -R games ${install_dir} 2>> "${log_file}" && chmod -R g+w ${install_dir} 2>> "${log_file}";
        [ ! -h "/usr/local/games/devilutionx" ] && ln -s "/opt/devilutionx/devilutionx" "/usr/local/games/devilutionx";
        errorCheck 1 "Erreur lors de l'installation.";
        cd ~;
        rm -fr "${temp_dir}";

        # Décommentez la section suivant si vous disposez d'un serveur HTTP(S) avec les fichiers MPQ du jeu.
#        if [ ! -f "${install_dir}/diabdat.mpq" ]; then
#                actionMessage "action" "Récupération des données.";
#                wget "${date_url}/${data_file}" -O "/tmp/${data_file}" > /dev/null 2>> "${log_file}" && cd "${install_dir}" && tar -xJf "/tmp/${data_file}" 2>> "${log_file}" && chgrp games "${install_dir}/"*.mpq 2>> "${log_file}" && chmod g+w "${install_dir}/"*.mpq 2>> "${log_file}" && rm -f "/tmp/${data_file}";
#                errorCheck 1 "Erreur lors de la récupération des données.";
#        fi
}

main() {
        check_deps;
        install;
}
 
main;
 
exit 0;

Installer ou mettre à jour Hamsket
Hamsket est un logiciel pour centraliser tout vos réseaux asociaux (c'est un chromium spécialisé)

Fichier de configuration :
  • Type : BASH
  • Droits : root:staff, 0644
  • Emplacement par défaut : /usr/local/etc/update-hamsket.conf

Code : Tout sélectionner

# CPU architecture, ia32 or x64
arch="amd64"

# Github/Gitlab repository
git_repo="TheGoddessInari/hamsket"

# Installation directory
install_dir="/opt"

# Log file
log_file="/var/log/update-hamsket.log"
Fichier éxécutable :
  • Type : BASH
  • Droits : root:staff, 0755
  • Emplacement par défaut : /usr/local/sbin/update-hamsket

Code : Tout sélectionner

#! /usr/bin/env bash
 
################################################################################
#                                                                              #
# LICENCE                                                                      #
#                                                                              #
# This program is free software: you can redistribute it and/or modify it      #
# under the terms of the GNU General Public License as published by the Free   #
# Software Foundation, either version 3 of the License, or (at your option)    #
# any later version.                                                           #
#                                                                              #
# This program is distributed in the hope that it will be useful, but WITHOUT  #
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or        #
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for     #
# more details.                                                                #
#                                                                              #
# You should have received a copy of the GNU General Public License along with #
# this program.  If not, see <http://www.gnu.org/licenses/>.                   #
#                                                                              #
################################################################################
 
################################################################################
#                                                                              #
# DESCRIPTION                                                                  #
#                                                                              #
# Ce programme permet la mise à jour de Hamsket, il se contente de récupérer la#
# dernière version via git et de l'installer via décompression.                #
#                                                                              #
#                                                                              #
# VARIABLES                                                                    #
#                                                                              #
# - arch : Architecture processeur.                                            #
# - install_dir : Dossier où le logiciel sera installé.                        #
#                                                                              #
#                                                                              #
# FONCTIONS                                                                    #
#                                                                              #
# - install() : Utilise les commandes de compilation nécessaire à              #
#   l'installation du programme.                                               #
#                                                                              #
#                                                                              #
# AUTEUR(S)                                                                    #
#                                                                              #
# - Collyer Cédric <collyer.cedric@zergy.net>                                  #
#                                                                              #
#                                                                              #
# DERNIER(S) CHANGEMENT(S) :                                                   #
#                                                                              #
# - 02/08/2022 : Ajout de la création du lien symbolique                       #
# - 03/07/2021 : Version 1.0                                                   #
#                                                                              #
################################################################################
 
# Variables
PATH="/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin";
 
. "/usr/local/lib/messagesFunctions";
. "/usr/local/etc/update-hamsket.conf";
 
install() {
        latest_version=$(curl --silent "https://api.github.com/repos/${git_repo}/releases/latest" | grep '"tag_name":' | sed -E 's/.*"([^"]+)".*/\1/');
        actionMessage "action" "Téléchargement...";
        wget "https://github.com/${git_repo}/releases/download/${latest_version}/hamsket-${latest_version}.tar.gz" -O "/tmp/hamsket.tar.gz" > /dev/null 2>> "${log_file}";
        errorCheck 1 "Erreur lors du téléchargement.";
        actionMessage "action" "Installation...";
        [ -d "${install_dir}" ] && rm -fr "${install_dir}/hamsket" > /dev/null 2>> "${log_file}" && cd "${install_dir}" && tar -xvzf "/tmp/hamsket.tar.gz" > /dev/null 2>> "${log_file}" && mv "${install_dir}/hamsket-${latest_version}" "${install_dir}/hamsket" > /dev/null 2>> "${log_file}" && chmod -R go-w "${install_dir}/hamsket" && rm -f "/tmp/hamsket.tar.gz" > /dev/null 2>> "${log_file}"; 
        [ ! -h "/usr/local/bin/hamsket" ] && ln -s "/opt/hamsket/hamsket" "/usr/local/bin/hamsket";
        errorCheck 1 "Erreur lors de la l'installation.";
}

main() {
        install;
}
 
main;
 
exit 0;

Installer ou mettre à jour Razer Commander
Razer Commander est une interface GTK3 pour OpenRazer.

Fichier de configuration :
  • Type : BASH
  • Droits : root:staff, 0644
  • Emplacement par défaut : /usr/local/etc/update-razercommander.conf

Code : Tout sélectionner

# Packages to install
ubuntu_packages_list="libglib2.0-dev pkg-config libappstream-glib-dev"

# Git download directory
git_dir="/tmp/razercommander.XXXXXXXXXXXX"

# Github/Gitlab repository
git_repo="gabmus/razercommander"

# Installation directory
install_dir="/opt/razercommander"

# Log file
log_file="/var/log/update-razercommander.log"
Fichier éxécutable :
  • Type : BASH
  • Droits : root:staff, 0755
  • Emplacement par défaut : /usr/local/sbin/update-razercommander

Code : Tout sélectionner

#! /usr/bin/env bash
 
################################################################################
#                                                                              #
# LICENCE                                                                      #
#                                                                              #
# This program is free software: you can redistribute it and/or modify it      #
# under the terms of the GNU General Public License as published by the Free   #
# Software Foundation, either version 3 of the License, or (at your option)    #
# any later version.                                                           #
#                                                                              #
# This program is distributed in the hope that it will be useful, but WITHOUT  #
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or        #
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for     #
# more details.                                                                #
#                                                                              #
# You should have received a copy of the GNU General Public License along with #
# this program.  If not, see <http://www.gnu.org/licenses/>.                   #
#                                                                              #
################################################################################
 
################################################################################
#                                                                              #
# DESCRIPTION                                                                  #
#                                                                              #
# Ce programme permet la mise à jour du programme Razer Commander, il se       #
# contente de récupérer la dernière version via git et de l'installer via      #
# compilation.                                                                 #
#                                                                              #
#                                                                              #
# VARIABLES                                                                    #
#                                                                              #
# - ubuntu_packages_list : Listes de paquets à installer sur les distributions #
#   Ubuntu ou dérivées.                                                        #
# - git_dir : Dossier où le contenu de la commande git sera stocké.            #
# - install_dir : Dossier où le logiciel sera installé.                        #
#                                                                              #
#                                                                              #
# FONCTIONS                                                                    #
#                                                                              #
# - check_deps() : Vérifier la version de la distribution et installe les      #
#   paquets de dépendances requis.                                             #
# - install() : Utilise les commandes de compilation nécessaire à              #
#   l'installation du programme.                                               #
#                                                                              #
#                                                                              #
# AUTEUR(S)                                                                    #
#                                                                              #
# - Collyer Cédric <collyer.cedric@zergy.net>                                  #
#                                                                              #
#                                                                              #
# DERNIER(S) CHANGEMENT(S) :                                                   #
#                                                                              #
# - 02/08/2022 : Débogage                                                      #
# - 01/08/2022 : Version 1.0                                                   #
#                                                                              #
################################################################################
 
# Variables
PATH="/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin";

. "/usr/local/lib/messagesFunctions";
. "/usr/local/etc/update-razercommander.conf";
 
check_deps() {
        distro=$(lsb_release -i | awk '{ print $3 }');
        case ${distro} in
                "Debian" | "Linuxmint" | "Ubuntu")
                        actionMessage "action" "Vérification des paquets installés...";
                        /usr/bin/apt --quiet=2 --yes install ${ubuntu_packages_list} > /dev/null 2> "${log_file}";
                        errorCheck 1 "Erreur lors de la vérification des paquets.";
                        ;;
                *)
                        echo -e "Ce programme n'est compatible qu'avec les distributions suivantes :\n- Debian\n- Linux Mint\n- Ubuntu";
                        exit 1;
                        ;;
        esac
}

install() {
        actionMessage "action" "Clonage du dépôt GIT...";
        temp_dir=$(mktemp -d "${git_dir}" 2>> "${log_file}") && git clone https://github.com/${git_repo} "${temp_dir}" > /dev/null 2>> "${log_file}";
        errorCheck 1 "Erreur lors du clonage GIT.";
        mkdir "${temp_dir}/builddir";
        cd "${temp_dir}/builddir";
        actionMessage "action" "Compilation...";
        meson .. > /dev/null 2>> "${log_file}" && meson configure -Dprefix="${install_dir}" > /dev/null 2>> "${log_file}";
        errorCheck 1 "Erreur lors de la compilation.";
        actionMessage "action" "Installation...";
        ninja install > /dev/null 2>> "${log_file}";
        [ ! -h "/usr/local/share/applications/org.gabmus.razercommander.desktop" ] && ln -s "/opt/razercommander/share/applications/org.gabmus.razercommander.desktop" "/usr/local/share/applications/org.gabmus.razercommander.desktop";
        [ ! -h "/usr/local/share/pixmaps/org.gabmus.razercommander.svg" ] && ln -s "/opt/razercommander/share/icons/hicolor/scalable/apps/org.gabmus.razercommander.svg" "/usr/local/share/pixmaps/org.gabmus.razercommander.svg";
        [ ! -h "/usr/local/bin/razercommander" ] && ln -s "/opt/razercommander/bin/razercommander" "/usr/local/bin/razercommander";
        errorCheck 1 "Erreur lors de l'installation.";
        cd ~;
        rm -fr "${temp_dir}";
}

main() {
        check_deps;
        install;
}

main;
 
exit 0;

Installer ou mettre à jour CPUFetch
CPUFetch est un petit programme permettant d'avoir rapidement des infos sur son processeur.

Fichier de configuration :
  • Type : BASH
  • Droits : root:staff, 0644
  • Emplacement par défaut : /usr/local/etc/update-cpufetch.conf

Code : Tout sélectionner

# Git download directory
git_dir="/tmp/cpufetch.XXXXXXXXXXXX"

# Github/Gitlab repository
git_repo="Dr-Noob/cpufetch"

# Installation directory
install_dir="/usr/local/bin/"

# Log file
log_file="/var/log/update-cpufetch.log"
Fichier éxécutable :
  • Type : BASH
  • Droits : root:staff, 0755
  • Emplacement par défaut : /usr/local/sbin/update-cpufetch

Code : Tout sélectionner

#! /usr/bin/env bash
 
################################################################################
#                                                                              #
# LICENCE                                                                      #
#                                                                              #
# This program is free software: you can redistribute it and/or modify it      #
# under the terms of the GNU General Public License as published by the Free   #
# Software Foundation, either version 3 of the License, or (at your option)    #
# any later version.                                                           #
#                                                                              #
# This program is distributed in the hope that it will be useful, but WITHOUT  #
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or        #
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for     #
# more details.                                                                #
#                                                                              #
# You should have received a copy of the GNU General Public License along with #
# this program.  If not, see <http://www.gnu.org/licenses/>.                   #
#                                                                              #
################################################################################
 
################################################################################
#                                                                              #
# DESCRIPTION                                                                  #
#                                                                              #
# Ce programme permet la mise à jour du programme CPUFetch, il se              #
# contente de récupérer la dernière version via git et de l'installer via      #
# compilation.                                                                 #
#                                                                              #
#                                                                              #
# VARIABLES                                                                    #
#                                                                              #
# - git_dir : Dossier où le contenu de la commande git sera stocké.            #
# - install_dir : Dossier où le logiciel sera installé.                        #
#                                                                              #
#                                                                              #
# FONCTIONS                                                                    #
#                                                                              #
# - install() : Utilise les commandes de compilation nécessaire à              #
#   l'installation du programme.                                               #
#                                                                              #
#                                                                              #
# AUTEUR(S)                                                                    #
#                                                                              #
# - Collyer Cédric <collyer.cedric@zergy.net>                                  #
#                                                                              #
#                                                                              #
# DERNIER(S) CHANGEMENT(S) :                                                   #
#                                                                              #
# - 06/03/2021 : Version 1.0                                                   #
#                                                                              #
################################################################################
 
# Variables
PATH="/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin";
 
. "/usr/local/lib/messagesFunctions";
. "/usr/local/etc/update-cpufetch.conf";
 
install() {
        actionMessage "action" "Clonage du dépôt GIT...";
        temp_dir=$(mktemp -d "${git_dir}" 2>> "${log_file}") && git clone https://github.com/${git_repo} "${temp_dir}" > /dev/null 2>> "${log_file}";
        errorCheck 1 "Erreur lors du clonage GIT.";
        cd "${temp_dir}";
        actionMessage "action" "Compilation...";
        make -j $(nproc) > /dev/null 2>> "${log_file}"; 
        errorCheck 1 "Erreur lors de la compilation.";
        [ ! -d "${install_dir}" ] && mkdir -p "${install_dir}";
        [ -f "${install_dir}/cpufetch" ] && rm -f "${install_dir}/cpufetch";
        actionMessage "action" "Installation...";
        mv cpufetch "${install_dir}" > /dev/null 2>> "${log_file}";
        errorCheck 1 "Erreur lors de l'installation."
        cd ~;
        rm -fr "${temp_dir}";
}

main() {
        install;
}
 
main;
 
exit 0;

Mise à jour du système
Parce que je suis fainéant.
Attention, le script bloque si APT vous pose une question lors de la mise à jour d'un paquet (pour valider le changement d'un fichier de configuration par exemple)

Fichier de configuration :
  • Type : BASH
  • Droits : root:staff, 0644
  • Emplacement par défaut : /usr/local/etc/update-system.conf

Code : Tout sélectionner

# Lock files list
apt_lockfiles_list="/var/lib/apt/lock /var/lib/dpkg/lock /var/lib/apt/lists/lock /var/cache/apt/archives/lock"

# Acme.sh Binaries
acmesh_bin="/root/.acme.sh/acme.sh"

# Options for $APT_COMMAND.
apt_options="--quiet=2 --yes"

# Log file
log_file="/var/log/update-system.log"
Script de suppression des anciens Snaps :
  • Type : BASH
  • Droits : root:staff, 0755
  • Emplacement par défaut : /usr/local/sbin/remove-old-snaps

Code : Tout sélectionner

#! /usr/bin/env bash

# Removes old revisions of snaps
# CLOSE ALL SNAPS BEFORE RUNNING THIS
set -eu

LANG=en_US.UTF-8 snap list --all | awk '/disabled/{print $1, $3}' |
while read snapname revision; do
        snap remove "$snapname" --revision="$revision"
done
Fichier éxécutable :
  • Type : BASH
  • Droits : root:staff, 0755
  • Emplacement par défaut : /usr/local/sbin/update-system

Code : Tout sélectionner

#! /usr/bin/env bash

#################################################################################
#                                                                               #
# LICENCE :                                                                     #
#                                                                               #
# This program is free software: you can redistribute it and/or modify it under #
# the terms of the GNU General Public License as published by the Free Software #
# Foundation, either version 3 of the License, or (at your option) any later    #
# version.                                                                      #
#                                                                               #
# This program is distributed in the hope that it will be useful, but WITHOUT   #
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS #
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.#
#                                                                               #
# You should have received a copy of the GNU General Public License along with  #
# this program.  If not, see <http://www.gnu.org/licenses/>.                    #
#                                                                               #
#################################################################################

#################################################################################
#                                                                               #
# DESCRIPTION                                                                   #
#                                                                               #
# Ce script est conçu pour mettre à jour automatiquement les distributions      #
# Debian ou Ubuntu.                                                             #
#                                                                               #
# Si vous avez l'habitude d'utiliser apt-get et Synaptic vous devriez changer   #
# les commandes par les commandes apt-get appropriés.                           #
#                                                                               #
#                                                                               #
# VARIABLES                                                                     #
#                                                                               #
# - apt_bin : Commande de mise à jour. Peut être "apt-get", ou "aptitude".      #
# - apt_options : Options pour la commande de mise à jour.                      #
# - lockfiles_list : List des fichiers lock à supprimer.                        #
# - RKHUNTER_SUPPORT : Support de RKHunter si installé, le met à jour et        #
#   vérifie les fichier.                                                        #
# - SNAPS_SUPPORT : Support des Snaps.                                          #
# - UPDATE_HWIDS : Met à jour les ID matériel (PCI et USB).                     #
#                                                                               #
#                                                                               #
# AUTEUR(S)                                                                     #
#                                                                               #
# - Collyer Cédric <collyer.cedric@zergy.net>                                   #
#                                                                               #
#                                                                               #
# DERNIER(S) CHANGEMENT(S)                                                      #
#                                                                               #
# - 02/08/2022 : Débogage                                                       #
# - 19/05/2022 : Réécriture de la fonction update_packages, support de d'autres #
#   formats que les DEB.                                                        #
# - 18/05/2022 : Ajout de la mises à jour des flatpak.                          #
# - 25/04/2020 : Ajout de la mises à jour des snaps.                            #
# - 30/10/2016 : Ajout du choix de la commande de mise à jour.                  #
# - 08/05/2016 : Réécriture complète de script.                                 #
#                                                                               #
#################################################################################

# Variables
PATH="/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin";

. "/usr/local/lib/messagesFunctions";
. "/usr/local/etc/update-system.conf";

# Mise à jour des paquets
update_packages() {
        echo -e "Paquets";
        distro=$(lsb_release -i | awk '{ print $3 }');
        case $distro in
                "CentOS" | "Fedora" | "RedHatEnterpriseServer" | "Scientific")
                        dnf_bin=$(which dnf);
                        actionMessage "action" "Mise à jour...";
                        ${dnf_bin} --assumeyes --quiet update > /dev/null 2> "${log_file}";
                        errorCheck 1 "Erreur lors de la mise à jour des paquets.";
                        ;;
                "Debian" | "Linuxmint" | "Raspbian" | "Ubuntu")
                        apt_bin=$(which apt);
                        for apt_lockfile in ${apt_lockfiles_list}; do
                                if [ -e "${apt_lockfile}" ]; then
                                        rm -f "${apt_lockfile}";
                                fi
                        done
                        actionMessage "action" "Vérification...";
                        ${apt_bin} update --quiet=2 --yes > /dev/null 2> "${log_file}";
                        errorCheck 1 "Erreur lors de la vérification des paquets à mettre à jours.";
                        actionMessage "action" "Mise à jour...";
                        ${apt_bin} full-upgrade --quiet=2 --yes > /dev/null 2>> "${log_file}";
                        errorCheck 1 "Erreur lors de la mise à jour des paquets.";
                        actionMessage "action" "Suppression des obsolètes...";
                        ${apt_bin} autopurge --quiet=2 --yes > /dev/null 2>> "${log_file}";
                        errorCheck 1 "Erreur lors du nettoyage des paquets obsolètes.";
                        ${apt_bin} clean --quiet=2 --yes > /dev/null 2>> "${log_file}";
                        for apt_lockfile in ${apt_lockfiles_list}; do
                                if [ -e "${apt_lockfile}" ]; then
                                        rm -f "${apt_lockfile}";
                                fi
                        done
                        ;;
                "openSUSE")
                        zypper_bin=$(which zypper);
                        actionMessage "action" "Mise à jour...";
                        ${zypper_bin} --non-interactive --quiet update > /dev/null 2> "${log_file}" && ${zypper_bin} --non-interactive --quiet dup > /dev/null 2>> "${log_file}";
                        errorCheck 1 "Erreur lors de la mise à jour des paquets.";
                        ;;
                "VoidLinux")
                        xbpsinstall_bin=$(which xbps-install);
                        xbpsremove_bin=$(which xbps-remove);
                        actionMessage "action" "Mise à jour...";
                        ${xbpsinstall_bin} -Syu > /dev/null 2> "${log_file}";
                        errorCheck 1 "Erreur lors de la mise à jour des paquets.";
                        ${xbpsremove_bin} -yO > /dev/null 2>> "${log_file}"
                        ;;
                *)
                        echo -e "Ce programme n'est compatible qu'avec les distributions suivantes :\n- CentOS\n- Fedora\n- RHEL\n- Scientific Linux\n- Debian\n- Raspbian\n- Linux Mint\n- Ubuntu\n- openSUSE\n- Linux Void" && exit 5;
        esac;
}

# Mise à jour des snaps
update_snaps() {
        pidof -q snapd && snapd_active="1" || snapd_active="0";
        if [ ${snapd_active} -eq "1" ]; then
                echo -e "\nSnaps";
                snap_bin=$(which snap);
                actionMessage "action" "Mise à jour...";
                ${snap_bin} refresh > /dev/null 2>> "${log_file}";
                errorCheck 1 "Erreur lors de la mise à jour des snaps.";
                if [ -x /usr/local/sbin/remove-old-snaps ]; then
                        actionMessage "action" "Suppression des obsolètes...";
                        /usr/local/sbin/remove-old-snaps > /dev/null 2>> "${log_file}";
                        errorCheck 1 "Erreur lors du nettoyage des snaps obsolètes.";
                fi
        fi
}

# Mise à jour des flatpaks
update_flatpaks() {
        flatpak_bin=$(which flatpak 2>> /dev/null);
        if [ -x "${flatpak_bin}" ]; then
                echo -e "\nFlatpaks";
                actionMessage "action" "Mise à jour...";
                ${flatpak_bin} --noninteractive update > /dev/null 2>> "${log_file}";
                errorCheck 1 "Erreur lors de la mise à jour des flatpaks.";
        fi
}

# Mise à jour de acme.sh
update_acmesh() {
        if [ -x ${acmesh_bin} ]; then
                echo -e "\nAcme.sh";
                actionMessage "action" "Mise à jour...";
                ${acmesh_bin} --upgrade > /dev/null 2>> "${log_file}";
                errorCheck 0 "Erreur lors de la mise à jour de Acme.sh";
        fi
}

# Mise à jour des hardware IDs
update_hwids() {
        updatepciids_bin=$(which update-pciids 2>> /dev/null);
        if [ -x ${updatepciids_bin} ]; then
                echo -e "\nID matériels";
                actionMessage "action" "PCI...";
                ${updatepciids_bin} -q 2>> "${log_file}";
                errorCheck 0 "Erreur lors de la mise à jour des ID PCI."; 
        fi
}

# Mise à jour de Rootkit Hunter
update_rkhunter() {
        rkhunter_bin=$(which rkhunter 2>> /dev/null);
        if [ -x ${rkhunter_bin} ]; then
                echo -e "\nRootkit Hunter";
                actionMessage "action" "Mise à jour..."
                ${rkhunter_bin} -q --update 2>> "${log_file}";
                errorCheck 0 "Erreur lors de la mise à jour de rkhunter.";
                actionMessage "action" "Calcul des MD5...";
                ${rkhunter_bin} -q --propupd 2>> "${log_file}";
                errorCheck 0 "Erreur lors de la mise à jour des sommes MD5 de rkhunter.";
        fi
}

main() {
        update_packages;
        update_snaps;
        update_flatpaks;
        update_acmesh;
        update_hwids;
        update_rkhunter;
}

main;

exit 0;

Élagage (triming) des SSD
Permet d'automatiser l'élagage (triming) de vos SSD, à ne pas utiliser sur les partitions hébergés par un HDD classque !

Fichier de configuration :
  • Type : BASH
  • Droits : root:staff, 0644
  • Emplacement par défaut : /usr/local/etc/trim.conf

Code : Tout sélectionner

# Partitions to trim.
partitions="/boot/efi / /usr" # Points de montage des partitions sur SSD, séparées par un espace.
     
# Log file.
log_file="/var/log/trim.log";
Fichier éxécutable :
  • Type : BASH
  • Droits : root:staff, 0755
  • Emplacement par défaut : /usr/local/sbin/trim

Code : Tout sélectionner

#! /usr/bin/env bash
 
################################################################################
#                                                                              #
# LICENCE                                                                      #
#                                                                              #
# This program is free software: you can redistribute it and/or modify it      #
# under the terms of the GNU General Public License as published by the Free   #
# Software Foundation, either version 3 of the License, or (at your option)    #
# any later version.                                                           #
#                                                                              #
# This program is distributed in the hope that it will be useful, but WITHOUT  #
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or        #
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for     #
# more details.                                                                #
#                                                                              #
# You should have received a copy of the GNU General Public License along with #
# this program.  If not, see <http://www.gnu.org/licenses/>.                   #
#                                                                              #
################################################################################
 
################################################################################
#                                                                              #
# DESCRIPTION                                                                  #
#                                                                              #
# Ce script permet d'élaguer (trim) les partition utilisant un SSD.            #
#                                                                              #
#                                                                              #
# VARIABLES                                                                    #
#                                                                              #
# - partitions : Liste des partition à élaguer, ne mettre que celles utilisant #
#   un SSD !                                                                   #
# - log_file : Fichier utilisé pour la journalisation.                         #
# - log_file_date : Date de journalisation.                                    #
#                                                                              #
#                                                                              #
# FONCTIONS                                                                    #
#                                                                              #
# - logDate() : Ajoute la date aux messages de journalisation.                 #
# - createLog() : Créer un fichier de journalisation et lui donne les droits   #
#   corrects s'il n'existe pas.                                                #
# - trim() : Lance la fonction fstrim sur les points de montages listé via une #
#   boucle "for".                                                              #
#                                                                              #
#                                                                              #
# AUTEUR(S)                                                                    #
#                                                                              #
# - Collyer Cédric <collyer.cedric@zergy.net>                                  #
#                                                                              #
#                                                                              #
# DERNIER(S) CHANGEMENT(S) :                                                   #
#                                                                              #
# - 16/05/2020 : Création du script et écriture de la documentation.           #
#                                                                              #
################################################################################
 
# Variables
PATH="/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin";
 
. "/usr/local/etc/trim.conf";
 
# Routine d'horodatage des logs
logDate() {
        log_file_date="$(date +'%d/%m/%Y %H:%M')";
}

# Mise en place des journaux.
createLog() {
        if [ ! -f "${log_file}" ]; then
                touch "${log_file}";
                chown root "${log_file}";
                chgrp adm "${log_file}";
                chmod 640 "${log_file}";
        fi
}

# Triming
trim() {
        logDate;
        echo "*** ${log_file_date} ***" >> "${log_file}"
        for partition in ${partitions}; do
                fstrim -v ${partition} >> "${log_file}";
        done
}

main() {
        createLog;
        trim;
}
 
main;
 
exit 0;
Fichier cron :
  • Type : BASH
  • Droits : root:root, 0755
  • Emplacement par défaut : /etc/cron.weekly/trim

Code : Tout sélectionner

#!/bin/sh

test -x /usr/local/sbin/trim || exit 0
/usr/local/sbin/trim
Modifié en dernier par Zergy le mer. 10 août 2022 17:24, modifié 2 fois.
Me trouver ailleurs
Portable : MSI Modern 15 A5M-216XFR, AMD Ryzen 5 5500U, 64Gio, iGPU AMD Radeon 2Gio, LinuxMint 21.3, Cinnamon
Tour : MSI MAG B550M Mortar WiFi, AMD Ryzen 5 5600X, 64Gio, AMD Radeon RX 6700XT 12Gio, LinuxMint 21.3, Cinnamon
HTPC Parents : GB-BXBT-1900, AMD Ryzen 5 5500U, 32Gio, iGPU AMD Radeon 2Gio, LinuxMint 21.3, Cinnamon
Tour Parents : Asus P8P67 PRO, Intel i7 3770k, 32Gio, Nvidia GTX 960 4Gio, LinuxMint 21.3, Cinnamon

Avatar du membre
philosophedesetoiles
Messages : 267
Enregistré le : jeu. 31 août 2017 12:26

Re: Quelques scripts personnels

Message par philosophedesetoiles »

Merci ;)
"De chacun selon ses forces, à chacun selon ses besoins."
Thinkpad L570
8Go Ram 500Go HDD
MXlinux 21.3 “Wildflower”
XFCE4
--
Thinkpad t520
4Go Ram 250Go HDD
Mint 20-3 Mate

Répondre