0

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
2

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
  • 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

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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