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?
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
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.
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":