#!/bin/bash # # $Id$ # # choice: Display an X dialogue to allow a choice. # Usage: choice [-t ] : [: ...] # Example: choice "File exists" "Replace:1" "Back up:2" "No action:0" # function usage() { echo >&2 "Usage: choice [-t ] : [: ...]" exit 100 } while getopts ":t:" arg; do case $arg in t) timeout="$OPTARG";; *) usage;; esac done shift $((OPTIND-1)) description="$1"; shift [ -z "$description" ] && usage for i in ${1+"$@"}; do [ "${i/:/}" = "$i" ] && usage done args= if which zenity 2>/dev/null | grep ^/ &>/dev/null; then [ ! -z "$timeout" ] && timeout="--timeout=$timeout" for i in ${1+"$@"}; do args="$args \"${i/:/\" }" done ret=$(eval zenity --list --text="\"$description\"" --column=Choice --column=hidden --hide-column=2 --print-column=2 $args $timeout) exit $ret else [ ! -z "$timeout" ] && timeout="-timeout $timeout" for i in ${1+"$@"}; do args="$args,\"$i\"" done args=${args/,/} eval exec xmessage -center $timeout -buttons $args "$description" fi