LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 05-29-2012, 01:43 AM   #1
sukhdip
Member
 
Registered: Sep 2011
Posts: 54

Rep: Reputation: Disabled
Question 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,
 
Old 05-29-2012, 03:13 AM   #2
cliffordw
Member
 
Registered: Jan 2012
Location: South Africa
Posts: 497

Rep: Reputation: 188Reputation: 188
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
 
Old 05-29-2012, 05:23 AM   #3
sukhdip
Member
 
Registered: Sep 2011
Posts: 54

Original Poster
Rep: Reputation: Disabled
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.
 
Old 05-29-2012, 05:47 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,421

Rep: Reputation: 2815Reputation: 2815Reputation: 2815Reputation: 2815Reputation: 2815Reputation: 2815Reputation: 2815Reputation: 2815Reputation: 2815Reputation: 2815Reputation: 2815
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.
 
Old 05-29-2012, 07:02 AM   #5
cliffordw
Member
 
Registered: Jan 2012
Location: South Africa
Posts: 497

Rep: Reputation: 188Reputation: 188
Quote:
Originally Posted by sukhdip View Post
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.

Last edited by cliffordw; 05-29-2012 at 07:03 AM.
 
Old 05-29-2012, 08:41 AM   #6
sukhdip
Member
 
Registered: Sep 2011
Posts: 54

Original Poster
Rep: Reputation: Disabled
Post

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;
 
Old 05-29-2012, 02:55 PM   #7
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,421

Rep: Reputation: 2815Reputation: 2815Reputation: 2815Reputation: 2815Reputation: 2815Reputation: 2815Reputation: 2815Reputation: 2815Reputation: 2815Reputation: 2815Reputation: 2815
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.
 
Old 05-30-2012, 03:47 AM   #8
sukhdip
Member
 
Registered: Sep 2011
Posts: 54

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by grail View Post
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
 
Old 05-30-2012, 05:46 AM   #9
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,421

Rep: Reputation: 2815Reputation: 2815Reputation: 2815Reputation: 2815Reputation: 2815Reputation: 2815Reputation: 2815Reputation: 2815Reputation: 2815Reputation: 2815Reputation: 2815
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Shell based menu of options lucmove Programming 6 01-02-2012 08:35 PM
Right-click menu options yogomix Red Hat 1 07-13-2010 07:20 AM
menu for some options kopper27 Linux - Newbie 6 04-07-2010 04:35 AM
Boot menu options carlindenver Linux - Newbie 1 10-23-2005 02:30 PM
Question on Boot Menu options blair Linux - Newbie 1 05-05-2005 05:12 AM


All times are GMT -5. The time now is 09:55 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration