shell scripting - with 3 parameters - help
Hello all,
I am struggling with writing this type of script file in which I would need to run 3 parameters by it. Allow me to explain in further detail, I am trying to make a shell script that would search a load of html or txt files but with using only 3 parameters. So for instance lets say if i were to say search "jack john and karla" or "john or karla" i would want my shell script to go through searching the files for those keywords and if not able to find to echo an error message. This si what I have so far : cod: _________ #!/bin/sh echo " write one two or three words" read $word1 $word2 $word3 if [ $# -lt 1 -o $# -gt 3 ] then echo "ERROR" if [ "$1 ] -a [ "$2" ]; found then echo "found $word1 and $word22" else echo "not found" elif [ "$word1" ] o [ "$word2" ]; are found then echo "found $1 or $2" else echo "word not found" ___ I am new in scrip[ting so bare with my mistakes, but could anyone kindly lend a helping hand. |
You didn't explain very well what you're trying to achieve.
Can you please try again? Cheers, Tink |
sorry about that , ill try to be more clearer with what i am asking:
i want to write a script lets say i call it "findit" that accept a keyword as a command line parameter and finds all the lines in all the html files or txt files in the current directory containing the keyword. and would like to display the numbe rof lines in a readable format. for example findit drum will return something like "we found drum in 2 lines and i would like to adjust the script to accept 1,2,3 parameters, any attempt to run the script with less then 1 or more than 3 , you will get an error message for instance, if 1 parameter exists we would assume we'd be looking for that word if 2 parameters exist we would be looking for lines that have both keywords "this AND that" if 3 parameters exist test the 2nd one for the values: and - lines having both words ex: fidnit tall and red or - lines having one or other words - ex findit tall or red -v - lines having the first parameter and not the second - findit red -v tall these are the only accepted as the second parameter when 3 parameters are present . anything else would be displayed in error message. i hope ive explained it into more detail for you :) |
Your posted bash script (at post #1) is a good start, but full of syntactical and logical errors.
You should read a bash scripting tutorial. There are many on the net, or you can just buy a book (I'm old, so prefer those tree-based-things). My suggestion is to start very simply: Write a script that'll search for files containing only one keyword and print the results. Test it out. Then work out the syntax of how you are going to write the script to handle two keywords, and decide how you are going to parse firstword and secondword or firstword or secondword. Quote:
We found "drum" in these files: ( /full/path/to/filenames list follows ..) So please start very simply, and then work forwards. You might like to read, absorb, and sleep on, the output of man grep You are welcome to post the scripts you have tried, with the errors you encounter, and we'll do our best to help you. But you have to do your homework and read up on the basics. bash can be furiously complicated (as I know to my cost), but you'll probably get acquainted with it. Best wishes. |
Comments and various scripting pointers are in the script ... the below should get you off the ground and walking at least. Good luck.
Code:
#!/bin/sh |
As per Tredegar, here are some good tutorials on bash
http://rute.2038bug.com/index.html.gz http://tldp.org/LDP/Bash-Beginners-G...tml/index.html http://www.tldp.org/LDP/abs/html/ Might I specifically suggest you have a good look at the getopts cmd eg http://bashcurescancer.com/the-60-se...-tutorial.html |
What class is this for? I seem to remember something like this from a recent course.
http://tldp.org/LDP/abs/html/ I went here to find everything that I needed for it. |
All times are GMT -5. The time now is 03:56 AM. |