Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 97fb101

Browse files
committed
Simplify logic using a case statement, remove redundant code, demonstrate a break
1 parent 69444e0 commit 97fb101

File tree

1 file changed

+16
-15
lines changed

1 file changed

+16
-15
lines changed

scripts/while-menu.sh

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/bin/bash
22
# while-menu: a menu driven system information program
33
DELAY=1 # Number of seconds to display results
4-
while [[ $REPLY != 0 ]]; do
4+
while true; do
55
clear
66
cat << EOF
77
Please Select:
@@ -11,29 +11,30 @@ while [[ $REPLY != 0 ]]; do
1111
0. Quit
1212
EOF
1313
read -p "Enter selection [0-3] > "
14-
if [[ $REPLY =~ ^[0-3]$ ]]; then
15-
if [[ $REPLY == 1 ]]; then
14+
case "$REPLY" in
15+
0)
16+
break
17+
;;
18+
1)
1619
echo "Hostname: $HOSTNAME"
1720
uptime
18-
sleep $DELAY
19-
fi
20-
if [[ $REPLY == 2 ]]; then
21+
;;
22+
2)
2123
df -h
22-
sleep $DELAY
23-
fi
24-
if [[ $REPLY == 3 ]]; then
24+
;;
25+
3)
2526
if [[ $(id -u) -eq 0 ]]; then
2627
echo "Home Space Utilization (All Users)"
2728
du -sh /home/*
2829
else
2930
echo "Home Space Utilization ($USER)"
3031
du -sh $HOME
3132
fi
32-
sleep $DELAY
33-
fi
34-
else
35-
echo "Invalid entry."
36-
sleep $DELAY
37-
fi
33+
;;
34+
*)
35+
echo "Invalid entry."
36+
;;
37+
esac
38+
sleep "$DELAY"
3839
done
3940
echo "Program terminated."

0 commit comments

Comments
 (0)