Results 1 to 4 of 4
Hi ,
I want to check if a particular user is connected to my system, I should get an alert in terminal that <user> hased loged in.
I want to ...
- 05-12-2009 #1Just Joined!
- Join Date
- Apr 2009
- Location
- Bangalore,India
- Posts
- 10
Problem in if syntax
Hi ,
I want to check if a particular user is connected to my system, I should get an alert in terminal that <user> hased loged in.
I want to run this program in background every n sec, that I can do using "&"
But problem in the script is-
isuser.sh
-------------
sleep 3
if [ `who|grep niraj` ]
then echo "niraj has logged in"
who|grep niraj
fi
----------------
I am getting following error
./isuser.sh: line 2: [: too many arguments
Can anyone help. I am new to schell scripting. So if any other suggestion also,then please let me know.
- 05-13-2009 #2Linux User
- Join Date
- Jan 2007
- Location
- cleveland
- Posts
- 452
try this with an xterminal:
while true
do
sleep 3
if (who|grep niraj>/dev/null)
then xmessage "niraj has logged in"
fi
done &
or with "echo" in text modethe sun is new every day (heraclitus)
- 05-14-2009 #3Just Joined!
- Join Date
- May 2009
- Posts
- 5
Your script has a flaw. If expects a binary value to decide its outcome. When you run
>>> if [ `who|grep niraj` ] ,
the command retunrs a string which is essentially output of 'who' and that's why shell is complaining about so many args in if condition.
A simple fix is just to run this:
" if [ `who|grep niraj|wc -l` ] "
It's a hack, but will keep the shell happy
HTH,
Vishal
- 05-15-2009 #4Just Joined!
- Join Date
- Apr 2009
- Location
- Bangalore,India
- Posts
- 10
Hi bluehive,
Thanks. I got it where I was doing the mistake inside if syntax.
tpl,
code is excellent. I did not know Xmessage cmd. thanks for the code.


Reply With Quote