BASH Script to Configure Monitors on Linux via xRandR
for a problem like this, it helps to break down the possible states and map out what you want to do for each, and then construct a data model that lets you minimize duplication.

Here is my approach to your problem: 


hardcoded array of monitors by order of preference, allowing wildcards
hardcoded assoc. array of screen resolutions for each preference
hardcoded assoc. array of options templates for each possible configuration
construct array of actual available monitor names from (1) and xrandr output
construct array of resolutions from (2) and (4)
get options template from (3) and (4)
fill template using (4) and (5)


__

#!/usr/bin/env bash

# each PRIORITY entry must have matching entry in MODE; $displays will be sorted in priority order
declare -ar PRIORITY=( "DP-1*" HDMI-2 eDP-1 )
declare -Ar MODE=(
        [eDP-1]=1920x1080
        [DP-1*]=2560x1440
        [HDMI-2]=1680x1050
    )

# options corresponding to each possible config.  sorted in PRIORITY order.  
# left hand side is matched against space-separated list of actual monitor labels from xrandr
# template values like &lt;D2&gt; are zero-based
declare -Ar OPTS=(
            [DP-1* HDMI-2 eDP-1]='--output &lt;D2&gt; --off --output &lt;D0&gt; --mode &lt;M0&gt; --pos 1680x0 --right-of &lt;D2&gt; --output &lt;D1&gt; --mode &lt;M1&gt; --pos 0x0 --left-of &lt;D0&gt;'
            [HDMI-2 eDP-1]='--output &lt;D1&gt; --off --output &lt;D0&gt; --mode &lt;M0&gt; --pos 0x0'
            [eDP-1*]='--output &lt;D0&gt; --primary --mode &lt;M0&gt; --rotate normal --pos 0x0'
        )

declare -ar ALL_CONNECTED=( $( { xrandr -q || exit 1; } | awk '$2 == "connected" {print $1}' ) )


[[ ${#ALL_CONNECTED[@]} = 0 ]] &amp;&amp; {
    echo no monitors connected
    exit 1
}

declare -a displays=()
declare -a modes=()

# populate displays and modes in preference order from ALL_CONNECTED 
for (( i=0; i&lt;${#PRIORITY[@]}; i++ )); do
    for (( j=0; j&lt;${#ALL_CONNECTED[@]}; j++ )); do
        if [[ ${ALL_CONNECTED[$j]} == ${PRIORITY[$i]} ]]; then
            displays+=( ${ALL_CONNECTED[$j]} )
            modes+=( ${MODE[${PRIORITY[$i]}]} )
            break
        fi
    done
done

echo "
ALL_CONNECTED: ${ALL_CONNECTED[@]}
displays: ${displays[@]}
modes: ${modes[@]}
"

for i in "${!OPTS[@]}"; do
    if [[ "${displays[@]}" == $i ]]; then
        opts=${OPTS[$i]}
        opts=${opts//&lt;M/\$\{modes\[}            
    opts=${opts//&lt;D/\$\{displays\[}
        opts=${opts//&gt;/\]\}}
        set -x
        xrandr $( eval echo $opts )
        exit $?
    fi
done
echo "no OPT setting found for connected display combination of ${ALL_CONNECTED[@]} [ ${displays[@]} ]"
exit 1