LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Menu with sub-menu options (https://www.linuxquestions.org/questions/linux-newbie-8/menu-with-sub-menu-options-947326/)

sukhdip 05-29-2012 12:43 AM

Menu with sub-menu options
 
Hi!
I have created on script which is working fine with menu options and with a sub-menu.
I want to enhance it by using sub-options under menu options.
Like.
Quote:

1) Take input from user and show list of files with that name.
1.1) Show the contents of file chosen by user from the list.
option 1) will give the list only.
option 1.1) should give the option to user to choose one file, whose content user wanna see.
I can implement this as a separate option. But I want to implement this under option 1.
Can this be happen? I tried using some but failed.

Here is how my script is working.
Code:

#!/usr/bin/ksh

script_menu() {
while :
      do
      clear
echo  some action here
read choice
case $choice in

1)
some action as per choice;;
2)   
      return;;
3)
      exit;;
    esac
 done
}

#Main Menu
while :
      do
      clear
      echo ----------------------------------------------------
      echo    ******************Main Menu******************
      echo ----------------------------------------------------
      echo Select a Choice
      echo [1] call sub menu
      echo [2] View Log Files
      echo [3] Exit
      echo ----------------------------------------------------
      echo Select choice {1-2}:
read choice
case $choice in
1)
      script_menu;;
2)   
      echo files;
      echo Enter file name;
      read -r filename ; find "$filename"_*.* -type f ! -name ".*" 2>errorlog
      echo Press Enter;
      read x;;
2.1) Can I solve above question here, like this?

3) 
      exit;;
*)
      echo Invalid Number
    esac
done

please provide me your valuable help.
Thanks,

cliffordw 05-29-2012 02:13 AM

Something along the lines of this code snippet might help:
Code:

case $choice in
1)
      script_menu;;
2 | 2.1)   
      echo files;
      echo Enter file name to list;
      read -r filename ; find . -type f -name "${filename}_*.*" ! -name ".*"
      if [ "$choice" = "2.1" ]
      then

          echo Enter file name to view;
          read filename
          if [ ! -z "$filename" ]
          then
              if [ -f "$filename" ]
              then
                  less "$filename"
              else
                  echo "$filename not found"
                  read
              fi
          fi
      else
          echo Press Enter;
          read x
      fi
      ;;
3) 
      exit;;
*)
      echo Invalid Number
      read x;;
    esac
done


sukhdip 05-29-2012 04:23 AM

Hi I made some enhancement in the code. But its not helping out.

Code:

case $choice in
1)
      script_menu;;
2 | 2.1)   
      echo files;
      echo Enter file name to list;
      read -r filename ; find /test/LogFiles/$filename"_"*.* -type f ! -name ".*" 2>errorlog | awk -F/ '{ print $NF }'
      if [ -s errorlog ] ; then echo "No file with this name"; fi

      if [ "$choice" = "2.1" ]
      then
          echo Enter file name to view;
          read -r filename
          if [ ! -z "$filename" ]
          then
              if [ -f "$filename" ]
              then
                  less "$filename"
              else
                  echo "$filename not found"
                  read
              fi
          fi
      else
          echo Press Enter;
          read x
      fi
      ;;
3) 
      exit;;
*)
      echo Invalid Number
      read x;;
    esac
done

This is my final code. Its working functionality is same i.e., taking input from user print out the list. But the further is not working.
What exactly I wanna do:
1. User enter to view the log files.
2. Entered the menu number. Menu shows the list of files.
3. Now he wanna see the contents of a particular file.
4. There should be an option which make him capable to enter the file name, so that he can see the contents of file.
5. If he doesn't enter the file name. It should return back to menu. Without saying anything.

grail 05-29-2012 04:47 AM

Have you perhaps tried looking at the 'select' command? It would seem trivial to use it as the main menu operator and also to then apply a smaller sub-select on the files you are interested in.

cliffordw 05-29-2012 06:02 AM

Quote:

Originally Posted by sukhdip (Post 4690082)
Hi I made some enhancement in the code. But its not helping out.

Its working functionality is same i.e., taking input from user print out the list. But the further is not working.
What exactly I wanna do:
1. User enter to view the log files.
2. Entered the menu number. Menu shows the list of files.
3. Now he wanna see the contents of a particular file.
4. There should be an option which make him capable to enter the file name, so that he can see the contents of file.
5. If he doesn't enter the file name. It should return back to menu. Without saying anything.

I'm not sure I understand what is not working?

