Choose or automatic continue: Difference between revisions

From WickyWiki
Created page with "Category:Ubuntu Category:Bash Category:2019023 <syntaxhighlight lang=bash> # usage: choose_or_continue "question-without-questionmark" "default-answer" "secs-to-w..."
 
mNo edit summary
 
Line 4: Line 4:


<syntaxhighlight lang=bash>
<syntaxhighlight lang=bash>
# usage: choose_or_continue "question-without-questionmark" "default-answer" "secs-to-wait"
# usage: choose_or_wait "question" "default" "secs-to-wait"
function choose_or_continue {
function choose_or_wait {
   question=$1
   question=$1
   default=$2
   default=$2
Line 11: Line 11:
   while [ $secs -gt 0 ]; do
   while [ $secs -gt 0 ]; do
     echo -ne "\r$question [$default]? $secs\033[0K"
     echo -ne "\r$question [$default]? $secs\033[0K"
     if read -s -n1 -t1 decide; then
     if read -s -n1 -t1 choice; then
       break
       break
     fi
     fi
     let secs--
     let secs--
   done
   done
   if [ -z "${decide}" ] ; then
   if [ -z "${choice}" ] ; then
     decide=$default
     choice=$default
   fi
   fi
   echo -e "\r$question [$default]? $decide "
   echo -e "\r$question [$default]? $choice "
}
}


choose_or_continue "Just do it" "n" "10"
choose_or_wait "Just do it" "n" "10"
</syntaxhighlight>
</syntaxhighlight>

Latest revision as of 17:15, 21 March 2019


# 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"