Skip to content

Instantly share code, notes, and snippets.

@robertkraig
Forked from wafsek/whiptailmenu.bash
Created August 22, 2018 20:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save robertkraig/40957d9f742cb38ff4e03a9122f3c07c to your computer and use it in GitHub Desktop.
Save robertkraig/40957d9f742cb38ff4e03a9122f3c07c to your computer and use it in GitHub Desktop.
A Example of whiptail menu with functions
#! /bin/bash
# This program is just an example of how to make a whiptail menu and some basic commands.
# Copyright (C) 2016 Baljit Singh Sarai
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
clear
function contextSwitch {
{
ctxt1=$(grep ctxt /proc/stat | awk '{print $2}')
echo 50
sleep 1
ctxt2=$(grep ctxt /proc/stat | awk '{print $2}')
ctxt=$(($ctxt2 - $ctxt1))
result="Number os context switches in the last secound: $ctxt"
echo $result > result
} | whiptail --gauge "Getting data ..." 6 60 0
}
function userKernelMode {
{
raw=( $(grep "cpu " /proc/stat) )
userfirst=$((${raw[1]} + ${raw[2]}))
kernelfirst=${raw[3]}
echo 50
sleep 1
raw=( $(grep "cpu " /proc/stat) )
user=$(( $((${raw[1]} + ${raw[2]})) - $userfirst ))
echo 90
kernel=$(( ${raw[3]} - $kernelfirst ))
sum=$(($kernel + $user))
result="Percentage of last sekund in usermode: \
$((( $user*100)/$sum ))% \
\nand in kernelmode: $((($kernel*100)/$sum ))%"
echo $result > result
echo 100
} | whiptail --gauge "Getting data ..." 6 60 0
}
function interupts {
{
ints=$(vmstat 1 2 | tail -1 | awk '{print $11}')
result="Number of interupts in the last secound: $ints"
echo 100
echo $result > result
} | whiptail --gauge "Getting data ..." 6 60 50
}
while [ 1 ]
do
CHOICE=$(
whiptail --title "Operative Systems" --menu "Make your choice" 16 100 9 \
"1)" "The name of this script." \
"2)" "Time since last boot." \
"3)" "Number of processes and threads." \
"4)" "Number of context switches in the last secound." \
"5)" "How much time used in kernel mode and in user mode in the last secound." \
"6)" "Number of interupts in the last secound." \
"9)" "End script" 3>&2 2>&1 1>&3
)
result=$(whoami)
case $CHOICE in
"1)")
result="I am $result, the name of the script is start"
;;
"2)")
OP=$(uptime | awk '{print $3;}')
result="This system has been up $OP minutes"
;;
"3)")
p=$(ps ax | wc -l)
t=$(ps amx | wc -l)
result="Number of processes $p\nNumber os threads $t"
;;
"4)")
contextSwitch
read -r result < result
;;
"5)")
userKernelMode
read -r result < result
;;
"6)")
interupts
read -r result < result
;;
"9)") exit
;;
esac
whiptail --msgbox "$result" 20 78
done
exit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment