Ceci est une ancienne révision du document !
Bash
Array
- déclaration :
array=( 1 2 3 )
- ajouter un élément :
array+=( 4 )
- lister tous les éléments :
echo “${array[@]}”
- récupérer le nombre d'élement :
echo ${#array[@]}
Fonctions utiles
in_array
function in_array() { local needle=$1 el shift for el in "$@"; do [ "$el" = "$needle" ] && return 0 done return 1 }
Utilisation :
array=(1 2 3) in_array 1 ${array[@]} && echo IN in_array 5 ${array[@]} && echo OUT
is_empty
function is_empty() { [ $# -gt 0 ] && return 1 return 0 }
Utilisation :
array=(1 2 3) is_empty $array && echo empty ! is_empty $array && echo not empty
implode
function implode() { local d=${1-} f=${2-} if shift 2; then printf %s "$f" "${@/#/$d}" fi }
Utilisation :
array=(1 2 3) echo $( implode "," "${array[@]}" ) # Output: 1,2,3 echo -e "- $( implode "\n- " "${array[@]}" )" # Output: # - 1 # - 2 # - 3
format_duration
function format_duration { local T=$1 local D=$((T/60/60/24)) local H=$((T/60/60%24)) local M=$((T/60%60)) local S=$((T%60)) (( $D > 0 )) && printf '%d days and ' $D printf '%02d:%02d:%02d' $H $M $S }
dump_ass_array
function dump_ass_array() { declare -n aarr="$1" echo "\"$1\" = {" for key in "${!aarr[@]}"; do printf ' "%s" => "%s"\n' "$key" "${aarr[$key]}" done echo "}" }
Utilisation :
declare -A myarray myarray[a]="b" myarray[c]="d" dump_ass_array myarray
var_dump
declare -p variableName
Gestion des paramètres
#!/bin/bash DEBUG=0 BIN_PATH="/bin/binary" EXTRA_ARGS=() function usage() { error="$1" [ -n "$error" ] && echo "$error" cat << EOF Usage : $(basename $0) [-d] [-b /path/to/binary] -b [path] Binary path (default: $BIN_PATH) -d Debug mode -X Enable bash tracing (=set -x) -h Show this message EOF [ -n "$error" ] && exit 1 exit 0 } function debug() { [ $DEBUG -eq 1 ] && >&2 echo -e "$( date '+%Y-%m-%d %H:%M:%S' ) - $@" } idx=1 while [ $idx -le $# ] do OPT=${!idx} case $OPT in -d) DEBUG=1 ;; -h) usage ;; -b) ((idx++)) BIN_PATH=${!idx} if [ ! -x "$BIN_PATH" ] then usage "Invalid binary path ($BIN_PATH)" fi ;; -X) set -x ;; *) EXTRA_ARGS+=( $OPT ) ;; esac ((idx++)) done debug "Extra args: ${EXTRA_ARGS[@]}"
Barre de progression
declare -A PBARS declare PBID function pbar_create() { [ -n "$3" ] && PBAR_ID_VAR="$3" || PBAR_ID_VAR=PBID declare -n ID="$PBAR_ID_VAR" ID=$( tr -dc A-Za-z0-9 </dev/urandom | head -c 3 ) PBARS["${ID}_START_TIME"]="$( date +%s )" PBARS["${ID}_TITLE"]="$1" PBARS["${ID}_TOTAL"]="$2" PBARS["${ID}_CURRENT"]=0 PBARS["${ID}_LAST_UPDATE"]=0 pbar_draw $ID } function pbar_finish() { [ -n "$1" ] && ID=$1 || ID=$PBID unset 'PBARS[${ID}_START_TIME]' unset 'PBARS[${ID}_TITLE]' unset 'PBARS[${ID}_TOTAL]' unset 'PBARS[${ID}_CURRENT]' unset 'PBARS[${ID}_LAST_UPDATE]' echo } function pbar_draw() { [ -n "$1" ] && ID=$1 || ID=$PBID let PERC=${PBARS[${ID}_CURRENT]}*100/${PBARS[${ID}_TOTAL]} NOW=$(date +%s) [ $NOW -eq ${PBARS[${ID}_LAST_UPDATE]} ] && return let DURATION=NOW-${PBARS[${ID}_START_TIME]} if [ ${PBARS[${ID}_CURRENT]} -gt 0 ] then let TOTAL_DURATION=DURATION*${PBARS[${ID}_TOTAL]}/${PBARS[${ID}_CURRENT]} SPEED=$( echo "scale=1; ${PBARS[${ID}_CURRENT]}/$DURATION"|bc ) else TOTAL_DURATION=0 SPEED="?" fi let ETA=TOTAL_DURATION-DURATION let DONE=$PERC*4/10 let LEFT=40-$DONE DONE=$(printf "%${DONE}s"|tr ' ' '#') LEFT=$(printf "%${LEFT}s"|tr ' ' '-') printf "\r%s: [%s%s] - %d/%d - %d%% - ETA: %s (%s / %s, %s/s)" \ "${PBARS[${ID}_TITLE]}" \ "${DONE}" "${LEFT}" \ ${PBARS[${ID}_CURRENT]} ${PBARS[${ID}_TOTAL]} \ $PERC \ "$(format_duration $ETA)" \ "$(format_duration $DURATION)" \ "$(format_duration $TOTAL_DURATION)" \ $SPEED PBARS[${ID}_LAST_UPDATE]=$NOW } function pbar_increment() { [ -n "$1" ] && ID=$1 || ID=$PBID ((PBARS[${ID}_CURRENT]++)) pbar_draw $ID }
Utilisation :
pbar_create "Test" 20 for i in $( seq 1 20 ) do pbar_increment sleep 0.1 done pbar_finish
La fonction format_duration est nécessaire.