LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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!

Notices


Reply
  Search this Thread
Old 06-15-2011, 12:40 PM   #1
supernova88
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 :

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.
 
Old 06-15-2011, 12:54 PM   #2
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,066
Blog Entries: 11

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

Can you please try again?


Cheers,
Tink
 
Old 06-15-2011, 01:39 PM   #3
supernova88
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, 03:25 PM   #4
tredegar
LQ 5k Club
 
Registered: May 2003
Location: London, UK
Distribution: Debian "Jessie"
Posts: 6,085

Rep: Reputation: 398Reputation: 398Reputation: 398Reputation: 398
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.
 
Old 06-15-2011, 04:16 PM   #5
sandwormusmc
Member
 
Registered: Nov 2006
Distribution: Fedora 15 x86_64
Posts: 76

Rep: Reputation: 24
Smile

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
 
Old 06-15-2011, 07:28 PM   #6
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,240

Rep: Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324
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
 
Old 06-16-2011, 04:05 PM   #7
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 3,816

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


Reply


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 09:31 AM
LXer: Terminal functions for shell scripting with Shell Curses LXer Syndicated Linux News 0 03-27-2008 12:50 AM
shell scripting/ bash/ a command with 3 parameters mayaabboud Programming 4 01-05-2008 11:22 AM
teaching shell scripting: cool scripting examples? fax8 Linux - General 1 04-20-2006 05:29 AM
shell: positional parameters... neidias Programming 1 04-04-2005 03:45 PM


All times are GMT -5. The time now is 10:37 PM.

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