Hi all,
I am new in shell scripting and I would like to make a bash script which will inform me if one of my friend is already logeed in. If a friend is logged in I want to take as output
USER1 : is in.
else USER1 : last time he/she logged in was "date"

I have to notice that the names of my friends are written in a file for example 1.txt. The 1.txt file is like that :
USER1
USER2
USER3
...

COULD ANYONE HELP ME ???

Thanks a lot!!!!!

check the man page of who command.
for reading the text file, you can use while loop
eg just an example

while read user
do
    who |grep $user
    if [ $? -eq 0 ];then
       echo "user $user logged in
    fi
done < "file"

I'm not sure if who will give you the last time they were logged in. If not, you can use finger, which will also tell you if they are currently logged in.

while read user

What do you mean when you say user.
Is it a variable from command line ??
I don't understand.
I want to write only the name of sh file on command line.

Also, how are read the lines from file 1.txt which contain the users ?

Could anyone give me the code exacly to read it ?

P.S. : I am sorry but now I learn about shell scripting.

#!/bin/sh

while read user
do
    who |grep $user
    if [ $? -eq 0 ];then
       echo "user $user logged in
    fi
done < "file"

"file" contains the list of users you have stated. the while loop reads each line of input from "file" , and assign that line to variable "user". Inside the loop, we do the who command and try to grep the user. to call a variable value out, we use the $ dollar sign infront of the variable word, like $user. The return value of grep, when found , is 0. So we catch this return value using $?. It will store the return value. We check it against 0, if it is 0, then print out the result.
please look here to learn more.

Your code is working nearly perfect.
The only problem I have is that the result of command

who | grep $user

appear on screen and I won't it.

And secondly, if an user is logged on two times appear two times, while I would like to appear only one.

Thanking you in advance!!!!!!

p.s. : Believe me it's the last question.

Your code is working nearly perfect.
The only problem I have is that the result of command

who | grep $user

appear on screen and I won't it.

And secondly, if an user is logged on two times appear two times, while I would like to appear only one.

Thanking you in advance!!!!!!

p.s. : Believe me it's the last question.

who |grep $user > /dev/null 2>&1

What about your requirement that it tell the last time they logged in? Assuming that's still important, you could try this (requres finger):

#/bin/bash

if [ $# -ne 1 ]
then
  echo "Need a file as input"
  exit 1
fi

while read user
do
  on=`finger $user | grep -c "On since"`
  if [ $on -gt 0 ]
  then
    echo "user $user is logged in"
  else
    last=`finger $user | grep "Last login"`
    echo "user $user not logged in. $last"
  fi
done < $1

What about your requirement that it tell the last time they logged in? Assuming that's still important, you could try this (requres finger):

finger is another way to find last logon time, however the finger daemon may be disabled on machines that are locked down in security. other tools such as last, and also 'who' (it has a column on the login time,) may be able to do the job as well...

finger is another way to find last logon time, however the finger daemon may be disabled on machines that are locked down in security. other tools such as last, and also 'who' (it has a column on the login time,) may be able to do the job as well...

Ah, didn't know about those options. Something new to be learned every day :icon_wink:

I found it fillaly how can I display the date when an user is logged in, but I have problems when an user is offline.

I write the command

lu_lines=`last $user | wc -l`
if [ $lu_lines -eg 2 ]; then
    echo "He is offline since a lot of time"
else
    echo ".... (wtmp info)"
fi

The result is an error -->
[: -eg: binary operator expected

check your spelling to the equal operator

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.