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.
|
 |
01-20-2009, 07:46 AM
|
#1
|
Member
Registered: Nov 2008
Posts: 31
Rep:
|
File identifier script
Hi
I have a file that contains the following:
drwxr-xr-x 11 root root 4096 2008-06-04 15:32 /usr/local
drwxr-xr-x 2 root root 4096 2008-11-05 09:52 /usr/local/bin
rwxr--r-- 1 root root 163 2008-06-04 15:45 /usr/local/bin/tune.all.sh
lrwxrwxrwx 1 root root 13 2008-09-30 08:41 /usr/local/bin/perl -> /usr/bin/perl
-rwxr--r-- 1 root root 631 2008-06-04 15:45 /usr/local/bin/tune.oracle.default.sh
What i need to do is write a script that will ask a user to enter D for directory, L for list, or F for file and print the contents of the file according to the user's entry. e.g. if the user entered D the script should display only those files taht start with d (i.e. directory) in the file. I dont want you to write me a script but to suggest which tool (awk or sed, or both) to use and how to approach it. I thank you...
Last edited by mmahulo; 01-20-2009 at 07:49 AM.
|
|
|
01-20-2009, 08:17 AM
|
#2
|
LQ Veteran
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809
|
Kudos to you for successfully getting help with homework here....
I'm not sure you need either sed or awk.
1. To get the user input, you can pass it when calling the script, eg: "myscript D", or you can set up a loop which includes the "read" command.
2. What do you want it to do when the user enters "L"?
3. Do you mean print the file contents, or just the file name? If you want the contents, then what happens when the user enters "D"?
4. You'll need the "if" construct to test the user input, and the execute the appropriate command.
5. Does it need to be recursive?---ie do you need to go down multiple levels in the directory tree?
|
|
|
01-20-2009, 07:41 PM
|
#3
|
LQ Guru
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.x
Posts: 18,441
|
As pixellany intimated, bash should be sufficient really. Try this link: http://www.tldp.org/LDP/abs/html/
Last edited by chrism01; 01-22-2009 at 05:11 AM.
Reason: mispell
|
|
|
01-21-2009, 04:35 AM
|
#4
|
Member
Registered: Nov 2008
Posts: 31
Original Poster
Rep:
|
Quote:
Originally Posted by pixellany
Kudos to you for successfully getting help with homework here....
I'm not sure you need either sed or awk.
1. To get the user input, you can pass it when calling the script, eg: "myscript D", or you can set up a loop which includes the "read" command.
2. What do you want it to do when the user enters "L"?
3. Do you mean print the file contents, or just the file name? If you want the contents, then what happens when the user enters "D"?
4. You'll need the "if" construct to test the user input, and the execute the appropriate command.
5. Does it need to be recursive?---ie do you need to go down multiple levels in the directory tree?
|
Thanks for the reply. I need it to print all the filenames associated with the file. I already did it using read, case, and grep like so:
#!/bin/ksh
echo -n "Please enter D(irectory), F(ile), or L(ink)"
read choice
case $choice in
[d,D])
cat directory.lst | grep 'drw*' | awk '{print $8}';;
[f,F])
cat directory.lst | grep -e '-rw*' | awk '{print $8}';;
[l,L])
cat directory.lst | grep 'lrw*' | awk '{print $8}';;
esac
But I was thinking maybe awk or sed or both could save me a lot of typing as they did before
Another thing is that for a linked list (e.g. lrwxrwxrwx 1 root root 13 2008-09-30 08:41 /usr/local/bin/perl -> /usr/bin/perl) the script must print the whole name with a link like: /usr/local/bin/perl -> /usr/bin/perl
Please help...
Last edited by mmahulo; 01-21-2009 at 07:50 AM.
|
|
|
01-21-2009, 05:57 AM
|
#5
|
LQ Guru
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509
|
Quote:
Originally Posted by mmahulo
But I was thinking maybe awk or sed or both could save me a lot of typing as they did before
|
To me your shell script is enough. I don't see any advantage by using awk or sed in this case. Obviously it all depends on your learning process. Is your course based on pure ksh or do the scope span over a more advanced usage of awk and sed?
In any case... well done!  Just a little note: you can avoid the echo statement when using the read command, since read can prompt the user directly with a question. An example to clarify:
Code:
read choice?"Please enter D(irectory), F(ile), or L(ink) "
# This is for the Korn shell KSH
The question mark separate the name of the variable to read from the message used to prompt the user. This is valid in KSH only, whereas in BASH the syntax is different (it uses option -p):
Code:
read -p "Please enter D(irectory), F(ile), or L(ink) " choice
# This is for the BASH shell
Last edited by colucix; 01-21-2009 at 05:58 AM.
|
|
|
01-22-2009, 12:53 AM
|
#6
|
Member
Registered: Nov 2008
Posts: 31
Original Poster
Rep:
|
Another thing is that for a linked list (e.g. lrwxrwxrwx 1 root root 13 2008-09-30 08:41 /usr/local/bin/perl -> /usr/bin/perl) the script must print the whole name with a link like: /usr/local/bin/perl -> /usr/bin/perl
Please help...
And thanks colocix for your post. it saves me a lot of typing.
Last edited by mmahulo; 01-22-2009 at 12:55 AM.
|
|
|
01-22-2009, 01:35 AM
|
#7
|
LQ Guru
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509
|
Try to figure it out. In case of L selection you can just modify the awk statement...
Another clue I didn't notice before: when you specify a list of characters using the square brackets syntax, you don't have to put a comma between items, otherwise the comma is included in the characters list. What happens if you answer "," (without quotes) when prompted?
Moreover, an alternative to the character list is the logical OR:
Code:
case $choice in
d | D)
cat blah blah blah
|
|
|
All times are GMT -5. The time now is 11:03 PM.
|
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
|
|