Skip to content

Commit 7b6f93c

Browse files
committed
added ask_yesno, ask_yesno_timeout to more.sh
1 parent ea9bd56 commit 7b6f93c

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

ops.d/more.sh

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,45 @@
11
#
22
# You may add more commands/functions here.
33
#
4+
5+
ask_yesno() {
6+
while true; do
7+
read -p "Do you want to continue? (y/n): " yesno
8+
case $yesno in
9+
[Yy]*)
10+
echo "You chose Yes. Proceeding..."
11+
true
12+
;;
13+
[Nn]*)
14+
echo "You chose No. Exiting."
15+
false
16+
;;
17+
*) echo "Invalid input. Please answer 'y' or 'n'." ;;
18+
esac
19+
done
20+
}
21+
22+
ask_yesno_timeout() {
23+
local timeout="${1:-10}"
24+
local msg="${2:-Do you want to continue? (Y/n): }"
25+
echo "Please enter Y or N within $timeout seconds:"
26+
read -t $timeout -p "$msg" yesno
27+
28+
if [ $? -eq 0 ]; then
29+
# User entered input within the timeout
30+
case $yesno in
31+
[Yy]*)
32+
echo "You chose Yes. Proceeding..."
33+
true
34+
;;
35+
[Nn]*)
36+
echo "You chose No. Exiting."
37+
false
38+
;;
39+
*) echo "Invalid input. Please answer 'y' or 'n'." ;;
40+
esac
41+
else
42+
# Timeout occurred or read failed for another reason
43+
true
44+
fi
45+
}

0 commit comments

Comments
 (0)