Menu based programs in Shell Script

By: Vivek G Emailed: 1698 times Printed: 2203 times    

Latest comments
By: rohit kumar - how this program is work
By: Kirti - Hi..thx for the hadoop in
By: Spijker - I have altered the code a
By: ali mohammed - why we use the java in ne
By: ali mohammed - why we use the java in ne
By: mizhelle - when I exported the data
By: raul - no output as well, i'm ge
By: Rajesh - thanx very much...
By: Suindu De - Suppose we are executing

This shell script shows how to script menu-based programs in linux. This sample program implements menus using dialog utility. Menu-items and action according to select menu-item is as follows:

Menu-Item Purpose Action for Menu-Item
Date/time  To see current date time Date and time must be shown using infobox of dialog utility
Calendar To see current calendar Calendar must be shown using infobox of dialog utility
Delete To delete selected file First ask user name of directory where all files are present, if no name of directory given assumes current directory, then show all files only of that directory, Files must be shown on screen using menus of dialog utility, let the user select the file, then ask the confirmation to user whether he/she wants to delete selected file, if answer is yes then delete the file , report  errors if any while deleting file to user.
Exit To Exit this shell script Exit/Stops the menu driven program i.e. this script
 
show_datetime()
{
   dialog --backtitle "Linux Shell Tutorial" --title "System date and Time" --infobox "Date is `date`" 3 40 
   read
   return
}

show_cal()
{
   cal > menuchoice.temp.$$
   dialog --backtitle "Linux Shell Tutorial" --title "Calender" --infobox "`cat menuchoice.temp.$$`" 9 25 
   read
   rm -f menuchoice.temp.$$
   return
}

