Linux - NewbieThis 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.
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.
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...
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
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
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.
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:
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.