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...