Latest LQ Deal: Latest LQ Deals
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!


  Search this Thread
Old 06-15-2011, 11:40 AM   #1
LQ Newbie
Registered: Jun 2011
Posts: 7

Rep: Reputation: Disabled
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 :



echo " write one two or three words"
read $word1 $word2 $word3

if [ $# -lt 1 -o $# -gt 3 ]
echo "ERROR"

if [ "$1 ] -a [ "$2" ]; found
echo "found $word1 and $word22"
echo "not found"

[ "$word1" ] o [ "$word2" ]; are found
echo "found $1 or $2"
echo "word not found"


I am new in scrip[ting so bare with my mistakes, but could anyone kindly lend a helping hand.
Old 06-15-2011, 11:54 AM   #2
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 914Reputation: 914Reputation: 914Reputation: 914Reputation: 914Reputation: 914Reputation: 914Reputation: 914
You didn't explain very well what you're trying to achieve.

Can you please try again?

Old 06-15-2011, 12:39 PM   #3
LQ Newbie
Registered: Jun 2011
Posts: 7

Original Poster
Rep: Reputation: Disabled
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
Old 06-15-2011, 02:25 PM   #4
LQ 5k Club
Registered: May 2003
Location: London, UK
Distribution: Debian "Testing"
Posts: 6,112

Rep: Reputation: 416Reputation: 416Reputation: 416Reputation: 416Reputation: 416
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.

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.
Old 06-15-2011, 03:16 PM   #5
Registered: Nov 2006
Distribution: Fedora 15 x86_64
Posts: 76

Rep: Reputation: 24

Comments and various scripting pointers are in the script ... the below should get you off the ground and walking at least. Good luck.


# Set your PATH variable for added security
# Change the below variable accordingly, or even make it an argument to your script

# 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
   # 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
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') ;;

# 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
     echo 'Found something!';
     # Read up on 'escaping' characters if you don\'t know about it ...
     echo 'Didn\'t find anything. :(';
Old 06-15-2011, 06:28 PM   #6
LQ Guru
Registered: Aug 2004
Location: Sydney
Distribution: Centos 7.7 (?), Centos 8.1
Posts: 17,774

Rep: Reputation: 2537Reputation: 2537Reputation: 2537Reputation: 2537Reputation: 2537Reputation: 2537Reputation: 2537Reputation: 2537Reputation: 2537Reputation: 2537Reputation: 2537
As per Tredegar, here are some good tutorials on bash

Might I specifically suggest you have a good look at the getopts cmd eg
Old 06-16-2011, 03:05 PM   #7
Senior Member
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 4,237

Rep: Reputation: 1654Reputation: 1654Reputation: 1654Reputation: 1654Reputation: 1654Reputation: 1654Reputation: 1654Reputation: 1654Reputation: 1654Reputation: 1654Reputation: 1654
What class is this for? I seem to remember something like this from a recent course.

I went here to find everything that I needed for it.


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

Similar Threads
Thread Thread Starter Forum Replies Last Post
Scripting Question - multiple {parameters} and variables gchilders Linux - Newbie 3 10-08-2008 08:31 AM
LXer: Terminal functions for shell scripting with Shell Curses LXer Syndicated Linux News 0 03-26-2008 11:50 PM
shell scripting/ bash/ a command with 3 parameters mayaabboud Programming 4 01-05-2008 10:22 AM
teaching shell scripting: cool scripting examples? fax8 Linux - General 1 04-20-2006 04:29 AM
shell: positional parameters... neidias Programming 1 04-04-2005 02:45 PM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 07:53 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration