Programming Tutorials

Menu based programs in Shell Script

By: Vivek G in Linux Tutorials on 2011-01-29  

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






Add Comment

* Required information
1000

Comments

No comments yet. Be the first!

Most Viewed Articles (in Linux )

Latest Articles (in Linux)