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 11-11-2005, 04:34 AM   #1
qerf
LQ Newbie
 
Registered: Mar 2004
Posts: 13

Rep: Reputation: 0
shell programming


hi,
Problem is like that
I have to write a shell script. Script will get many arguments. first is file name and the others are strings which script search them in a file.
for example my file name is shell.txt and it contains

"I have to write a shell script.
Script will get many arguments. first is
file name and the others are strings
which script search them in file."

./search shell.txt script strings and first
then results must bu like that:
script 3 times at lines 1,2,4
strings 1 time at line 2
and 1 time at line 3
first 1 time at line 2

i am new to shell programming. i search a bit and i decide grep command may help me but i think its useful to ask my questions to experts. is shell scripting have capability reading and writing files like c. open a file for reading and then getting chars etc...
or i have to use linux commands like grep to solve this problem.
help me a bit. which commands do i have to insist on.
 
Old 11-11-2005, 05:12 AM   #2
marghorp
Senior Member
 
Registered: Jan 2004
Location: Slovenia
Distribution: Slackware 10.1, SLAX to the MAX :)
Posts: 1,040

Rep: Reputation: 45
Smells like homework to me
 
Old 11-11-2005, 05:42 AM   #3
bigjohn
Senior Member
 
Registered: Jun 2002
Location: UK .
Distribution: *buntu (usually Kubuntu)
Posts: 2,682
Blog Entries: 9

Rep: Reputation: 45
Quote:
Originally posted by marghorp
Smells like homework to me
Well if thats right, then it's one hell of a home work topic. When I was at school, that kind of stuff was left up to the post graduate "pin striped nazis" in big corporations in those spooky looking 70's pictures of mega main frame systems
 
Old 11-11-2005, 05:57 AM   #4
scuzzman
Senior Member
 
Registered: May 2004
Location: Hilliard, Ohio, USA
Distribution: Slackware, Kubuntu
Posts: 1,851

Rep: Reputation: 47
A shell script is a collection of commands in a single file - thus, your script will not handle the opening/closing of files. You'll want to look into the following commands:
grep
cat
sed
 
Old 11-11-2005, 06:05 AM   #5
qerf
LQ Newbie
 
Registered: Mar 2004
Posts: 13

Original Poster
Rep: Reputation: 0
yes it is true, it is homework but i dont want to complete solution. Only tips pls...
 
Old 11-11-2005, 06:18 AM   #6
blindcoder
ROCK Linux
 
Registered: Dec 2003
Location: Berlin, Germany
Distribution: Crystal ROCK
Posts: 108

Rep: Reputation: 15
Quote:
Originally posted by qerf
yes it is true, it is homework but i dont want to complete solution. Only tips pls...
In that case, grep is not what you want. Neither is cat or sed. What you want is the read command:
Code:
       read [-ers] [-u fd] [-t timeout] [-a aname] [-p prompt] [-n nchars] [-d
       delim] [name ...]
              One  line  is  read  from  the  standard input, or from the file
              descriptor fd supplied as an argument to the -u option, and  the
              first word is assigned to the first name, the second word to the
              second name, and so on, with leftover words and their  interven-
              ing  separators  assigned  to the last name.  If there are fewer
              words read from the input stream than names, the remaining names
              are  assigned  empty  values.  The characters in IFS are used to
              split the line into words.  The backslash character (\)  may  be
              used  to  remove any special meaning for the next character read
              and for line continuation.  Options, if supplied, have the  fol-
              lowing meanings:
              -a aname
                     The words are assigned to sequential indices of the array
                     variable aname, starting at 0.  aname is unset before any
                     new  values  are  assigned.   Other  name  arguments  are
                     ignored.
              -d delim
                     The first character of delim is  used  to  terminate  the
                     input line, rather than newline.
              -e     If the standard input is coming from a terminal, readline
                     (see READLINE above) is used to obtain the line.
              -n nchars
                     read returns after reading nchars characters rather  than
                     waiting for a complete line of input.
              -p prompt
                     Display prompt on standard error, without a trailing new-
                     line, before attempting to read any input.  The prompt is
                     displayed only if input is coming from a terminal.
              -r     Backslash does not act as an escape character.  The back-
                     slash is considered to be part of the line.  In  particu-
                     lar,  a  backslash-newline pair may not be used as a line
                     continuation.
              -s     Silent mode.  If input is coming from a terminal, charac-
                     ters are not echoed.
              -t timeout
                     Cause  read  to time out and return failure if a complete
                     line of input is not read within timeout  seconds.   This
                     option  has  no  effect if read is not reading input from
                     the terminal or a pipe.
              -u fd  Read input from file descriptor fd.

              If no names are supplied, the line read is assigned to the vari-
              able  REPLY.   The  return  code  is zero, unless end-of-file is
              encountered, read times out, or an invalid  file  descriptor  is
              supplied as the argument to -u.
As well as using a while loop:

Code:
while read line ; do
...
done < file
Of course, you still have to break up the variable line into smaller parts which is left as an excercise to you (hint: only requires a for-loop).

Good luck,
Benjamin
 
Old 11-11-2005, 06:25 AM   #7
qerf
LQ Newbie
 
Registered: Mar 2004
Posts: 13

Original Poster
Rep: Reputation: 0
thank you blindcoder. as i said my problem is that i am not familiar to linux so i dont know which command i have to use. i can only find grep but no clue about read. so
i will insist on read. i can handle syntax solution logic and others thank you again. i only need a bit technical support.
can anybody suggest me bash tutorials book or web page
 
Old 11-11-2005, 06:30 AM   #8
blindcoder
ROCK Linux
 
Registered: Dec 2003
Location: Berlin, Germany
Distribution: Crystal ROCK
Posts: 108

Rep: Reputation: 15
Quote:
Originally posted by qerf
can anybody suggest me bash tutorials book or web page
Sign up to your favorite newsserver, subscribe to comp.unix.shell and look for Message-ID: <bash2_1130770800@news.cwru.edu>

It's the comp.unix.shell bash FAQ 3.30.

Greetings,
Benjamin
 
Old 11-11-2005, 08:30 PM   #9
shengchieh
Member
 
Registered: Jul 2004
Location: Palo Alto, CA
Distribution: #! Korora
Posts: 472

Rep: Reputation: 30
> can anybody suggest me bash tutorials book or web page

A whole list of links at

http://www.websamba.com/Linux_Docs_Links
-> console commands
-> general & references

Sheng-Chieh
 
Old 11-12-2005, 06:27 PM   #10
Dtsazza
Member
 
Registered: Oct 2005
Location: Oxford, UK
Distribution: Debian Etch (w/ dual-boot XP for gaming)
Posts: 282

Rep: Reputation: 30
And, as always, I'll recommend the Linux Documentation Project as an excellent source of info - particularly their books on Bash (both available from the guides page, along with much other useful info).
 
  


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
Shell programming quan Programming 8 11-27-2005 08:46 PM
shell programming qerf Linux - Newbie 5 11-06-2005 06:28 PM
programming shell tuls Slackware 3 08-24-2005 06:58 PM
new to shell programming bluetwist Debian 3 08-01-2005 04:56 PM
Shell Programming eantoranz Linux - Networking 2 06-02-2005 12:21 PM


All times are GMT -5. The time now is 10:57 PM.

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