Other articles


  1. Bash: Imploser une liste

    list_implode () {
        SEPARATOR="$1"; shift
        LIST=( "$@" )
        RESULT="$(printf "${SEPARATOR}%s" "${LIST[@]}")"
        RESULT="${RESULT:${#SEPARATOR}}"
        echo "$RESULT"
    }
    
    LIST=( "foo" "bar" "with space" "fiz" "buz" )
    
    echo "$(list_implode ", " "${LIST[@]}")"
    

    Sortie :

    foo, bar, foo bar, fiz, buz
    
    read more
  2. Bash: Trouver si un élément est dans une liste

    function in_list {
        SEARCH="$1"; shift
        LIST=( "$@" )
        if [ -z "${LIST}" ] || [ -z "${SEARCH}" ]; then
          return 1
        fi
        for ITEM in ${LIST[@]}; do
            if [[ "${ITEM}" == "${SEARCH}" ]]; then
                return 0
            fi
        done
        return 1
    }
    
    LIST=( "foo" "bar" "with space" "fiz" "buz" )
    
    if (in_list "bar" "${LIST[@]}"); then
        echo "bar is in MY_LIST"
    fi
    

    Sortie :

    bar ...
    read more

blogroll

social