LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 03-25-2005, 03:58 PM   #1
lowpro2k3
Member
 
Registered: Oct 2003
Location: Canada
Distribution: Slackware
Posts: 340

Rep: Reputation: 30
Bash assignment - using a grep inside an if statement


Me and bash arent getting along too well.

My script is supposed to read in a username option and display processes owned by that user. Example:

Code:
[prompt@box dir]$ ./ps+ -u rpcuser
rpcuser      642    0.0   0.1    1600   etc...
My script just sends the username flag to a ps aux | grep $USER_NAME command in the script. That works fine, but now I have to satisfy the requirement that if the user isnt found, it looks like this:

Code:
[prompt@box dir]$ ./ps+ -u foo
Not found!


When life was good an hour ago, I output a table row then ran the grep command. I spent alot of time formatting a printf statement to look the exact same as the ps aux table row. Now that I have this requirement i didnt notice, I'm trying to test if the ps aux | grep commands return anything, before I do any outputting (exiting if there is no user).

I decided to use the grep -q switch for my tests. It suppresses output and stops scanning after the first match. This seems (or seemed) ideal for my use. Note the simple regexp i use below, and how I use the grep -q at the cmd-line

Code:
[prompt@box dir]$ ps aux | grep -q "^root "
[prompt@box dir]$ echo $?
0
[prompt@box dir]$ ps aux | grep -q "^rootttteeer "
[prompt@box dir]$ echo $?
1


So in my script, I'm trying to say something like this (which isn't working properly)

Code:
testone=`ps aux | grep -q "^$USER_INPUT "`
if [ ! "testone" ]
then
    echo "Not found!"
    exit 1
fi


displayTableRow    # call function to output formatted table row
ps aux | grep "^$USER_INPUT "






# Pretend we're back at the cmd-line - running the script.
# This is the output I keep getting and I'm getting mad
[prompt@box dir]$ ./ps+ -u rpcuser
Not found!
[prompt@box dir]$ ./ps+ -u myusername
Not found!
[prompt@box dir]$ ./ps+ -u user_that_doesnt_exist
Not found!
[prompt@box dir]$ ./ps+ -u root
Not found!

I dont get it...

Last edited by lowpro2k3; 03-25-2005 at 04:02 PM.
 
Old 03-25-2005, 04:11 PM   #2
TheLinuxDuck
Member
 
Registered: Sep 2002
Location: Tulsa, OK
Distribution: Slack, baby!
Posts: 349

Rep: Reputation: 33
In the []'s, the variable name is missing the $. (=

But, try this test, instead:

Code:
if [ "$?" == "1" ]; then
  echo "Not found!"
  exit 1
fi

Last edited by TheLinuxDuck; 03-25-2005 at 04:14 PM.
 
Old 03-25-2005, 04:27 PM   #3
lowpro2k3
Member
 
Registered: Oct 2003
Location: Canada
Distribution: Slackware
Posts: 340

Original Poster
Rep: Reputation: 30
Quote:
Originally posted by TheLinuxDuck
In the []'s, the variable name is missing the $. (=

Your right, but in my code I wasnt missing it.

Quote:
Originally posted by TheLinuxDuck
But, try this test, instead:

Code:
if [ "$?" == "1" ]; then
  echo "Not found!"
  exit 1
fi
That provided some interesting changes. Now I'm finding usernames again, but I'm never getting into the body of the if test.

Example:

Code:
[prompt@box dir]$ ./ps+ -u root
USER       PID %CPU %MEM   VSZ  RSS TTY      STAT START   TIME COMMAND
----------------------------------------------------------------------
root         1  0.0  0.0  1416  456 ?        S    Mar17   0:04 init
root         2  0.0  0.0     0    0 ?        SW   Mar17   0:00 [keventd]
root         3  0.0  0.0     0    0 ?        SW   Mar17   0:00 [kapmd]
root         4  0.0  0.0     0    0 ?        SWN  Mar17   0:00 [ksoftirqd_CPU0]
root         5  0.0  0.0     0    0 ?        SW   Mar17   0:00 [kswapd]
root         6  0.0  0.0     0    0 ?        SW   Mar17   0:00 [kscand/DMA]
root         7  0.0  0.0     0    0 ?        SW   Mar17   3:24 [kscand/Normal]
root         8  0.0  0.0     0    0 ?        SW   Mar17   0:00 [kscand/HighMem]
etc...
[prompt@box dir]$ ./ps+ -u rooooooooooooot
USER       PID %CPU %MEM   VSZ  RSS TTY      STAT START   TIME COMMAND
I tried a few variations on your syntax, and still can't get it. I also tried not using a variable, and simply plunking the ps aux | grep $USER_NAME command right into an if statement. I cant get the right functionality...

Also I'm using the textbook "UNIX Shells by Example, 4th edition (with Linux coverage)" I'm trying to find some examples in here as I try and debug this mess...
 
Old 03-25-2005, 04:39 PM   #4
TheLinuxDuck
Member
 
Registered: Sep 2002
Location: Tulsa, OK
Distribution: Slack, baby!
Posts: 349

Rep: Reputation: 33
The test I posted works, but only if the ps | grep line is the last comand executed right before checking the contents of $?. $? is reset if anything else occurs. here is a variation that protects the contents of $? by assigning it to testone:
Code:
#!/bin/bash

ps aux | grep -q "^$1 "
testone=$?

if [ "$testone" == "1" ]; then
    echo "Not found!"
    exit 1
fi
And in action:
Code:
~/bash> ./finduser.sh foo
Not found!
~/bash> ./finduser.sh root
~/bash>
 
Old 03-25-2005, 05:24 PM   #5
lowpro2k3
Member
 
Registered: Oct 2003
Location: Canada
Distribution: Slackware
Posts: 340

Original Poster
Rep: Reputation: 30
Smile

Bingo, thanks alot for your help!
 
  


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 statement os2 Programming 2 03-20-2005 10:13 PM
Can I use grep inside awk? Helene Programming 6 04-27-2004 12:58 AM
bash if statement question xscousr Programming 3 09-02-2003 11:58 AM
Need help adding an IF / ELSE statement to my Bash Script Relix Linux - General 1 08-01-2002 02:03 PM
bash for statement with 2 arrays? Noerr Linux - General 10 05-27-2002 12:58 PM


All times are GMT -5. The time now is 03:40 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration