LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Reply
 
Search this Thread
Old 10-03-2012, 10:46 PM   #1
summer
LQ Newbie
 
Registered: Oct 2012
Posts: 12

Rep: Reputation: Disabled
Help with bash shell script


Need to create a bash shell script

The script should accept three command-line arguments: the name of an input file, a word to search for, and the name of an output file.
The script should search the specified input file for the specified word, and put the search results into the specified output file.
For example, if I call the script like this:
FileName content.sh read outputFile.txt

... the script should search for the word read in the file called content.sh, and put the search results into the file called outputFile.txt
 
Old 10-03-2012, 11:35 PM   #2
nugat
Member
 
Registered: Sep 2012
Posts: 122

Rep: Reputation: 31
Quote:
Originally Posted by summer View Post
Need to create a bash shell script

The script should accept three command-line arguments: the name of an input file, a word to search for, and the name of an output file.
The script should search the specified input file for the specified word, and put the search results into the specified output file.
For example, if I call the script like this:
FileName content.sh read outputFile.txt

... the script should search for the word read in the file called content.sh, and put the search results into the file called outputFile.txt
Okay...and what have you come up so far? Show us your code, or explain where you are stuck, with specific details. Otherwise, it sounds like you are asking us to do your homework for you.

Some tips:

read the bash man page ("man bash")

read up on bash position parameters ($0, $1, etc.)

you can use either the grep or awk GNU command to search

in bash, you can use the ">" character to redirect program output to a file
">>" appends to a file

you can use the GNU cat program to read a file
 
Old 10-03-2012, 11:45 PM   #3
summer
LQ Newbie
 
Registered: Oct 2012
Posts: 12

Original Poster
Rep: Reputation: Disabled
#!/bin/bash

if test $# -ne 3 #Error check
then
echo "you must provide 3 inputs. Good Bye"
exit
fi
grep -c "$2" $1 > $3

so far i did this.its coming out to be alright.but wanted to make sure whether it is completely right or not.

Thanks
 
Old 10-04-2012, 12:00 AM   #4
Ztcoracat
Senior Member
 
Registered: Dec 2011
Distribution: Slackware, CentOS & Android
Posts: 3,174
Blog Entries: 1

Rep: Reputation: Disabled
This Bash Guide will help you and confirm a lot of things.
I've been studying it for days and it's a good Guide.

http://mywiki.wooledge.org/BashGuide/Parameters
 
Old 10-04-2012, 12:17 AM   #5
nugat
Member
 
Registered: Sep 2012
Posts: 122

Rep: Reputation: 31
Quote:
Originally Posted by summer View Post
#!/bin/bash

if test $# -ne 3 #Error check
then
echo "you must provide 3 inputs. Good Bye"
exit
fi
grep -c "$2" $1 > $3

so far i did this.its coming out to be alright.but wanted to make sure whether it is completely right or not.
well, that's a pretty good start. the main thing is that the "-c" will tell grep to not print the search string it found, but rather a count. you said you wanted to print what you found, so leave that off.

if you want to literally only print the search string itself, and not the entire line in which the search string is found, then you can use the "-o" grep option.
 
1 members found this post helpful.
Old 10-04-2012, 12:23 AM   #6
summer
LQ Newbie
 
Registered: Oct 2012
Posts: 12

Original Poster
Rep: Reputation: Disabled
Thank You very much Nugat..
 
Old 10-04-2012, 12:27 AM   #7
Ztcoracat
Senior Member
 
Registered: Dec 2011
Distribution: Slackware, CentOS & Android
Posts: 3,174
Blog Entries: 1

Rep: Reputation: Disabled
Summer & Nugat:

If you don't mind me asking; like this?

Code:
grep-o "$2" $1 > $3
 
Old 10-04-2012, 12:52 AM   #8
nugat
Member
 
Registered: Sep 2012
Posts: 122

Rep: Reputation: 31
Quote:
Originally Posted by Ztcoracat View Post
Summer & Nugat:

If you don't mind me asking; like this?

Code:
grep-o "$2" $1 > $3
almost, put a space after "grep" and before "-o", e.g.:
Code:
grep -o "$2" $1 > $3
 
Old 10-04-2012, 12:58 AM   #9
Ztcoracat
Senior Member
 
Registered: Dec 2011
Distribution: Slackware, CentOS & Android
Posts: 3,174
Blog Entries: 1

Rep: Reputation: Disabled
Thanks got it!

Code:
grep - o "$2" $1 > $3
 
Old 10-04-2012, 07:58 PM   #10
nugat
Member
 
Registered: Sep 2012
Posts: 122

Rep: Reputation: 31
Quote:
Code:
grep - o "$2" $1 > $3
actually, as you have a space in between the dash and the "o", it grep will look for a "-" in a file called "o", as well as in $2 and $1. The -o is a parameter, or option, and should never have a space after the dash. this is true of pretty much all commands that take options. take a look at the grep man page ("man grep") and you can see each the parameters listed there.

you can also do "grep --help" for a quick list of options (but don't do "grep -- help"!)
 
Old 10-05-2012, 12:19 AM   #11
Ztcoracat
Senior Member
 
Registered: Dec 2011
Distribution: Slackware, CentOS & Android
Posts: 3,174
Blog Entries: 1

Rep: Reputation: Disabled
I'll look in the terminal and type man grep and do some reading.

I didn't even know that there was grep --help.........Thanks!
 
Old 10-05-2012, 08:18 PM   #12
nugat
Member
 
Registered: Sep 2012
Posts: 122

Rep: Reputation: 31
Quote:
Originally Posted by Ztcoracat View Post
I didn't even know that there was grep --help
Yeah, with pretty much any GNU program, and really most UNIX/Linux commands, you can run "--help" (or "-h" or "--usage") to get some helpful output quickly printed to the screen.

so between doing "man <command>" and "<command> --help", you're well on your way!

it should also be mentioned that there is the "info" command, which is similar to man pages, but often times has more details. there seems to be less info pages, but some commands only have info pages and not man ones, so you can always try "info <command>", too.

one final note: many packages install documentation to whatever the standard doc dir is on your system. often times, that is /usr/share/doc. so it is worth your while to take a peek there, too.
 
Old 10-05-2012, 09:18 PM   #13
Ztcoracat
Senior Member
 
Registered: Dec 2011
Distribution: Slackware, CentOS & Android
Posts: 3,174
Blog Entries: 1

Rep: Reputation: Disabled
Thanks Nugat!

You have been very helpful and I have good info. to learn from.
I'll peek in /usr/share/doc and see what I find there too.

Have a good weekend!
 
Old 10-07-2012, 09:10 PM   #14
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,287

Rep: Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034
See also http://rute.2038bug.com/index.html.gz
 
  


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 On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Calling a bash shell script from within another script dedman Linux - Software 7 04-24-2010 08:53 PM
in bash shell how to run shell script during startup rammohan04 Red Hat 2 07-31-2009 02:07 AM
bash shell script globeTrotter Linux - Newbie 5 06-03-2004 05:07 AM
BASH shell script help ewarmour Programming 8 05-24-2002 07:57 AM
bash shell script MaryM Linux - Newbie 0 02-15-2002 11:45 PM


All times are GMT -5. The time now is 01:08 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration