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... |
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? |
As pixellany intimated, bash should be sufficient really. Try this link: http://www.tldp.org/LDP/abs/html/
|
Quote:
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... |
Quote:
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) " Code:
read -p "Please enter D(irectory), F(ile), or L(ink) " choice |
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: Code:
case $choice in |
All times are GMT -5. The time now is 05:12 AM. |