1

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)

3
  • @RavinderSingh13 can you help me on this Commented Jun 25, 2018 at 14:00
  • You might find it easier to use the shell's select..in construct. Beyond that, it is difficult to offer solutions when the problem statement is simply, "it doesn't work". Please edit your question to give a more complete description of what you expected to happen and how that differs from the actual results. See How to Ask for hints on what makes a good explanation. Commented Jun 25, 2018 at 14:13
  • Take a look at dialog. Commented Jun 25, 2018 at 14:49

3 Answers 3

1

This is more of a code review.

#!/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 "$@"
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks Glenn. I need script to prompt for selection(1.Basic details 2.CPU usage 3.Memory usage 4. ALL 5. Cancel). Help me in that. Where is it prompting for the menu in provided code ?
That's exactly what select does. Did you try this code?
I hope you didnt understood what i mean. My need is it should display menu as soon as i execute the script and Just providing the option number should provide output Example : Enter your choice from menu: 1.Basic details 2.CPU Usage 3.Memory 4.ALL 5.Cancel 1 (enter) --> Should display the Basic info 2(enter) --> Should display the CPU and it should return to menu again as soon as function is executed. Hope this clears the issue.
Have you tested the code I gave to you? If yes, I don't know why you're still explaining this to me. If no, why not?
trying above code keeps on asking "Enter a choice" and does not call the method
0

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

Comments

0

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 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.