Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I am receiving the following error when attempting to run my script:

./watchuser: line 12: syntax error near unexpected token `echo'
./watchuser: line 12: `echo ("$User online since:" who| awk ' { print "User: " $1" -    Terminal: "$2" - Login @ " $3" "$4}') '

Here is my script, it is supposed to show users that are online in Epoch format.

#!/bin/bash
for User in $(cd /home;ls --hide=lost+found)do
echo ("$User online since:" who| awk ' { print "User: " $1" - Terminal: "$2" - $
NOW=$ ( date +%s -d "Jan 1, 1980 00:00:01")
USR=$ ( who |awk ' {print $1, ":",$5,$6,$9 } ' | grep $U)
USRDATE=$(echo $USR | cut -d ":" -f2)
(( USRDATE = $(date --date "$USRDATE" +%s) / 86400 ))
(( NOW = NOW/86400 ))
(( DAYS = NOW - USRDATE ))
done
share|improve this question

1 Answer 1

up vote 2 down vote accepted

Yes of course you receive errors. Your script is a mess. I can't understand exactly what you intend to do, but maybe this variant will give you an idea where are you wrong:

#!/bin/bash

for User in $(cd /home;ls --hide=lost+found); do
    echo "$User online since:" 
    who | awk '{ print "\tUser: "$1" - Terminal: "$2" - "$3" "$4}'
    NOW=$(date +%s -d "Jan 1, 1980 00:00:01")
    USR=$(lastlog |awk ' {print $1, ":",$5,$6,$9 } ' | grep $User)
    USRDATE=$(echo $USR | cut -d ":" -f2)
    (( USRDATE = $(date --date "$USRDATE" +%s) / 86400 ))
    (( NOW = NOW/86400 ))
    (( DAYS = NOW - USRDATE ))
done
share|improve this answer
    
I am trying to get a report of users that are logged in and for how many seconds. –  Macenbro Sep 27 '13 at 17:22
    
@Macenbro Ok, try, but please, without syntax errors, eventually read some tutorials about bash and use man <command> when you don't know something about a command. Also, before to use your commands in script, test those commands in terminal. –  Radu Rădeanu Sep 27 '13 at 17:28
    
well, I still get syntax errors with your corrections, but what can we do :P. You did something heroic just figuring out what OP wanted. –  Braiam Sep 27 '13 at 17:37

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.