Skip to main content

Menu driven program to do some shell commands



#menu driven program to do some shell functionality

  while true
  do
      cat <<menu
        1.date
        2.cal
        3.directory
        4.exit
    menu
echo "Enter your choice"
read op
    case $op in
    1) echo "todays date is"
       date;;
    2) echo "current month calendar"
       cal;;
    3)echo "directory listing"
       ls;;
    4)exit;;
    *)echo "invalid option";;
    esac
done

Note: Here the while true statement will make the loop to execute infinitely.The exit statement cause the program to terminate when the option 4 is pressed.cat command is used here for making a menu.You can also use simple echo statement.

Another efficient way to create menu in bash shell script is to use select statement as shown below.


menu="date cal dir exit"
PS3="Enter choice..:"
select op in $menu
do
    case $op in
    'date') echo "todays date is"
               date;;

    'cal') echo "current month calendar"
                cal;;

    'dir')echo "directory listing"
                ls;;

    'exit')exit;;

    *)echo "invalid option";;
    esac
done


Simple way to create a menu driven program
while true
do
echo "********************"
echo "Menu"
echo "1.date and time"
echo "2.calendar of the month"
echo "3.exit"
echo "Enter the choice....."
read ch
echo "********************"
case $ch in
1)echo "date and time is"
  date;;
2)echo "Feb 2024 calendar"
  cal 02 2024;;
3)exit;;
esac
done

Comments