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 |
Quote:
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 |
#!/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 |
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 |
Quote:
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. |
Thank You very much Nugat..
|
Summer & Nugat:
If you don't mind me asking; like this? Code:
grep-o "$2" $1 > $3 |
Quote:
Code:
grep -o "$2" $1 > $3 |
Thanks got it!
Code:
grep - o "$2" $1 > $3 |
Quote:
you can also do "grep --help" for a quick list of options (but don't do "grep -- help"!) |
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! |
Quote:
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. |
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! |
|
All times are GMT -5. The time now is 03:58 AM. |