LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   File identifier script (https://www.linuxquestions.org/questions/linux-newbie-8/file-identifier-script-698586/)

mmahulo 01-20-2009 07:46 AM

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...

pixellany 01-20-2009 08:17 AM

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?

chrism01 01-20-2009 07:41 PM

As pixellany intimated, bash should be sufficient really. Try this link: http://www.tldp.org/LDP/abs/html/

mmahulo 01-21-2009 04:35 AM

Quote:

Originally Posted by pixellany (Post 3414980)
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...

colucix 01-21-2009 05:57 AM

Quote:

Originally Posted by mmahulo (Post 3415971)
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


mmahulo 01-22-2009 12:53 AM

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.

colucix 01-22-2009 01:35 AM

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 05:12 AM.