Find the answer to your Linux question:
Results 1 to 6 of 6
Hello folks, i am using this piece of code to check if a user is logged on to the system or not; #! /bin/sh echo -n "Please enter the username ...
  1. #1
    Just Joined!
    Join Date
    Mar 2010
    Posts
    5

    Check if a user is logged on

    Hello folks,

    i am using this piece of code to check if a user is logged on to the system or not;

    #! /bin/sh


    echo -n "Please enter the username to check->"
    read username

    if who | grep $username
    then
    :
    else
    echo "'$username' is not currently logged in";
    fi

    exit 0

    My problem is that if am to enter a user id of less than 3 characters it is returning a posative result even if that user id does not exist, whats going on? I should mention that i am a total noob.....

  2. #2
    Linux Enthusiast scathefire's Avatar
    Join Date
    Jan 2010
    Location
    Western Kentucky
    Posts
    616
    perhaps you should try this instead:
    Code:
    #!/bin/bash
    echo -n "User: "
    read -e username
    TEST=`who | awk '{print $1}' | grep -x $username`
    if [ -z "$TEST" ];then
    echo "Not here"
    else
    echo "Here"
    fi
    linux user # 503963

  3. #3
    Just Joined!
    Join Date
    Mar 2010
    Posts
    5
    cheers for the help, though im not sure i understand the code......

    why would my current code be returning false positives? i have tested the program a little further and it would appear to work fine with numbers above 33. When 33 or anything below that number is entered i get a false positive.

  4. #4
    Linux Enthusiast scathefire's Avatar
    Join Date
    Jan 2010
    Location
    Western Kentucky
    Posts
    616
    when you run your command in the terminal what does it look like? when you do a grep the way you are doing its going to look for patterns, not exact matches.
    linux user # 503963

  5. #5
    Linux Enthusiast scathefire's Avatar
    Join Date
    Jan 2010
    Location
    Western Kentucky
    Posts
    616
    Quote Originally Posted by scathefire View Post
    perhaps you should try this instead:
    Code:
    #!/bin/bash
    echo -n "User: "
    read -e username
    TEST=`who | awk '{print $1}' | grep -x $username`
    if [ -z "$TEST" ];then
    echo "Not here"
    else
    echo "Here"
    fi
    Explained by line number

    2) echo and do not output trailing newline
    3) use readline to place the input into variable called username
    4) define a variable whose value is the total output of the commands inside the tick marks.
    4a) run who, use awk to grab the first string value it comes to, which is a space separated line, and grep for exact match for the value that is username.
    5) if the value of $TEST is null, the we can assume the user is not logged on.
    7) else we assume the user is here
    linux user # 503963

  6. #6
    Just Joined!
    Join Date
    Mar 2010
    Posts
    5
    Here is my complete code.

    Thanks greatly for breaking down your example code, not many would take the time to do that

    I shall try to make sense of it after i have my dinner
    Attached Files Attached Files

Posting Permissions

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