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 ...
- 03-22-2010 #1Just 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.....
- 03-22-2010 #2
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" filinux user # 503963
- 03-22-2010 #3Just 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.
- 03-22-2010 #4
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
- 03-22-2010 #5
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 herelinux user # 503963
- 03-22-2010 #6Just 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


Reply With Quote
