Last active 1746271884

do-nothing-script-example.bash Raw
1#!/bin/bash
2
3function printDivider() {
4 local columns
5 columns=$(tput cols)
6 printf '─%.0s' $(seq 1 "$columns")
7 echo ''
8}
9
10# Global state
11user_name=""
12greeting=""
13
14function step1() {
15 echo "Step 1: Ask for your name"
16 read -rp "Enter your name: " user_name
17}
18
19function 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
28function 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
37steps=("step1" "step2" "step3")
38current_step=0
39
40function 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
49function 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
83runSteps
84