943,544 Members | Top Members by Rank

Ad:
May 15th, 2004
0

Using Grep

Expand Post »
my problem I have to write on a single line a command to launch a script only if the number of users on the system is less than ten.
This is what I have so far.
grep | who "* < 10 " $Users-on
I always get confused about quotes and brackets
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
hotlantas69 is offline Offline
7 posts
since May 2004
May 15th, 2004
0

Re: Using Grep

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
Team Colleague
Reputation Points: 121
Solved Threads: 57
Posting Virtuoso
kc0arf is offline Offline
1,629 posts
since Mar 2004
May 15th, 2004
0

Re: Using Grep

Quote 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.
You'd just write the same thing at the prompt, just use semicolons instead of carriage returns:

bash-2.05# if [ foo ] ; then bar ; else echo "not foo or bar" ; fi
Team Colleague
Reputation Points: 186
Solved Threads: 147
Cookie... That's it
alc6379 is offline Offline
2,519 posts
since Dec 2003
May 24th, 2005
0

Re: Using Grep

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
profhof is offline Offline
1 posts
since May 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Linux Applications and Software Forum Timeline: XP, Redhat, Xandros, FreeBSD on one machine?
Next Thread in Linux Applications and Software Forum Timeline: Getting my CD drive to work in WINE





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC