How to add interactive questions to bash scripts (#tilPost)

I was chatting to my colleague Jeff about dotfiles today and he showed me something really interesting. He uses some handy shell functions in his terminal to secure critical but repetitive git tasks.
pushToPreview() {
CURRENTBRANC…


This content originally appeared on Stefan Judis Web Development and was authored by Stefan Judis

I was chatting to my colleague Jeff about dotfiles today and he showed me something really interesting. He uses some handy shell functions in his terminal to secure critical but repetitive git tasks.

pushToPreview() {
  CURRENTBRANCHNAME="$(git rev-parse --abbrev-ref HEAD)"
  echo "Working branch: $CURRENTBRANCHNAME"

  echo "Force push $CURRENTBRANCHNAME to preview?"
  select yn in "Yes" "No"; do
    case $yn in
      Yes ) echo "Updating preview from $CURRENTBRANCHNAME" && git push -f origin $CURRENTBRANCHNAME:preview; break;;
      No ) break;;
    esac
  done
}

I don't want to get too much into details what this snippet above does but it includes a yes/no prompt that stops the shell script and waits for user input?!

If the user enters 1 a git push -f is performed. ?

Interactive prompt in bash

It turns out the select statement makes this possible. I digged a bit and found this clear definition of what select does.

- generates a menu of each item in list, formatted with numbers for each choice
- prompts the user for a number
- stores the selected choice in the variable name and the selected number in the built-in variable REPLY
- executes the statements in the body
- repeats the process forever (but see below for how to exit)

This is really nice because with select I can customize commands like a rm -rf or the mentioned git push -f easily and safe myself some future headaches without the need for an additional library or something similar! ?


Reply to Stefan


This content originally appeared on Stefan Judis Web Development and was authored by Stefan Judis


Print Share Comment Cite Upload Translate
APA
Stefan Judis | Sciencx (2022-12-08T15:35:50+00:00) » How to add interactive questions to bash scripts (#tilPost). Retrieved from https://www.scien.cx/2018/02/20/how-to-add-interactive-questions-to-bash-scripts-tilpost/.
MLA
" » How to add interactive questions to bash scripts (#tilPost)." Stefan Judis | Sciencx - Tuesday February 20, 2018, https://www.scien.cx/2018/02/20/how-to-add-interactive-questions-to-bash-scripts-tilpost/
HARVARD
Stefan Judis | Sciencx Tuesday February 20, 2018 » How to add interactive questions to bash scripts (#tilPost)., viewed 2022-12-08T15:35:50+00:00,<https://www.scien.cx/2018/02/20/how-to-add-interactive-questions-to-bash-scripts-tilpost/>
VANCOUVER
Stefan Judis | Sciencx - » How to add interactive questions to bash scripts (#tilPost). [Internet]. [Accessed 2022-12-08T15:35:50+00:00]. Available from: https://www.scien.cx/2018/02/20/how-to-add-interactive-questions-to-bash-scripts-tilpost/
CHICAGO
" » How to add interactive questions to bash scripts (#tilPost)." Stefan Judis | Sciencx - Accessed 2022-12-08T15:35:50+00:00. https://www.scien.cx/2018/02/20/how-to-add-interactive-questions-to-bash-scripts-tilpost/
IEEE
" » How to add interactive questions to bash scripts (#tilPost)." Stefan Judis | Sciencx [Online]. Available: https://www.scien.cx/2018/02/20/how-to-add-interactive-questions-to-bash-scripts-tilpost/. [Accessed: 2022-12-08T15:35:50+00:00]
rf:citation
» How to add interactive questions to bash scripts (#tilPost) | Stefan Judis | Sciencx | https://www.scien.cx/2018/02/20/how-to-add-interactive-questions-to-bash-scripts-tilpost/ | 2022-12-08T15:35:50+00:00
https://github.com/addpipe/simple-recorderjs-demo