Skip to content

Instantly share code, notes, and snippets.

@wafsek
Last active November 2, 2024 09:54
Show Gist options
  • Save wafsek/b78cb3214787a605a28b to your computer and use it in GitHub Desktop.
Save wafsek/b78cb3214787a605a28b 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.
# 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
@wafsek
Copy link
Author

wafsek commented Jan 4, 2021

Thanks for the feedback. I appreciate it :)

@iusmac
Copy link

iusmac commented Oct 28, 2021

Good job. But I think you don't need option "9)" "End script". Simply check the command exit code and break the loop.

while [ 1 ]
do
    whiptail --title="[...]" --menu="[...]" [...]
    if [ $? -gt 0 ]; then # user pressed <Cancel> button
        break
    fi
done

@wafsek
Copy link
Author

wafsek commented Oct 28, 2021

Good job. But I think you don't need option "9)" "End script". Simply check the command exit code and break the loop.

while [ 1 ]
do
    whiptail --title="[...]" --menu="[...]" [...]
    if [ $? -gt 0 ]; then # user pressed <Cancel> button
        break
    fi
done

ahh thanks i will see and do it .

@wafsek
Copy link
Author

wafsek commented Dec 1, 2022

Thank you! This is a very good example

Thank you so much. I appreciate it :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment