Outils pour utilisateurs

Outils du site


tech:script_bash_fonctions_envrac

Différences

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

Lien vers cette vue comparative

Les deux révisions précédentesRévision précédente
tech:script_bash_fonctions_envrac [2026/05/29 14:51] Jean-Baptistetech:script_bash_fonctions_envrac [2026/06/30 21:52] (Version actuelle) Jean-Baptiste
Ligne 1: Ligne 1:
 +<!DOCTYPE markdown>
 +{{tag>Brouillon Bash Script}}
 +
 +# Script bash fonctions en vrac
 +
 +Voir [[notes_hexa|Fonctions conversion Hexa ASCII]]
 +
 +Fonctions récupérées de http://www.trapkit.de/tools/checksec.sh
 +
 +~~~bash
 +#! /bin/bash
 + 
 +set -euo pipefail
 +IFS=$' \t\n'
 +export LC_ALL=C
 +
 +SCRIPT_NAME=$(basename "$0")
 +readonly SCRIPT_NAME
 +SCRIPT_DIR=$(readlink -m "$(dirname "$0")")
 +readonly SCRIPT_DIR
 +
 +# Launch this script as root
 +if [ $EUID != 0 ]; then
 +    sudo "$0" "$@"
 +    exit $?
 +fi
 +
 +[ -r /etc/default/plop ] && . /etc/default/plop
 +
 +# check if command exists
 +command_exists() { type $1  > /dev/null 2>&1; }
 +
 +command_exists() {
 + command -v "$1" >/dev/null 2>&1 || (
 + echo "I require $1 but it's not installed. Aborting." >&2
 + exit 127
 + )
 +}
 +
 +for COMMAND in "nova" "glance" "dmidecode" "tr"
 +do
 +  command_exists "${COMMAND}"
 +done
 +
 +
 +exit_si_erreur(){
 +    if [ $1 != 0 ]
 +    then
 +        echo "Sortie en erreur."
 +        exit 1
 +    fi
 +}
 +
 +# plop ; exit_si_erreur $?
 +
 +get_mountpoint() { df -l --output=target "$1" |sed -e "1d" ;}
 +
 +
 +# check if directory is empty
 +dir_is_empty() {
 +  if [ -z "$(ls -A $1)" ]
 +  then
 +    return 0
 +  else
 +    return 1
 +  fi
 +}
 +
 +isNonEmptyStr() { echo "$@" | grep -q -v -e "^$" ;}
 +
 +is_empty_var() {
 +        local var
 +        var=$1
 +        if [[ "${!var:-}" == "" ]]
 +}
 +
 +is_valid_value_var() {
 +  # Syntax exemple :
 +  # is_valid_value_var BOOL y n && echo "VAR 'BOOL' is 'y' or 'n'"
 +  # is_valid_value_var NUM 1 2 3 || echo "VAR 'NUM' is not 1, 2, or 3"
 +  local var=$1
 +  shift
 +  for value in "$@"
 +  do
 +      if [ "${!var-}" == "$value" ]
 +      then
 +          return 0 # TRUE
 +      fi
 +  done
 +  return 1 # FALSE
 +}
 +
 +
 +
 +
 +isDevMounted() { findmnt --source "$1" >/dev/null;} #device only
 +isPathMounted() { findmnt --target "$1" >/dev/null;} #path   only
 +isMounted() { findmnt          "$1" >/dev/null;} #device or path
 +is_mount() { findmnt -M "$1" >/dev/null ;}
 +# Voir aussi la commande "mountpoint"
 +
 +# check user privileges
 +root_privs() {
 +  if [ "$(/usr/bin/id -u)" -eq 0 ]
 +  then
 +    return 0
 +  else
 +    return 1
 +  fi
 +}
 +
 +# Require that this runs as root.
 +[ "$UID" -eq 0 ] || exec sudo "$0" "$@"
 +
 +# check if input is numeric
 +isNumeric() { echo "$@" | grep -q -v -e "[^0-9]" ; }
 +
 +# check if input is a string
 +isString() { echo "$@" | grep -q -v -e "[^A-Za-z]" ; }
 +
 +# check if file exist
 +file_exist() {
 +  if [ $# -ne 1 ]
 +  then
 +      echo "illegal number of parameters"
 +      exit 2
 +  fi
 +  [ -e "$1" ]
 +}
 +
 +#
 +return_only_files_exists() { ls -1 "$@" 2>/dev/null ;}
 +
 +
 +if !(isString "$2")
 +then
 +    printf "\033[31mError: Please provide a valid process name.\033[m\n\n"
 +    exit 1
 +fi
 +
 +if ! command -v zgrep &> /dev/null
 +then
 +        zgrep() {
 +                zcat "$2" | grep "$1"
 +        }
 +fi
 +
 +check_device() {
 + if [ -c "$1" ]
 +        then
 + wrap_good "$1" 'present'
 + else
 + wrap_bad "$1" 'missing'
 + EXITCODE=1
 + fi
 +}
 +
 +strip_comment() { grep -Ev -e '^\s+#|^#' - ;}
 +
 +abs() {
 +        local -i VAL=$1
 +        echo "${VAL#-}"
 +}
 +
 +
 +calc_sum() { awk '{s+=$1} END {printf "%.0f", s}' "$*" ;}
 +
 +
 +mk_calc() { awk "BEGIN { print $* }"; }
 +# ex : mk_calc 7.5/3.2 => 2.34375
 +# Voir aussi expr : CACHE_USED=$( expr \( $CACHE_USED \* 1024 \) )
 +
 +# A helper to make sure that Chrome is linked correctly
 +function installation_status() {
 +    google-chrome-stable --version > /dev/null 2>&1
 +    [ $? -eq 0 ]
 +}
 +
 +
 +############ Source : https://github.com/danguita/scripts/blob/master/kickstart/kickstart-debian.sh
 +
 +say() {
 +  printf "\n[$(date --iso-8601=seconds)] %s\n" "$1"
 +}
 +
 +# shellcheck disable=SC2317
 +confirm() {
 +  while true; do
 +    read -r -p "$1 (y/[n]): " answer
 +    case $answer in
 +      [Yy]* ) return 0; break;;
 +      [Nn]* ) return 1; break;;
 +      "" ) return 1; break;;
 +      * ) echo "Please answer yes or no.";;
 +    esac
 +  done
 +}
 +
 +install_package() {
 +  sudo apt install --no-install-recommends -y "$@"
 +}
 +
 +#########
 +
 +quote () {
 +    local quoted=${1//\'/\'\\\'\'};
 +    printf "'%s'" "$quoted"
 +}
 +
 +
 +dequote () {
 +    eval printf %s "$1" 2> /dev/null
 +}
 +~~~
 +
 +
 +
 +
 +`get_dev.sh`
 +~~~bash
 +#! /bin/bash
 +
 +# Respond to the question : if a new file is created, this will be on which block device ?
 +# Input arg 1 : path (file)
 +# Outpout     : path (block device)
 +
 +set -euo pipefail
 +IFS=$' \t\n'
 +export LC_ALL=C
 +
 +NEW_FILE="$1"
 +
 +is_regular_file() {
 +    [[ -f $1 ]]
 +}
 +
 +# check if directory exists
 +is_dir() {
 +    [[ -d $1 ]]
 +}
 +
 +get_parent_dir() {
 +        local CHEMIN="$1"
 +        while ! "$(dir_exists "$CHEMIN")"
 +        do
 +                CHEMIN=$(dirname "$CHEMIN")
 +        done
 +        echo "$CHEMIN"
 +}
 +
 +get_device() { df -lP "$(get_parent_dir "$NEW_FILE")" |awk 'NR==2{print $1}' }
 +
 +get_device "$(get_parent_dir "$NEW_FILE")"
 +~~~
 +
 +A la place de 
 +~~~bash
 +dir_exists /tmp/plop || mkdir -p /tmp/plop
 +~~~
 +
 +Il est préférable de faire d'uuliser la commande `install`
 +~~~bash
 +install -d /tmp/plop
 +~~~
 +
 +Faire une division
 +~~~bash
 +echo $(( 10 / 3 ))
 +echo $(( VAR / 3 ))
 +expr 10 / 3
 +
 +calc() { awk "BEGIN { print ""$*"" }" }
 +
 +~~~
 +
 +~~~bash
 +my_command || { echo 'my_command failed' ; exit 1; }
 +~~~
 +
 +Bash Colors \
 +source : https://github.com/moby/moby/blob/master/contrib/check-config.sh
 +~~~bash
 +color() {
 + local codes=()
 + if [ "$1" = 'bold' ]; then
 + codes=( "${codes[@]}" '1' )
 + shift
 + fi
 + if [ "$#" -gt 0 ]; then
 + local code=
 + case "$1" in
 + # see https://en.wikipedia.org/wiki/ANSI_escape_code#Colors
 + black) code=30 ;;
 + red) code=31 ;;
 + green) code=32 ;;
 + yellow) code=33 ;;
 + blue) code=34 ;;
 + magenta) code=35 ;;
 + cyan) code=36 ;;
 + white) code=37 ;;
 + esac
 + if [ "$code" ]; then
 + codes=( "${codes[@]}" "$code" )
 + fi
 + fi
 + local IFS=';'
 + echo -en '\033['"${codes[*]}"'m'
 +}
 +
 +wrap_color() {
 + text="$1"
 + shift
 + color "$@"
 + echo -n "$text"
 + color reset
 + echo
 +}
 +
 +wrap_good() { echo "$(wrap_color "$1" white): $(wrap_color "$2" green)" }
 +
 +wrap_bad() { echo "$(wrap_color "$1" bold): $(wrap_color "$2" bold red)" ; }
 +
 +wrap_warning() { wrap_color >&2 "$*" red ; }
 +
 +error() { printf "${red}!!! %s${reset}\\n" "${*}" 1>&2 ; }
 +
 +failure() {
 +    local rc=$?
 +    echo -e "\t[FAILED]"
 +    return $rc
 +}
 +
 +success() {
 +    echo -e "\t[  OK  ]"
 +    return 0
 +}
 +
 +# Getpass
 +read -s password
 +read -s -p "Password: "
 +
 +get_free_space() {
 + local chemin=$1
 + local -i free_mb
 + free_mb="$(df --output=avail -m "$chemin" | tail -1 | tr -d ' ')"
 + echo "$free_mb"
 +}
 +
 +check_free_space() {
 + # check_free_space /tmp/ 200
 + # will exit if /tmp/ less than 200MB
 + local chemin=$1
 + local -i min_free_mb=$2
 + if [[ "$(get_free_space "$chemin")" -lt min_free_mb ]]
 + then
 +   echo ERROR: No space left on device >&2
 +   exit 28
 + fi
 +}
 +~~~
 +
 +Appelle d'une fonction bash depuis un autre shell \
 +Même UID pour deux utilisateurs différents
 +~~~bash
 +cat <<EOF | bash -s --
 +same_uid_for_Utilisateur_and_utilisateur()
 +{
 +    local -i RET=0
 +    local -i UID_Utilisateur=$(id -u Utilisateur)
 +    RET=$(( RET + $? ))
 +    local -i UID_utilisateur=$(id -u utilisateur)
 +    RET=$(( RET + $? ))
 +    
 +    if [[ (RET -eq 0) && (UID_Utilisateur -ne UID_utilisateur) ]]
 +    then
 +        echo "usermod -o -u $(id -u utilisateur) -g $(id -g utilisateur) Utilisateur"
 +        chown -R utilisateur:utilisateur /var/opt/plop
 +        usermod -o -u $(id -u utilisateur) -g $(id -g utilisateur) Utilisateur
 +    fi
 +
 +    exit
 +}
 +
 +same_uid_for_Utilisateur_and_utilisateur
 +EOF
 +~~~
 +
 +Variables
 +~~~bash
 +SCRIPTPATH=$(dirname "$(realpath "$0")")
 +
 +~~~
 +
 +
 +http://linuxfr.org/users/srb/journaux/waitend-executer-une-commande-apres-une-autre-deja-lancee
 +
 +Si la commande `waitpid` n'est pas présente (util-linux-extra)
 +
 +~~~bash
 +waitpid()
 +{
 +        while ps -p $1 >/dev/null ; 
 +        do
 +                sleep 5
 +        done
 +        shift
 +        "$@"
 +}
 +~~~
 +
 +Strip char / trim => utiliser `xargs` ex :
 +~~~bash
 +load5=$(echo $UPTIME | awk -F, '{print $5}' | xargs)
 +~~~
 +
 +
 +## Gestion des logs
 +
 +~~~bash
 +#!/bin/bash
 +NAME=plop
 +LOG=/var/log/$NAME/$NAME.log
 + 
 +exec >>$LOG 2>&1
 +exec &> >(tee -i /var/log/stackscript.log)
 +
 +~~~
 +
 +
 +## Gestion des erreurs
 +
 +~~~bash
 +#! /bin/bash
 +
 +# to combine `set -e` (same as: `set -o errexit`) with an ERR trap
 +set -eE  # same as: `set -o errexit -o errtrace`
 +
 +err_report()
 +{
 +    echo "Error on line $1"
 +}
 +
 +trap 'err_report $LINENO' ERR
 +
 +echo hello | grep foo  # This is line number 9
 +~~~
 +
 +Source : https://unix.stackexchange.com/questions/39623/trap-err-and-echoing-the-error-line
 +
 +
 +## Exécution à la sortie
 +
 +~~~bash
 +trap 'rm -f "$TMP_INV_INI"' EXIT ERR
 +~~~
 +
 +ou encore
 +~~~bash
 +trap '[ "$?" -eq 61 ] || exit 61' 0
 +~~~
 +
 +
 +## No sigterm terminate
 +
 +~~~bash
 +#!/bin/bash
 +trap '' SIGTERM
 +
 +~~~
 +
 +
 +## Utilisation d'aliases dans un script shell
 +
 +~~~bash
 +shopt -s expand_aliases
 +source ~/.bash_aliases
 +~~~
 +
 +
 +
 +## Autres
 +
 +How to make sure only one instance of a bash script runs ?
 +~~~bash
 +if [ $(pidof -x $0| wc -w) -gt 2 ]; then
 +    echo "More than 1"
 +    exit
 +fi
 +~~~
 +
 +
 +Note : [[https://fr.wikipedia.org/wiki/NOP|NOP]] en bash :
 +~~~bash
 +true
 +~~~
 +
 +
 +~~~bash
 +# We use this to print to the terminal when the context
 +# of STDOUT is something other than the pty.
 +declare -r TTY="$(tty)"
 +
 +printf $'%s %s\n' "${method}" "${path}" >${TTY}
 +printf $'HTTP/1.1 %s %s\r\n' "${status:?}" "${msg:?}" | tee ${TTY}
 +~~~
 +
 +
 +## Bash variable Substitution
 +
 +Supprimer une extention de fichier 
 +~~~
 +$ VAR="plop.txt"
 +$ echo ${VAR%.txt}
 +plop
 +~~~
 +
 +~~~
 +$ var=sd_feeef_efe_g
 +$ echo "${var#?*_}"
 +feeef_efe_g
 +~~~
 +
 +~~~
 +$ var=toto-tt-ll
 +$ echo "${var//-/_}"
 +toto_tt_ll
 +~~~
 +
 +~~~
 +$ echo "${a/-/_}"
 +toto_tt-ll
 +~~~
 +
 +
 +Case upper lower majuscule minuscule
 +~~~bash
 +x="Hello"
 +echo $x  # HELLO
 +
 +y=${x,,}
 +echo $y  # hello
 +
 +z=${y^^}
 +echo $z  # HELLO
 +~~~
 +
 +Dernier caractère d'une chaine
 +~~~bash
 +echo "${str: -1}"
 +
 +# ou
 +
 +echo ${str:0-1}
 +~~~
 +
 +Renommer en supprimant un prefix 
 +~~~bash
 +for fic in *.j2 ; do mv "$fic" "${fic#PREFIX_}" ; done
 +~~~
 +
 +-----------
 +
 +~~~bash
 +#!/bin/bash
 +SCRIPT_PATH="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
 +
 +${SCRIPT_PATH}/sds_run.sh portal
 +~~~
 +
 +-----
 +
 +~~~bash
 +negate() {
 +    if [[ $# -eq 0 ]]; then
 +        echo "ERROR. ENOSYS Function not implemented" >&2
 +        return 38
 +    else
 +        # ! "${@}"
 +        "${@}" && return 1 || return 0
 +    fi
 +}
 +~~~
 +
 +~~~
 +$ negate true ; echo $?
 +1
 +
 +$ negate false ; echo $?
 +0
 +
 +$ true | negate ; echo $?
 +ERROR. ENOSYS Function not implemented
 +38
 +
 +$ false | negate ; echo $?
 +ERROR. ENOSYS Function not implemented
 +38
 +~~~
 +
 +Voir aussi :
 +* PIPESTATUS
 +* pipefail
 +
  

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki