LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   shell scripting - with 3 parameters - help (https://www.linuxquestions.org/questions/linux-newbie-8/shell-scripting-with-3-parameters-help-886541/)

supernova88 06-15-2011 11:40 AM

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.

Tinkster 06-15-2011 11:54 AM

You didn't explain very well what you're trying to achieve.

Can you please try again?


Cheers,
Tink

supernova88 06-15-2011 12:39 PM

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 :)

tredegar 06-15-2011 02:25 PM

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:

for example
findit drum

will return something like


"we found drum in 2 lines
Might be more useful to have it reply:
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.

sandwormusmc 06-15-2011 03:16 PM

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

# Set your PATH variable for added security
PATH=/usr/bin:/bin:/usr/sbin
# Change the below variable accordingly, or even make it an argument to your script
DIRTOSEARCH='/var/www/html'

# You actually don't need to "read" any input if you are already using arguments ... the below if statement should only be used if there are no arguments passed to the script
if [ $# -eq 0 ]; then
  # Don't use dollar signs here, otherwise you'll have a boatload of problems
  # I always make my variables in UPPER CASE to easily identify them from commands and bash syntax
  echo " write one two or three words"
  # Put your read statement after your basic sanity checks, otherwise you'll be wasting time reading input
  read WORD1 QUALIFIER WORD3
  # This is a matter of style, but I usually put my "if" and "then" statements on the same line, notice the semicolon that separates them
  echo "ERROR"
  # Indentation is key if you want your scripts to be readable
  # You need an "fi" at the end of every if statement
  # Add an "exit" statement if you want your script to crap out
  exit
elif [ $# -lt 1 -o $# -gt 3 ]; then
  # So we've ruled out the fact that the script was run without any arguments, and that there were the correct amount of arguments given
  # Now let's create the grep expression that will complete what we need
  # Whenever I'm referencing a variable, I encase it in curly braces (just got used to doing so to avoid syntax errors)
  # Study up on the case statement, it's incredibly useful.  Note that you need a ";;" at the end of every matching case statement
  case ${QUALIFIER} in
    not|NOT) CMD="grep -r ${1} ${DIRTOSEARCH} | grep -v ${3}";;
    and|AND) CMD="grep -r ${1} ${DIRTOSEARCH} | grep ${3}";;
    '-v') ;;
  esac
fi

# The below runs the appropriate "grep" command, redirects all of the output to /dev/null (throws away the output)
# You can remove everything after the last } (curly brace) to experiment with how you want your output to show up ... grep will show the matching lines otherwise
# Or, if you want to show only the filenames that match your search, replace all the "grep" commands above (except the one with the -v in it), and remove the redirect to dev null and 2>&1
${CMD} ${DIRTOSEARCH} >/dev/null 2>&1

# Next we'll check the return code of the above command ... read up on return codes, they're also very useful, but they are different for every command you run, so be careful
# For grep, 0 means your search was found
case $? in
  0)
    echo 'Found something!';
  *)
    # Read up on 'escaping' characters if you don\'t know about it ...
    echo 'Didn\'t find anything. :(';
esac


chrism01 06-15-2011 06:28 PM

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

szboardstretcher 06-16-2011 03:05 PM

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.