LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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


Reply
  Search this Thread
Old 01-20-2009, 07:46 AM   #1
mmahulo
Member
 
Registered: Nov 2008
Posts: 31

Rep: Reputation: 15
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.
 
Old 01-20-2009, 08:17 AM   #2
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
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?
 
Old 01-20-2009, 07:41 PM   #3
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.9, Centos 7.3
Posts: 17,374

Rep: Reputation: 2383Reputation: 2383Reputation: 2383Reputation: 2383Reputation: 2383Reputation: 2383Reputation: 2383Reputation: 2383Reputation: 2383Reputation: 2383Reputation: 2383
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
 
Old 01-21-2009, 04:35 AM   #4
mmahulo
Member
 
Registered: Nov 2008
Posts: 31

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by pixellany View Post
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.
 
Old 01-21-2009, 05:57 AM   #5
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
Quote:
Originally Posted by mmahulo View Post
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.
 
Old 01-22-2009, 12:53 AM   #6
mmahulo
Member
 
Registered: Nov 2008
Posts: 31

Original Poster
Rep: Reputation: 15
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.
 
Old 01-22-2009, 01:35 AM   #7
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
-sh: CVS_RSH=ssh: is not an identifier rose_bud4201 Linux - Software 1 04-11-2007 05:22 AM
Create a script to display file name, Inode, and size of any file. Has to be a script JaxsunApex Linux - Newbie 7 01-29-2007 08:15 PM
Processor Chip Identifier? WindyT General 9 07-27-2005 05:39 PM
Video Card's bus identifier? zeroz52 Debian 2 02-26-2005 03:01 PM
ERROR: not an identifier GEEXTER Linux - Newbie 0 12-16-2002 11:27 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 08:50 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration