Problems with "if" and "wc"

Hello, I'm pretty new to this shell scripting, and I need some help, I must make a script, where I'm supposed to count all the running processes of the user,
what I've done so far, is, ps -u | wc -l
but I dont know even if this is completely right..

and besides that, I must make a statement if the processes are more than 5, I was thinking of using something like

if []
then
echo "good job"
else;
echo "bad job"

but thing is, I dont know how to configure wc, so that this message will show only if the number of processes is 5 or more. how do i do this?

This sounds remarkably like homework. Please post this in the homework forum.

Try this one.

 
# if [ $(ps -ef | grep "^root" | wc -l) -lt 5 ]; then echo "Good Job"; else echo "Bad Job"; fi

replace with your username inplace of bold

A small change . Pls avoid double quotes for search pattern . Command should as
if [ $(ps -ef | grep root | wc -l) -lt 5 ]; then echo "Good Job"; else echo "Bad Job"; fi

problem is not with the double quote but with "^"
because the ps command output begins with space.
try it:-

ps -ef | grep "username"
or
ps -ef | grep 'username'
or even
ps -ef | grep username

all of them will give you same o/p

BR

It's better to let "ps" do the selection work.
Be careful about counting the process "ps" itself in the count or counting the column heading from "ps".

For example, counting the processes for user "lp". There is much variation in the syntax for "ps" so you may need to adjust for your version.

#!/bin/ksh
USERNAME="lp"
COUNTER=$(ps -fu${USERNAME} | grep -v " PID " | grep -v 'ps -fu' | wc -l)
if [ ${COUNTER} -ge 5 ]
then
      echo "User ${USERNAME} : Processes count : ${COUNTER}"
fi

Let's not UUoC(grep & wc) :wink:

COUNTER=$(ps -fu ${USERNAME} | grep -vc "PID\|ps -fu")

@danmero

Interesting suggestion but your command does not work because it contains at least three errors and subtle changes to space characters which were not in the original. One side effect is that the "grep -v" never matches and COUNTER is therefore wrong.
Portable(ish) "ps" has no space character after "-u". We could be more specific if we knew which O/S .

If we were to go that way we could use "egrep" instead of "grep":

COUNTER=$(ps -fu${USERNAME} | egrep -vc " PID |ps \-fu")

I continue to prefer making a pipeline easy to read and less likely to introduce errors.