BASH Script, Watchuser

I am trying to write a BASH script to be able to tell me the following for any given user. I.e., I would like to be able to be prompted to type in a users name in order to get the info below.

User           Time logged on in seconds
User pts/0    86242

FYI, there are lots of people in these forums who will be willing to help you if you just show what you've already tried and ask a specific question.
Nobody here will do your job for you.
That being said, let me give you a couple of hints where you could start:
1) In the example

The 2nd field, indicated above as Time, I think it should be Terminal , because that's what a pts/0 is (see the line below the heading).
2) Do you really want to know the logged on time in seconds? If so, what for? Isn't the actual time (hh:mm) enough?
3) In the case that the time in the hh:mm is good, do you like the following output or still prefer the table format? (Either way, the command that was executed should give you a clue as to what you should include in your script):

gacanepa@Gabriel-PC ~ $ who | awk '{print "User: "$1 " - Terminal: "$2 " - Login @: " $3" "$4}'
User: gacanepa - Terminal: tty8 - Login @: 2013-09-25 19:58
User: gacanepa - Terminal: pts/0 - Login @: 2013-09-25 20:02
User: gacanepa - Terminal: pts/1 - Login @: 2013-09-25 20:14

Give it a try and get back to us if you have any questions afterwards.

Here is what I came up with. I am missing something, I am not sure what it is.

#!/bin/bash
#watchuser
case $1 in "0"
NOW=$(%s)
USR=$(who | awk {print "/user1|user2|user3|user4|user5/ "$1" Terminal: "$2 "  | grep $U)
USER1='/home/Alice | grep 'Alice' | cut -d -f2 | awk 'NR==1''
USER2='/home/Bob | grep 'Bob' | cut -d -f2 | awk 'NR==1''
USER3='/home/Mike | grep 'Mike' | cut -d -f2 | awk 'NR==1''
USER4='/home/Bill | grep 'Bill' | cut -d -f2 | awk 'NR==1''
USER5='/home/ec2-user | grep 'ec2-user' | cut -d -f2 | awk 'NR==1''
USRDATE=$(echo $USR | cut -d":" -f2)
(( USRDATE = $(date --date ""$USRDATE" +%s) / 86400 ))
(( NOW = NOW / 86400 )) (( DAYS = NOW - USRDATE ))
echo

I am getting this error when attempting to run this.

./watchuser: line 3: syntax error near unexpected token `newline'
./watchuser: line 3: `case $1 in "0"'

Quite simple, your case statement is wrong/incomplete..

case "$1" in
"opt1")  commands ;;
esac

You then set variables, and variables within a subshell.

What do you want to do?
-----
EDIT:

#!/bin/bash
#watchuser
for User in $(cd /home;ls --hide=lost+found);do
    echo "$User online since:"
    who|grep $User|grep pts|awk '{print $3" "$4" "$5}'
done

hth

I am trying to be able to run an output that asks me the usersname and then when I input it, it will output the following:

User         Time logged on in seconds
User pts/0 86242

reload the page, i had edited it :slight_smile:
It doesnt solve it, but it should get you get started.

EDIT:
You can 'read' users input by:

read -p "Enter your name: " buffer
echo "Your name is $buffer!"

However, you might prefer select..

select User in $(cd /home;/ls --hide=lost+found);do echo $User;break;done

How about if I want to have an output of when I type:
Filename username

User Time logged on in seconds
Mike pts/0 86242
Mike pts/1 8422
Mike pts/2 8422

How about read, think about what has been posted...
Specialy as its homework stuff and you dont seem to have basic understanding of bash... (sorry)

EDIT2:
Agree with gacanepa.
Read other scripts to enhance your skills.

When I started off with Linux and bash scripting, the most helpful book that I found is the Linux Command Line. I would suggest you download it from the book's website (it is available for free) and start reading it and doing the exercises. It will take its time, but eventually you will feel confident at writing scripts like this.
Believe it or not, I think the greater good that we can do here is to tell you to start from the beginning and build your scripting arsenal little by little :).
Good luck and let us know if you have further questions!

You have three output fields, but seem to only have two column headings. Please show us the input you're trying to process and explain (in English) what it is that you're trying to get out of it.

Note that in addition to the bad syntax on the case statement in Line 3 in your earlier posting:

#!/bin/bash
#watchuser
case $1 in "0"
NOW=$(%s)
USR=$(who | awk {print "/user1|user2|user3|user4|user5/ "$1" Terminal: "$2 "  | grep $U)
USER1='/home/Alice | grep 'Alice' | cut -d -f2 | awk 'NR==1''
USER2='/home/Bob | grep 'Bob' | cut -d -f2 | awk 'NR==1''
USER3='/home/Mike | grep 'Mike' | cut -d -f2 | awk 'NR==1''
USER4='/home/Bill | grep 'Bill' | cut -d -f2 | awk 'NR==1''
USER5='/home/ec2-user | grep 'ec2-user' | cut -d -f2 | awk 'NR==1''
USRDATE=$(echo $USR | cut -d":" -f2)
(( USRDATE = $(date --date ""$USRDATE" +%s) / 86400 ))
(( NOW = NOW / 86400 )) (( DAYS = NOW - USRDATE ))
echo

Line 4: %s is not a command.
Line 5: You are missing a }, some single quotes, and a definition for the variable U.
Lines 6-9: /home/* is most likely a directory name instead an executable command; -f2 is not a valid field delimiter for cut; you have mismatched double quotes; although you appear to be trying to store the output of a pipeline into these variables, you have no command substitution; and if these problems were fixed, you don't use these variables in your script.

In you original posting in this thread, you said you wanted to be prompted for a user's name and to get information for that user. There is nothing in this script that prompts for a user name. If you intended for the script to accept a user name as an operand, your script appears to only want to do something if the user name given is "0". But, the script doesn't do that and the sample output you have shown doesn't seem to do that either.

If we can't determine what you're trying to do from the description you provide, we can't help.