| | |
Using Grep
![]() |
•
•
Join Date: Mar 2004
Posts: 1,620
Reputation:
Solved Threads: 51
Hello,
Grep is a filter, meaning that you have to send something to grep before grep can do anything. For example:
ps -aux | grep kc0arf
on a *nix machine will list out all of the processes that have kc0arf on the output of the ps command.
So, you need to use the who command to find out how many users are logged onto your machine. That is done by:
who
And you see all of the users. But let's look further:
man who
and you will see some options. Looking, we see who -- count.
Take a look at that, and you should find what you are looking for.
Unfortunately, I do not know further on how to do a conditional at the command prompt... I usually write a short bash script that has if/then/else logic nice and neatly formatted.
Let us know.
Christian
Grep is a filter, meaning that you have to send something to grep before grep can do anything. For example:
ps -aux | grep kc0arf
on a *nix machine will list out all of the processes that have kc0arf on the output of the ps command.
So, you need to use the who command to find out how many users are logged onto your machine. That is done by:
who
And you see all of the users. But let's look further:
man who
and you will see some options. Looking, we see who -- count.
Take a look at that, and you should find what you are looking for.
Unfortunately, I do not know further on how to do a conditional at the command prompt... I usually write a short bash script that has if/then/else logic nice and neatly formatted.
Let us know.
Christian
•
•
•
•
Originally Posted by kc0arf
Unfortunately, I do not know further on how to do a conditional at the command prompt... I usually write a short bash script that has if/then/else logic nice and neatly formatted.
bash-2.05# if [ foo ] ; then bar ; else echo "not foo or bar" ; fi Alex Cavnar, aka alc6379
•
•
Join Date: May 2005
Posts: 1
Reputation:
Solved Threads: 0
The syntax is different according for each shell, but if you are using the bash shell you could try the following:
if [ `who | wc -l` -lt 10 ]; then
echo "less than";
else
echo " more than";
fi
which can be written as one line as well, but i don't know what difference that would make.
if [ `who | wc -l` -lt 10 ]; then echo "less than"; else echo " more than"; fi
the point is that you need two programs, one that lists the users: who and one that counts lines wc.
wc is actually a word counting program, but with the -l flag it counts lines
The execution inverted comma's '`' mean to take the textual result and use it as a variable. That is the number that is returned from word count should be used as a variable.
The if statement uses the -lt flag, which stands for less than
so if [ `who | wc -l` -lt 10 ]
means if the the number of lines of the who output is less than 10, then perform the following action ...
in our case just echoing out to the screen, but you will want to run your program there
l8a
profhof
if [ `who | wc -l` -lt 10 ]; then
echo "less than";
else
echo " more than";
fi
which can be written as one line as well, but i don't know what difference that would make.
if [ `who | wc -l` -lt 10 ]; then echo "less than"; else echo " more than"; fi
the point is that you need two programs, one that lists the users: who and one that counts lines wc.
wc is actually a word counting program, but with the -l flag it counts lines
The execution inverted comma's '`' mean to take the textual result and use it as a variable. That is the number that is returned from word count should be used as a variable.
The if statement uses the -lt flag, which stands for less than
so if [ `who | wc -l` -lt 10 ]
means if the the number of lines of the who output is less than 10, then perform the following action ...
in our case just echoing out to the screen, but you will want to run your program there
l8a
profhof
![]() |
Similar Threads
- 'GREP' usage on huge files, any Limitation? (Shell Scripting)
- Program for Grep search (Pascal and Delphi)
- derive full name of user and grep search count (Shell Scripting)
- help writing a grep program (C)
Other Threads in the *nix Software Forum
- Previous Thread: XP, Redhat, Xandros, FreeBSD on one machine?
- Next Thread: How do you install files in general on linux any operating system
| Thread Tools | Search this Thread |






