#!/bin/bash
# SCRIPT : menu_dialog.sh
# PURPOSE : A menu driven Shell script using dialog utility
# which has following options:
# Display Today's Date and Time.
# Display calendar.
# Delete selected file from supplied directory.
# List of users currently logged in
# Disk Statistics
# Exit
#
##############################################################################
# Checking availability of dialog utility #
##############################################################################
# dialog is a utility installed by default on all major Linux distributions.
# But it is good to check availability of dialog utility on your Linux box.
which dialog &> /dev/null
[ $? -ne 0 ] && echo "Dialog utility is not available, Install it" && exit 1
##############################################################################
# Define Functions Here #
##############################################################################
###################### deletetempfiles function ##############################
# This function is called by trap command
# For conformation of deletion use rm -fi *.$$
deletetempfiles()
{
rm -f *.$$
}
######################## Show_time function #################################
# Shows today's date and time
show_time()
{
dialog --backtitle "MENU DRIVEN PROGRAM" --title "DATE & TIME" \
--msgbox "\n Today's Date: `date +"%d-%m-%Y"` \n\n \
Today's Time: `date +"%r %Z"`" 10 60
}
####################### show_cal function ###################################
# Shows current month calendar
show_cal()
{
dialog --backtitle "MENU DRIVEN PROGRAM" --title "CALENDAR" \
--msgbox "`cal`" 12 25
}
####################### deletefile function #################################
# Used to delete file under supplied directory, not including sub dirs.
deletefile()
{
dialog --backtitle "MENU DRIVEN PROGRAM" --title "Directory Path" \
--inputbox "\nEnter directory path (Absolute or Relative) \
\nPress just Enter for current directory" 12 60 2> temp1.$$
if [ $? -ne 0 ]
then
rm -f temp1.$$
return
fi
rmdir=`cat temp1.$$`
if [ -z "$rmdir" ]
then
dirname=$(pwd) # You can also use `pwd`
rmdir=$dirname/*
else
# remove trailing * and / from directory path
echo "$rmdir" | grep "\*$" &> /dev/null && rmdir=${rmdir%\*}
echo "$rmdir" | grep "/$" &> /dev/null && rmdir=${rmdir%/}
# Check supplied directory exist or not
( cd $rmdir 2>&1 | grep "No such file or directory" &> /dev/null )
# Above codeblock run in sub shell, so your current directory persists.
if [ $? -eq 0 ]
then
dialog --backtitle "MENU DRIVEN PROGRAM" \
--title "Validating Directory" \
--msgbox "\n $rmdir: No such file or directory \
\n\n Press ENTER to return to the Main Menu" 10 60
return
fi
# Do you have proper permissions ?
( cd $rmdir 2> /dev/null )
if [ $? -ne 0 ]
then
dialog --backtitle "MENU DRIVEN PROGRAM" \
--title "Checking Permissions" \
--msgbox "\n $rmdir: Permission denied to access this directory \
\n\n Press ENTER to return to the Main Menu" 10 60
return
fi
if [ ! -r $rmdir ]
then
dialog --backtitle "MENU DRIVEN PROGRAM" \
--title "Checking Permissions" \
--msgbox "\n $rmdir: No read permission \
\n\n Press ENTER to return to the Main Menu" 10 60
return
fi
dirname=$rmdir
rmdir=$rmdir/* # get all the files under given directory
fi
for i in $rmdir # process each file
do
# Store all regular file names in temp2.$$
if [ -f $i ]
then
echo " $i delete? " >> temp2.$$
fi
done
if [ -f temp2.$$ ]
then
dialog --backtitle "MENU DRIVEN PROGRAM" \
--title "Select File to Delete" \
--menu "Use [UP/DOWN] keys to move, then press enter \
\nFiles under directory $dirname:" 18 60 12 \
`cat temp2.$$` 2> file2delete.$$
else
dialog --backtitle "MENU DRIVEN PROGRAM" --title "Select File to Delete" \
--msgbox "\n\n There are no regular files in $dirname directory" 10 60
return
fi
rtval=$?
file2remove=`cat file2delete.$$`
case $rtval in
0) dialog --backtitle "MENU DRIVEN PROGRAM" --title "ARE YOU SURE" \
--yesno "\nDo you Want to Delete File: $file2remove" 7 70
if [ $? -eq 0 ]
then
rm -f $file2remove 2> Errorfile.$$
# Check file successfully deleted or not.
if [ $? -eq 0 ]
then
dialog --backtitle "MENU DRIVEN PROGRAM" \
--title "Information : FILE DELETED" \
--msgbox "\nFile : $file2remove deleted" 8 70
else
dialog --backtitle "MENU DRIVEN PROGRAM" \
--title "Information : ERROR ON DELETION" \
--msgbox "\nProblem in Deleting File: $file2remove \
\n\nError: `cat Errorfile.$$` \n\nPress ENTER to return to the Main Menu" 12 70
fi
else
dialog --backtitle "MENU DRIVEN PROGRAM" \
--title "Information : DELETION ABORTED" \
--msgbox "Action Aborted: \n\n $file2remove not deleted" 8 70
fi ;;
*) deletetempfiles # Remove temporary files
return ;;
esac
deletetempfiles # remove temporary files
return
}
########################## currentusers function ############################
currentusers()
{
who > userslist.$$
dialog --backtitle "MENU DRIVEN PROGRAM" \
--title "CURRENTLY LOGGED IN USERS LIST" \
--textbox userslist.$$ 12 60
}
############################ diskstats function #############################
diskstats()
{
df -h | grep "^/" > statsfile.$$
dialog --backtitle "MENU DRIVEN PROGRAM" \
--title "DISK STATISTICS" \
--textbox statsfile.$$ 10 60
}
##############################################################################
# MAIN STRATS HERE #
##############################################################################
trap 'deletetempfiles' EXIT # calls deletetempfiles function on exit
while :
do
# Dialog utility to display options list
dialog --clear --backtitle "MENU DRIVEN PROGRAM" --title "MAIN MENU" \
--menu "Use [UP/DOWN] key to move" 12 60 6 \
"DATE_TIME" "TO DISPLAY DATE AND TIME" \
"CALENDAR" "TO DISPLAY CALENDAR" \
"DELETE" "TO DELETE FILES" \
"USERS" "TO LIST CURRENTLY LOGGED IN USERS" \
"DISK" "TO DISPLAY DISK STATISTICS" \
"EXIT" "TO EXIT" 2> menuchoices.$$
retopt=$?
choice=`cat menuchoices.$$`
case $retopt in
0) case $choice in
DATE_TIME) show_time ;;
CALENDAR) show_cal ;;
DELETE) deletefile ;;
USERS) currentusers ;;
DISK) diskstats ;;
EXIT) clear; exit 0;;
esac ;;
*)clear ; exit ;;
esac
done
OUTPUT:
[venu@localhost ~]$ sh menu_dialog.sh
SAMPLE SCREEN SHOTS:
Main Menu:
Users:
Disk Stats:
Delete File:
Thursday, January 6, 2011
Posted by venu k
25 comments | 1:29 PM
Subscribe to:
Post Comments (Atom)
Its very nice,I will try this.
ReplyDeletecan u pls let me know as how do i create multiple input boxes in single screen.
ReplyDeleteFor ex:
Enter your name:
Enter your age:
Enter your nation:
pls mail me to mannodaada@gmail.com. It will be a great help
dialog --title "Mixed Form" --backtitle "Mixed Form" --insecure \
Delete--mixedform "This is simple mixedform :" 15 50 0 \
"Enter your name: " 1 1 1 20 20 0 0 \
"Enter your age:" 2 1 2 20 20 0 0 \
"Enter your nation:" 3 1 3 20 20 0 0 2> /tmp/file
the menu lists out in one option in each line..
ReplyDeletethats ok..
but how can i print - 1 "Insert" 2 "Delete" 3 "Exit"
all options of menu in one single line in dialog menu ??
how much arguments can we pass to input dialog box and what are they mean for????
ReplyDeleteCan you tell me how to call this script from command prompt passing all these arguments in one shot? so the uses will not be prompted all the time
ReplyDeletenice sript
ReplyDeleteis it possible to write a menu driven bash script to display all user accounts, create new user accounts and delete an user account???
if yes could you please help me
is it possible to write a menu driven bash script to perform smtp check via telnet to port 25 ???
ReplyDeleteVery good script. I will try similar other scripts. Thanks
ReplyDeleteT.Hanks a lot for sharing, first of all. http://buy-essay.us/essayjedi-com-review/ This is what I would want to share with you in return as a thank you for your post.
ReplyDeleteWe list official login and signup pages of most popular websites. we divide these websites into different categories, provide step by step instructions and collect the most important features of these websites
ReplyDeletelogin
the menu lists out in one option in each line..
ReplyDeletethats ok..
but how can i print - 1 "Insert" 2 "Delete" 3 "Exit"
all options of menu in one single line in dialog menu ??
Encuentra juegos friv 2017 en lĂnea gratuitos en Friv 2017 Juegos.
Good post!
ReplyDeleteTraining in Chennai.
Final year java projects chennai
Big data hadoop training in chennai
This is what I need. Thank you for sharing!
ReplyDeletepiknu
Thank you for your sharing. Thanks to this article I can learn more things. Expand your knowledge and abilities. Actually the article is very practical.
ReplyDeleteIkutin bersama situs parlayprediksi untuk jadwal pertandingan piala dunia 2018 terbaru dan terkini. Tentunya event tertinggi dalam olahraga sepakbola ini yang terjadi hanya dalam empat tahun sekali menjadi hal yang menarik sekali apalagi Piala Dunia Rusia 2018 piala dunia 2018
ReplyDeletekali ini menjadi kemungkinan momentum terakhir bagi dua pemain sepakbola terbaik selama 10 tahun terakhir ini. Siapa lagi kalau bukan Cristiano Ronaldo dan Lionel Messi ?
Great reading these your posts.
ReplyDeleteWhat a sensational approach to the subject, great!
ReplyDeleteThank you for sharing wonderful information with us to get some idea about that content
ReplyDeleteAws Training in Chennai
Anda pasti sudah tak asing lagi dengan yang namanya permainan situs poker online. Permainan situs poker online ini sering dihiraukan oleh sebagian orang karena menganggapnya tidak menarik. Permainan poker atau pun domino online sendiri membutuhkan berbagai strategi serta trik untuk bisa memenangkan sebuah permainan.
ReplyDeleteJudi Bola Online
That is a lot of cash when you just have one to three months to pay it back. Purchasers be careful: when contemplating going out on a limb vehicle title credit, consider these money related entanglements previously you make all necessary endorsements. online auto title loans chicago
ReplyDeleteMenmyshopCar StereoDouble Din Android PlayerHyepersonic Double Din PlayerHyundai Creta Double Din Player Hyundai Xcent Double Din PlayerTata Nexon Double Din Player
ReplyDeleteCBSE open schoolcbse privatebanzaraonjourney
I have to thank for sharing this blog, it is really helpful to me.
ReplyDeleteccna Training in Chennai
ccna course in Chennai
RPA Training in Chennai
AWS Training in Chennai
Angularjs Training in Chennai
Usually, I never comment on blogs but yours is so convincing that I never stop myself to say something about it. keep updating regularly.
ReplyDeleteBest IELTS Class in Chennai
Best IELTS Courses in Chennai
IELTS Course in Chennai
IELTS Class near Chennai
IELTS Center in Mumbai
IELTS Training in Mumbai
Best IELTS Coaching Classes in Mumbai
Thanks for posting..
ReplyDeletephp training in chennai
php training in velachery chennai