Your find command might be the problem, though. It specifies a directory to search in (/test/LogFiles), which your original version didn't do, and if that is not your current directory, the option 2.1 code would not find it. Changing:
Code:

if [ ! -z "$filename" ]
then
    if [ -f "$filename" ]
    then
        less "$filename"
    else
        echo "$filename not found"
        read
    fi
fi

to:
Code:

dirname="/test/LogFiles"
if [ ! -z "$dirname/$filename" ]
then
    if [ -f "$dirname/$filename" ]
    then
        less "$dirname/$filename"
    else
        echo "$filename not found"
        read
    fi
fi

should fix this.

sukhdip 05-29-2012 07:41 AM

Hi all, thanks for you help.
I solved it like this:

Code:

    echo View Log files;   
    find /backup/test1/LogFiles/*.log -type f ! -name ".*" 2>errorlog | awk -F/ '{ print $NF }'
    if [ -s errorlog ] ; then
    echo "No file in Logs"
    noFile=1;
    fi
    if [ "$noFile" = "0" ]; then   

        echo Enter the file name to show its contents;
        read -r filename ;
        if [ "$filename" != "" ]; then
            find /backup/test1/LogFiles/$filename -type f ! -name ".*" -exec more {} \;
        fi
    fi


    echo Press Enter to continue..;
    read x;


grail 05-29-2012 01:55 PM

Well I am curious as to what errors you are anticipating from the first find that are not an issue in the second?

Also, you might be interested in looking up the -printf option for find so you can throw away the useless awk.
If you want to be more clever, you could store the output in an array and use it in a select, this way you do not rely on the user to type the filename in correctly :)

have you also had a close look at your last find command?
Code:

find /backup/test1/LogFiles/$filename -type f ! -name ".*" -exec more {} \;
You already provide the full path to the file, so your find cannot find anything but this one entry, ie just more the same as the find is not doing anything.

sukhdip 05-30-2012 02:47 AM

Quote:

Originally Posted by grail (Post 4690524)
Well I am curious as to what errors you are anticipating from the first find that are not an issue in the second?

Also, you might be interested in looking up the -printf option for find so you can throw away the useless awk.
If you want to be more clever, you could store the output in an array and use it in a select, this way you do not rely on the user to type the filename in correctly :)

have you also had a close look at your last find command?
Code:

find /backup/test1/LogFiles/$filename -type f ! -name ".*" -exec more {} \;
You already provide the full path to the file, so your find cannot find anything but this one entry, ie just more the same as the find is not doing anything.

thanks alot for guiding me. actually i'm at learner level now. I wanna learn more n more ways to do using script. I will be very thankful if you guide me in little bit detail. coz I don't know how to use select command.
about find, I'm not able to understand, I just used them and made them work as per requirement. I'd love to learn more. plz tell :)

grail 05-30-2012 04:46 AM

Ok ... I had a bit of a play and you will need to change to how you like it.

Things to note: Find used assumes that there are no sub directories and that filenames do not contain whitespace
Code:

#!/bin/bash

script_menu() {
    while :
    do
        clear
        echo  some action here
        read choice
        case $choice in

            1)
                some action as per choice;;
            2)
                return;;
            3)
                exit;;
        esac
    done
}

menu_type()
{
    if [[ $1 == main ]]
    then
        cat <<EOF
            -----------------------------------------------------
                ******************Main Menu******************
                (Press Enter key at anytime to redisplay list)
            -----------------------------------------------------

EOF
    else
        cat <<EOF
            ----------------------------------------------------
                ******************Sub Menu******************
              (Press Enter key at anytime to redisplay list)
            ----------------------------------------------------

EOF
    fi
}

declare -a main_menu

main_menu=( "call sub menu" "View Log Files" Exit )

PS3="Select a choice: "
clear
menu_type main

select choice in "${main_menu[@]}"
do
    case $REPLY in
        1)  script_menu;;

        2)  clear
            PS3="Select a file: "
            menu_type
            select f in $(find /backup/test1/LogFiles/ -type f ! -name '.*' -printf "%f\n") EXIT
            do
                case $f in
                    "") echo "Invalid selection, please try again";;

                    EXIT)  break;;

                    *)  less /backup/test1/LogFiles/$f
                        echo "Press Enter key to view list again";;
                esac
            done;;

        3)  break;;

        *)  echo "Invalid choice, please try again";;
    esac
    PS3="Select a choice: "
    clear
    menu_type main
done

I did not alter your original sub_menu code and used less instead of more as I find it easier when navigating files (up to you of course)

Hopefully it will give you some ideas.


All times are GMT -5. The time now is 03:05 PM.