do-nothing-script-example.bash
· 1.7 KiB · Bash
Raw
#!/bin/bash
function printDivider() {
local columns
columns=$(tput cols)
printf '─%.0s' $(seq 1 "$columns")
echo ''
}
# Global state
user_name=""
greeting=""
function step1() {
echo "Step 1: Ask for your name"
read -rp "Enter your name: " user_name
}
function step2() {
echo "Step 2: Customize greeting"
if [[ -z "$user_name" ]]; then
echo "Name not set. Go back to Step 1."
else
read -rp "Enter a greeting for $user_name: " greeting
fi
}
function step3() {
echo "Step 3: Display result"
if [[ -n "$user_name" && -n "$greeting" ]]; then
echo "$greeting, $user_name!"
else
echo "Incomplete information. Go back and complete previous steps."
fi
}
steps=("step1" "step2" "step3")
current_step=0
function showMenu() {
echo
echo -e "[\e[1mn\e[0m] Next [\e[1mp\e[0m] Previous [\e[1mq\e[0m] Quit"
echo -n "Choice: [N/p/q] "
IFS= read -r -n1 choice
echo
[[ -z "$choice" ]] && choice="n"
}
function runSteps() {
while true; do
clear
echo -e "Running \e[1m${steps[$current_step]}\e[0m (Step $((current_step + 1)) of ${#steps[@]})"
printDivider
"${steps[$current_step]}"
printDivider
showMenu
case "$choice" in
n|N)
if (( current_step < ${#steps[@]} - 1 )); then
((current_step++))
else
echo "Already at the last step."; read -rp "Press enter to continue..."
fi
;;
p|P)
if (( current_step > 0 )); then
((current_step--))
else
echo "Already at the first step."; read -rp "Press enter to continue..."
fi
;;
q|Q)
echo "Exiting."
break
;;
*)
echo "Invalid input."; read -rp "Press enter to continue..."
;;
esac
done
}
runSteps
| 1 | #!/bin/bash |
| 2 | |
| 3 | function printDivider() { |
| 4 | local columns |
| 5 | columns=$(tput cols) |
| 6 | printf '─%.0s' $(seq 1 "$columns") |
| 7 | echo '' |
| 8 | } |
| 9 | |
| 10 | # Global state |
| 11 | user_name="" |
| 12 | greeting="" |
| 13 | |
| 14 | function step1() { |
| 15 | echo "Step 1: Ask for your name" |
| 16 | read -rp "Enter your name: " user_name |
| 17 | } |
| 18 | |
| 19 | function step2() { |
| 20 | echo "Step 2: Customize greeting" |
| 21 | if [[ -z "$user_name" ]]; then |
| 22 | echo "Name not set. Go back to Step 1." |
| 23 | else |
| 24 | read -rp "Enter a greeting for $user_name: " greeting |
| 25 | fi |
| 26 | } |
| 27 | |
| 28 | function step3() { |
| 29 | echo "Step 3: Display result" |
| 30 | if [[ -n "$user_name" && -n "$greeting" ]]; then |
| 31 | echo "$greeting, $user_name!" |
| 32 | else |
| 33 | echo "Incomplete information. Go back and complete previous steps." |
| 34 | fi |
| 35 | } |
| 36 | |
| 37 | steps=("step1" "step2" "step3") |
| 38 | current_step=0 |
| 39 | |
| 40 | function showMenu() { |
| 41 | echo |
| 42 | echo -e "[\e[1mn\e[0m] Next [\e[1mp\e[0m] Previous [\e[1mq\e[0m] Quit" |
| 43 | echo -n "Choice: [N/p/q] " |
| 44 | IFS= read -r -n1 choice |
| 45 | echo |
| 46 | [[ -z "$choice" ]] && choice="n" |
| 47 | } |
| 48 | |
| 49 | function runSteps() { |
| 50 | while true; do |
| 51 | clear |
| 52 | echo -e "Running \e[1m${steps[$current_step]}\e[0m (Step $((current_step + 1)) of ${#steps[@]})" |
| 53 | printDivider |
| 54 | "${steps[$current_step]}" |
| 55 | printDivider |
| 56 | showMenu |
| 57 | case "$choice" in |
| 58 | n|N) |
| 59 | if (( current_step < ${#steps[@]} - 1 )); then |
| 60 | ((current_step++)) |
| 61 | else |
| 62 | echo "Already at the last step."; read -rp "Press enter to continue..." |
| 63 | fi |
| 64 | ;; |
| 65 | p|P) |
| 66 | if (( current_step > 0 )); then |
| 67 | ((current_step--)) |
| 68 | else |
| 69 | echo "Already at the first step."; read -rp "Press enter to continue..." |
| 70 | fi |
| 71 | ;; |
| 72 | q|Q) |
| 73 | echo "Exiting." |
| 74 | break |
| 75 | ;; |
| 76 | *) |
| 77 | echo "Invalid input."; read -rp "Press enter to continue..." |
| 78 | ;; |
| 79 | esac |
| 80 | done |
| 81 | } |
| 82 | |
| 83 | runSteps |
| 84 |