Find the answer to your Linux question:
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 ...
  1. #1
    Just Joined!
    Join Date
    Apr 2009
    Location
    Bangalore,India
    Posts
    10

    Question 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.

  2. #2
    tpl
    tpl is offline
    Linux 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 mode
    the sun is new every day (heraclitus)

  3. #3
    Just 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

  4. #4
    Just Joined!
    Join Date
    Apr 2009
    Location
    Bangalore,India
    Posts
    10

    Smile

    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...