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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
|
05-29-2012, 12:43 AM
|
#1
|
Member
Registered: Sep 2011
Posts: 55
Rep:
|
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,
|
|
|
05-29-2012, 02:13 AM
|
#2
|
Member
Registered: Jan 2012
Location: South Africa
Posts: 509
|
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
|
|
|
05-29-2012, 04:23 AM
|
#3
|
Member
Registered: Sep 2011
Posts: 55
Original Poster
Rep:
|
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.
|
|
|
05-29-2012, 04:47 AM
|
#4
|
LQ Guru
Registered: Sep 2009
Location: Perth
Distribution: Arch
Posts: 10,018
|
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.
|
|
|
05-29-2012, 06:02 AM
|
#5
|
Member
Registered: Jan 2012
Location: South Africa
Posts: 509
|
Quote:
Originally Posted by sukhdip
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 06:03 AM.
|
|
|
05-29-2012, 07:41 AM
|
#6
|
Member
Registered: Sep 2011
Posts: 55
Original Poster
Rep:
|
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;
|
|
|
05-29-2012, 01:55 PM
|
#7
|
LQ Guru
Registered: Sep 2009
Location: Perth
Distribution: Arch
Posts: 10,018
|
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.
|
|
|
05-30-2012, 02:47 AM
|
#8
|
Member
Registered: Sep 2011
Posts: 55
Original Poster
Rep:
|
Quote:
Originally Posted by grail
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
|
|
|
05-30-2012, 04:46 AM
|
#9
|
LQ Guru
Registered: Sep 2009
Location: Perth
Distribution: Arch
Posts: 10,018
|
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 07:57 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|