Demo entry 6319310

bash

   

Submitted by anonymous on Nov 07, 2016 at 21:56
Language: Bash. Code size: 4.6 kB.

#!/bin/bash
# A menu driven shell script sample template 
# Author   Date    Version    Decription
# --------------------------------
# Vendhan  11042016 0.1 Menu to retrieve kernel
#            related info
#

## ----------------------------------
# Step #1: Define variables
# ----------------------------------
EDITOR=vim
RED='\033[0;41;30m'
STD='\033[0;0;39m'
 
# ----------------------------------
# Step #2: User defined function
# ----------------------------------
pause(){
  read -p "Press [Enter] key to continue..." fackEnterKey
}

cpu(){
    echo "CPU Type and model :`cat /proc/cpuinfo | grep 'model name' |cut -d':' -f2 `"
    echo "--------------------------------------------------------------------------------------------"
}
 
booted(){
    echo "Last Booted since : `uptime -p`"
    echo "--------------------------------------------------------------------------------------------"
}
 
idle_status(){
    echo "CPU spent in the idle state:`top -b -n1 | grep "Cpu(s)" | awk '{print $2 + $4}'`"
    #finding % using top ##echo "`top -bn 2 -d 0.01 | grep '^%Cpu' | tail -n 1 | gawk '{print $2+$4+$6}'`"
    echo "--------------------------------------------------------------------------------------------"
}
memory_status(){
    echo "Memory Status (MemTotal): `cat /proc/meminfo |grep MemTotal |cut -d':' -f2 `"
    echo "--------------------------------------------------------------------------------------------"
}
avail_memory_status(){
    echo "Available Memory (MemAvailable): `cat /proc/meminfo |grep MemAvailable |cut -d':' -f2`"
    echo "--------------------------------------------------------------------------------------------"
}
kernel_context(){
    echo "Context Switches:(fetching output in 1-5 secs)"
    echo "`pidstat -w 10 1`"
    echo "--------------------------------------------------------------------------------------------"
}
process_origin(){
    echo "`cat /proc/stat |grep processes` created since boot time (`uptime -p`)" 
    echo "--------------------------------------------------------------------------------------------"
}

print_all(){
    echo "############################################################################################"
    echo "Printing All Menu Outputs"
    echo "############################################################################################"
         cpu 
         booted 
         idle_status
         memory_status
         avail_memory_status
         kernel_context
         process_origin
    echo "############################################################################################"
}
exit_menu(){
     echo "`banner 'ThankYou'`"
    exit 0 
}    
# function to display menus
show_menus() {
    clear
    echo "-------------------------------"
    echo " AOS - Ubuntu - Kernel Info    "
    echo "        M E N U                "
    echo "-------------------------------"
    echo "a) What is the CPU type and model?"
    echo "b) How long (in days, hours and minutes) has it been since the system was last booted?"
    echo "c) How much time the CPU spent in idle state?"
    echo "d) How much memory is configured into the machine?"
    echo "e) How much memory is currently available on it?"
    echo "f) How many context switches has the kernel performed so far?"
    echo "g) How many processes have been created since the system was booted?"
    echo "p) Print All"
    echo "x) Exit"
       
}
# read input from the keyboard and take a action
# invoke the one() when the user select 1 from the menu option.
# invoke the two() when the user select 2 from the menu option.
# Exit when user the user select 3 form the menu option.
read_options(){
    local choice
    read -p "Enter choice [ a-g,p,x] " choice
    case $choice in
        a) cpu && pause ;;
        b) booted && pause ;;
        c) idle_status && pause ;;
        d) memory_status && pause ;;
        e) avail_memory_status && pause;;
        f) kernel_context && pause ;;
        g) process_origin && pause ;;
        p) print_all && pause ;;
        x) exit_menu;;
        *) echo -e "${RED}Error...${STD}" && sleep 2
    esac
}
 
# ----------------------------------------------
# Step #3: Trap CTRL+C, CTRL+Z and quit singles
# ----------------------------------------------
trap '' SIGINT SIGQUIT SIGTSTP
 
# -----------------------------------
# Step #4: Main logic - infinite loop
# ------------------------------------
while true
do
 
    show_menus
    read_options
done

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).