delete_file()
{
 dialog --backtitle "Linux Shell Tutorial" --title "Delete file"\
 --inputbox "Enter directory path (Enter for Current Directory)"\
 10 40  2>/tmp/dirip.$$
 rtval=$?
 
 case $rtval in
     1) rm -f /tmp/dirip.$$ ; return ;;
     255) rm -f /tmp/dirip.$$ ; return ;;
 esac
 
 mfile=`cat /tmp/dirip.$$`
 
 if [ -z $mfile ]
 then
     mfile=`pwd`/*
 else
     grep "*" /tmp/dirip.$$
     if [ $? -eq 1 ]
     then
	mfile=$mfile/*
     fi    
 fi
 
 for i in $mfile 
 do
    if [ -f $i ]
    then
	echo "$i Delete?" >> /tmp/finallist.$$
    fi	
 done    


 dialog --backtitle "Linux Shell Tutorial" --title "Select File to Delete"\
 --menu "Use [Up][Down] to move, [Enter] to select file"\
 20 60 12 `cat /tmp/finallist.$$` 2>/tmp/file2delete.tmp.$$
 
 rtval=$? 
 
 file2erase=`cat /tmp/file2delete.tmp.$$`
 
 case $rtval in 
     0) dialog --backtitle "Linux Shell Tutorial" --title "Are you shur"\
      --yesno "\n\nDo you want to delete : $file2erase " 10 60
      
        if [ $? -eq 0 ] ; then
	  rm -f  $file2erase         
         if [ $? -eq 0 ] ; then
            dialog --backtitle "Linux Shell Tutorial"\
	    --title "Information: Delete Command" --infobox "File: $file2erase is Sucessfully deleted,Press a key" 5 60
	    read
	   else
	    dialog --backtitle "Linux Shell Tutorial"\ 
	    --title "Error: Delete Command" --infobox "Error deleting File: $file2erase, Press a key" 5 60
            read       
           fi
	else
	  dialog --backtitle "Linux Shell Tutorial"\
	  --title "Information: Delete Command" --infobox "File: $file2erase is not deleted, Action is canceled, Press a key" 5 60
	  read
	fi
     ;;
    1)  rm -f /tmp/dirip.$$ ; rm -f /tmp/finallist.$$ ; 
        rm -f /tmp/file2delete.tmp.$$; return;;
    255) rm -f /tmp/dirip.$$ ;  rm -f /tmp/finallist.$$ ;
         rm -f /tmp/file2delete.tmp.$$; return;;
esac
 rm -f /tmp/dirip.$$
 rm -f /tmp/finallist.$$
 rm -f /tmp/file2delete.tmp.$$
 return
}



while true
do
dialog --clear --title "Main Menu" \
        --menu "To move [UP/DOWN] arrow keys \n\
[Enter] to Select\n\
        Choose the Service you like:" 20 51 4 \
        "Date/time"       "To see System Date & Time" \
        "Calender"        "To see Calaender"\
	"Delete"          "To remove file"\
	"Exit"            "To exit this Program" 2> menuchoice.temp.$$

retopt=$?

choice=`cat menuchoice.temp.$$`

rm -f menuchoice.temp.$$

case $retopt in
    0)
	case $choice in
	    Date/time) show_datetime ;;
	    Calender) show_cal ;;
	    Delete) delete_file ;;
	    Exit) exit 0;; 
        esac    
      ;;	
     1) exit ;;
     255) exit ;;
 esac
done
clear


Linux Home | All Linux Tutorials | Latest Linux Tutorials

Sponsored Links

If this tutorial doesn't answer your question, or you have a specific question, just ask an expert here. Post your question to get a direct answer.



Bookmark and Share

Comments(0)


Be the first one to add a comment

Your name (required):


Your email(required, will not be shown to the public):


Your sites URL (optional):


Your comments:



More Tutorials by Vivek G
Getting System Information in Shell Script
Menu based programs in Shell Script
Digital Clock in Shell Script
Formating text in Shell Script
Check if file exists in Shell Script
Finding factorial in Shell Script
Decimal to hexadecimal converter in Shell Script
Calculator tutorial in Shell Script
getopts tutorial in Shell Script
Find the biggest number in Shell Script
Command line arguments to calculate average in Shell Script
Command line arguments in Shell Script
Sort numbers using arrays in Shell Script
Check for leap year in Shell Script
Using System Time in Shell Script

More Tutorials in Linux
smskannel SMS gateway run in background
Running jar files in background in ssh window
Could not open '': No such file or directory at lib/ExtUtils/MM_Unix.pm line 2697
Can't locate ExtUtils/MakeMaker.pm in @INC ...
bash: make: command not found
apxs: command not found
bash: svn: command not found
make: Nothing to be done for `all'.
How to burn your CD / DVD ISO image using Media Creator (Adaptec/Roxio) on Windows
How to burn your CD / DVD ISO image using Nero Burning ROM (Ahead Software) on Windows
How to burn your CD / DVD ISO image using Nero Express (Ahead Software) on Windows
How to burn your CD / DVD ISO image using NISO Recorder V2 Power Toy on Windows
How to burn your CD / DVD ISO image using k3b on CentOS
Compiling and Installing software from source in Linux
Installing RPM's using the Redhat Package Manager in Linux

More Latest News
Most Viewed Articles (in Linux )
Running jar files in background in ssh window
Nested if-else-fi in Linux Shell Script
Nested for loop in Linux Shell Script
Command line arguments to calculate average in Shell Script
Calculator tutorial in Shell Script
Digital Clock in Shell Script
Menu based programs in Shell Script
Getting System Information in Shell Script
apxs: command not found
Mounting cdrom in cent os 5 linux
How to use Linux?
What is a Shell Script in linux?
How to write your first Shell Script in linux?
Variables in Linux Shell Script
How to define User defined variables in Linux Shell Script
Most Emailed Articles (in Linux)
How to write your first Shell Script in linux?
How to define User defined variables in Linux Shell Script
Rules for Naming variable names in Linux Shell Script
Mounting cdrom in cent os 5 linux
Quotes in Linux Shell Script
Input - Output redirection in Linux Shell Script
Using System Time in Shell Script
Formating text in Shell Script
How to burn your CD / DVD ISO image using NISO Recorder V2 Power Toy on Windows
bash: svn: command not found
How to use Linux?
How to print or access value of user defined variables in Linux Shell Script
echo Command in Linux Shell Script
Arithmetic in Linux Shell Script
read Statement in Linux Shell Script