1

I have something like this:

while [[ $# > 0 ]] ; do
    key="$1"
    count=0
    echo "$1"
    case "$key" in
        -r|--rotate)
            shift
            rotate $1
            shift
            ;;
        -d|--devices)       
            shift
            while [[ "$1" != "-"* && "$1" != "" ]] ; do
                disps["$count"]="$1"
                ((count++))
                shift
            done
            calibrate disps[@]
            ;;
        -h|--help)
            shift
            usage
            ;;
        *)
            shift
            usage
            ;;
    esac
done

I want to make rotation states in options as they are only normal, right left and invert.

I know I need to create a case for each of them -n -r -l -i but... I will need to create a case for each combination too? -nd|-dn, -rd|-dr... Is there a simpler or polite way?

Thanks.

1

use getopt to enable long and short options, as well as -rdn option concatenation

parsed_options=$( getopt -o r:d:h --long rotate:,device:,help --name "$(basename -- "$0")" -- "$@" )
# option name followed by a single colon indicates the option takes
# a required argument

if [ $! -ne 0 ]; then
    echo "Exiting" >&2
    exit 1
fi

eval set -- "$parsed_options"

disps=()

while :; do
    case "$1" in
        -r|--rotate)
            rotate "$2"
            shift 2
            ;;
        -d|--device)       
            disps+=("$2")   # append the arg to the disps array
            shift 2
            ;;
        -h|--help) usage; exit ;;
        --) shift; break ;;
        *) echo "Internal error" >&2; exit 1 ;;
    esac
done

if (( ${#disps[@]} == 0 )); then
    echo "no devices to calibrate"
else
    calibrate "${disps[@]}"
fi

An option can only take zero or one argument. To specify many devices, use the -d arg option many times.

  • I will still use my "dirty solution", I want a simple solution where someone can do script -di HDMI-0 DVI-1 what will calibrate the device to cover both displays and invert the axis, where 'i' can be changed to 'r' right, 'l' left or 'n' normal. But I liked to know the alternative anyway. – RomuloPBenedetti Jul 23 '15 at 18:22
  • Alternately, give a single, comma-separated string as the argument to -d, and then you can split it into an array. – glenn jackman Jul 24 '15 at 1:45
  • this is true, your solution at all is a valid solution and as there appear to not exist a better answer I will mark it as the answer, thanks. – RomuloPBenedetti Jul 24 '15 at 19:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.