LinuxQuestions.org
Review your favorite Linux distribution.
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 02-21-2010, 03:03 PM   #1
Porch
LQ Newbie
 
Registered: Feb 2010
Location: Los Alamos, NM
Distribution: Mint
Posts: 10

Rep: Reputation: 0
Setting multiple switches in grep (bash script)


Hello,
My first post!
Without posting my whole long script, i'm working on setting multiple switches in grep.

Here's an example of how to run my script:
scriptname root daemon

would (should) produce the results of this:
ps aux | grep -i -e root -e daemon
(I could have 1 input, a thousand inputs, etc.)

Right now i have some code to catch the inputs, using a variable "z":
Code:
z=""

[Code to catch inputs, for each input:]
z="${z} -e ${1}"
shift 1
;;
Then:
Code:
ps aux | grep -i $z
I'm almost positive that the problem is in the bolded line above (for one thing, it always leaves off the initial "-e"). So basically i want a string that gives me "-e input" and concatenates as many times as necessary.

I've been working on this darn thing all morning and it's the last hurdle before i turn this assignment in!
Thanks in advance for the help!
~Chris
 
Old 02-21-2010, 03:59 PM   #2
micxz
Senior Member
 
Registered: Sep 2002
Location: CA
Distribution: openSuSE, Cent OS, Slackware
Posts: 1,131

Rep: Reputation: 75
How about something like:

for each $#;
do ps aux | grep -i $1 , $2 , $3 and so on..
 
Old 02-21-2010, 04:04 PM   #3
Porch
LQ Newbie
 
Registered: Feb 2010
Location: Los Alamos, NM
Distribution: Mint
Posts: 10

Original Poster
Rep: Reputation: 0
That's a good thought, but to be honest i left out a little bit of my assignment. My actual command line usage is this:

scriptname [patterns] [-t seconds] [-n count]

So it is possible that $2, $3, etc. may be related to the "time" or "count" functions. Thanks though
 
Old 02-21-2010, 04:06 PM   #4
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,358
Blog Entries: 55

Rep: Reputation: 3545Reputation: 3545Reputation: 3545Reputation: 3545Reputation: 3545Reputation: 3545Reputation: 3545Reputation: 3545Reputation: 3545Reputation: 3545Reputation: 3545
In your case you may want to string together all search terms, replace spaces with pipe sign then egrep for it: 'SEARCH_TERMS="${SEARCH_TERMS} $NEW_SEARCH_TERM"; "${SEARCH_TERMS}=${SEARCH_TERMS// /|}"; ps|egrep -i "(${SEARCH_TERMS})";'? But instead of using 'ps aux | grep' why not use 'pgrep'? 'pgrep' with "-f" accepts multiple search terms like '(term0|term1)' and can grep for UIDs as well so you can 'pgrep -lU root -f '(kj|bin)';'. As for the parameters see 'getopts'?
 
Old 02-21-2010, 04:33 PM   #5
Porch
LQ Newbie
 
Registered: Feb 2010
Location: Los Alamos, NM
Distribution: Mint
Posts: 10

Original Poster
Rep: Reputation: 0
Thanks, unspawn! I think i'm headed in the right direction.

To answer your question, i can't use pgrep because i don't know if the input is going to be a user, or a process, or...? It could be anything!

But the rest of your response makes perfect sense, and replacing the spaces with a | is a great idea, thanks! I'm having some problems getting grep or egrep to take a search in the form (this|that|someother), though. It looks like it's been deprecated?

UPDATE: Okay, i got it working using:
Code:
egrep 'this|that'
Fixing other little things here and there, will get back to you when i'm finished

EDIT: D'oh. Okay, so i changed that to
Code:
egrep "this|that"
...because passing it as egrep '${z}' passes ${z} instead of this|that.

Also, i ran into a problem where the first blank character was being converted to a pipe as well, and found the following code to fix that:
Code:
var="${var#"${var%%[![:space:]]*}"}"
Thanks again!!

Last edited by Porch; 02-21-2010 at 05:21 PM.
 
1 members found this post helpful.
Old 02-21-2010, 05:14 PM   #6
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.9, Centos 7.3
Posts: 17,395

Rep: Reputation: 2395Reputation: 2395Reputation: 2395Reputation: 2395Reputation: 2395Reputation: 2395Reputation: 2395Reputation: 2395Reputation: 2395Reputation: 2395Reputation: 2395
Definitely getopts as per UnSpawn
 
  


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
bash script grep without double saavik Programming 2 05-14-2009 10:15 AM
Bash: Script with tail and grep Primsi Linux - General 2 11-16-2006 05:30 AM
bash script with grep and sed: sed getting filenames from grep odysseus.lost Programming 1 07-17-2006 11:36 AM
bash script and grep syros Programming 4 01-13-2005 03:04 PM
Creating switches in a bash script Moto_Jon Programming 2 07-28-2004 06:06 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 08:06 AM.

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