Choose or automatic continue

From WickyWiki
Revision as of 17:15, 21 March 2019 by Wilbert (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)


# usage: choose_or_wait "question" "default" "secs-to-wait"
function choose_or_wait {
  question=$1
  default=$2
  secs=$(($3))
  while [ $secs -gt 0 ]; do
    echo -ne "\r$question [$default]? $secs\033[0K"
    if read -s -n1 -t1 choice; then
      break
    fi
    let secs--
  done
  if [ -z "${choice}" ] ; then
    choice=$default
  fi
  echo -e "\r$question [$default]? $choice  "
}

choose_or_wait "Just do it" "n" "10"