0

I found the following bash script which I would like to run, but I am not completely sure about what it is going to do. Can someone explain the working of the following bash script; i.e. what is it doing and how is it doing so?

echo - "Who are you looking for: "
read user

if [ -n "$user" ]
then
   list=`w | grep $user | cut -c19-30`

   if [ "$list" != "" ]
   then
     echo "The user $user is logged in from $list"
   else
     echo "The user $user is not logged in now"
   fi
fi
5
  • How can this question be off-topic? OP wants to know the working of the script. It doesn't describe a non-reproducible problem and it is more on-topic here than any other site given in the other off-topic close reason.
    – Aditya
    Apr 6, 2014 at 10:37
  • @Aditya I don't think that this is the right place to ask how working a specific script. If you accept this, soon you will see hundreds of questions with the same title and those will not be duplicates. If you have a specific problem like I can't understand the use of echo command in bash, or I don't know what if keyword do, then this make more sense and you can ask. And first to know something about a bash script, please read a tutorial. Apr 6, 2014 at 10:53
  • @RaduRădeanu: Okay, that makes sense.. But I think it is a small enough script and should be okay to be kept.. If you are not comfortable with the title or the wording of the question, please edit it to narrow the scope down... Moreover if the reason you closed it is different, then you should state the correct reasons why you are closing the question, just using the canonical close reasons only creates confusion for OP and people like me :)
    – Aditya
    Apr 6, 2014 at 11:03
  • 2
    @RaduRădeanu Understanding the workings of a bash script is on-topic here. It'b be good if we could come up with a better title though.
    – Seth
    Apr 6, 2014 at 17:09
  • I vote to leave this question open 'cause is on-topic. But for further users, the question should mention the specific sentences that he/she doesn't understand rather than the whole script.
    – Lucio
    Apr 7, 2014 at 15:52

1 Answer 1

6

This script allows you to enter a user that you are looking for. It will tell you whether or not the user is logged in or not and, if the user is logged in, from what location.

read user

This reads in the user name you entered

`w | grep $user | cut -c19-30`

The first part, w, prints out all logged in users accessing this machine. The grep $user filters out the lines that don't contain the user name you typed in. Finally, the cut -c19-30 splits the output to get the characters from position 19 to position 30 (this is where the location part is).

The last conditional block just checks to see if there is any output from the previous command. If there isn't, that means the user isn't logged in. If there is, then the user is logged in and it prints out the location.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .