informatique:outils:bash

Ceci est une ancienne révision du document !


Bash

function in_array() {
    local needle=$1 el
    shift
    for el in "$@"; do
        if [ "$el" = "$needle" ]; then
            return 0
        fi
    done
    return 1
}
array=(1 2 3)
in_array 1 ${array[@]} && echo IN
in_array 5 ${array[@]} && echo OUT
function implode() {
  local d=${1-} f=${2-}
  if shift 2; then
    printf %s "$f" "${@/#/$d}"
  fi
}
array=(1 2 3)
echo $( implode "," "${array[@]}" )
# Output: 1,2,3
echo -e "- $( implode "\n- " "${array[@]}" )"
# Output:
# - 1
# - 2
# - 3
  • informatique/outils/bash.1710939011.txt.gz
  • Dernière modification : 2024/03/20 12:50
  • de bn8