Reputation: 15
I have a Shell Script to do 4 tasks within a menu. The code I have so far is (basic one i guess) without a menu. I wanted it to add the menu at the start and i have tried many ways (using case statement, if else and while loop). Nothing has been worked out.
#!/bin/bash
cd /
echo "1. Basic Details"
echo "2. CPU Information"
echo "3. Network information"
echo "4. All"
echo "5. Cancel"
read option
if [ $option -eq 1 ];then
echo ">>>>> Server Name, Date, UPtime <<<<<"
echo "====================================="
echo "Date :- `date`"
echo " "
echo "Host Name :- `hostname`"
echo " "
echo " OS Version"
oslevel -g
echo " "
echo " UPTIME :- "
uptime
echo " "
echo " "
elif [ $option -eq 2 ];then
echo ">>>>> CPU and Memory Info. <<<<<"
echo "====================================="
echo " "
echo
echo " CPU :- `lsdev | grep Processor | wc -l`"
echo " "
echo " Memory :- `lsattr -El mem0 | tail -1`"
echo " "
echo " "
echo "====================================="
echo ">>>>> Important Kernel Params. <<<<<"
echo "====================================="
echo " "
echo "****************************************"
echo " "
lsattr -El aio0
elif [ $option -eq 3 ];then
echo ">>>>> Memory Usage Information <<<<<"
um=`svmon -G | head -2|tail -1| awk {'print $3'}`
um=`expr $um / 256`
tm=`lsattr -El sys0 -a realmem | awk {'print $2'}`
tm=`expr $tm / 1024`
fm=`expr $tm - $um`
ump=`expr $um \* 100`
ump=`expr $ump / $tm`
echo " "
echo "Memory Used :- "$ump"%"
echo " "
echo "----------------------";
echo "Memory Information\n";
echo "total memory = $tm MB"
echo "free memory = $fm MB"
echo "used memory = $um MB"
echo "-----------------------\n";
echo " "
echo " "
else
echo "Enter correct option"
fi
Please suggest with corrections. Not added the option 4 in script( i need it to display all the information)
Upvotes: 1
Views: 3254
Reputation: 158010
bash has the select
builtin:
select val in cherry banana apple ; do
echo "$val"
# do other things with $val
# ...
break # Leave this out if you want to run
# the menu forever in an endless loop
done
Upvotes: 0
Reputation: 1037
I quite don't understand "add the menu at start", In you script itself menu is the first printed statements. Here is the example of repeated menu where exiting the program is depends on user input
#!/bin/sh
#method to just print your menu
printmenu() {
echo "1. Operation 1"
echo "2. Operation 2"
echo "3. Exit"
}
operation1() {
echo "Operation 1 is performed..!"
}
operation2() {
echo "Operation 2 is performed..!"
}
while [ 1 ]
do
printmenu # printing menu
read u_option
#case handling the user inputs
case $u_option in
1) operation1;; # calling function for operation1
2) operation2;; # calling function for operation2
3) echo "Terminating the program as per your request"
exit;;
*) echo "Invalid option, provide only valid integers"
esac
done
Upvotes: 0
Reputation: 246827
This is more of a code review.
select
#!/bin/bash
main() {
cd /
PS3="Enter a choice: "
select ch in "Basic Details" "CPU Information" "Network Information" All Cancel
do
case $ch in
"Basic Details")
basicDetails
;;
"CPU Information")
cpuInfo
;;
"Network Information")
memUsage # you need to fix the menu text
;;
All)
basicDetails
cpuInfo
memUsage
;;
Cancel)
break
;;
esac
done
}
basicDetails() {
echo ">>>>> Server Name, Date, UPtime <<<<<"
echo "====================================="
echo "Date :- $(date)"
echo " "
echo "Host Name :- $(hostname)"
echo " "
echo " OS Version"
oslevel -g
echo " "
echo " UPTIME :- "
uptime
echo " "
echo " "
}
cpuInfo() {
echo ">>>>> CPU and Memory Info. <<<<<"
echo "====================================="
echo " "
echo
echo " CPU :- $(lsdev | grep -c Processor)"
echo " "
echo " Memory :- $(lsattr -El mem0 | tail -1)"
echo " "
echo " "
echo "====================================="
echo ">>>>> Important Kernel Params. <<<<<"
echo "====================================="
echo " "
echo "****************************************"
echo " "
lsattr -El aio0
}
memUsage() {
echo ">>>>> Memory Usage Information <<<<<"
um=$(svmon -G | head -2|tail -1| awk '{print $3}')
um=$(( um / 256 ))
tm=$(lsattr -El sys0 -a realmem | awk '{print $2}')
tm=$(( tm / 1024 ))
fm=$(( tm - um ))
ump=$(( um * 100 / tm ))
echo " "
echo "Memory Used :- $ump%"
echo " "
echo "----------------------"
echo "Memory Information"
echo
echo "total memory = $tm MB"
echo "free memory = $fm MB"
echo "used memory = $um MB"
echo "-----------------------"
echo
echo " "
echo " "
}
main "$@"
Upvotes: 1