LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   Help with bash shell script (https://www.linuxquestions.org/questions/linux-general-1/help-with-bash-shell-script-4175430400/)

summer 10-03-2012 10:46 PM

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

nugat 10-03-2012 11:35 PM

Quote:

Originally Posted by summer (Post 4796630)
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

summer 10-03-2012 11:45 PM

#!/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

Ztcoracat 10-04-2012 12:00 AM

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

nugat 10-04-2012 12:17 AM

Quote:

Originally Posted by summer (Post 4796651)
#!/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.

summer 10-04-2012 12:23 AM

Thank You very much Nugat..

Ztcoracat 10-04-2012 12:27 AM

Summer & Nugat:

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

Code:

grep-o "$2" $1 > $3

nugat 10-04-2012 12:52 AM

Quote:

Originally Posted by Ztcoracat (Post 4796679)
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

Ztcoracat 10-04-2012 12:58 AM

Thanks got it!

Code:

grep - o "$2" $1 > $3
;)

nugat 10-04-2012 07:58 PM

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"!)

Ztcoracat 10-05-2012 12:19 AM

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!

nugat 10-05-2012 08:18 PM

Quote:

Originally Posted by Ztcoracat (Post 4797743)
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.

Ztcoracat 10-05-2012 09:18 PM

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!

chrism01 10-07-2012 09:10 PM

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


All times are GMT -5. The time now is 03:58 AM.