Shell script to log user

dtank
dtank used Ask the Experts™
on
basically i am trying to use this file to output the date only if the user name supplied as an argument is logged in.

#!/bin/sh
set user_in = 0
set user_date = 0
user_in=`who | grep $1 | tail -1 | awk '{printf $1}'`
user_date=`who | grep $1 | tail -1 | awk '{printf $3 " " $4 " " }'`
case $user_in in
$1)
        echo $user_date > ./out
*)
        echo "$1 is not currently logged in"
esac

i get the following error messages
: Command not found.
: Command not found.
case: Too many arguments.

Thanks in advance for any assistance
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2006

Commented:
each case is terminated by a ;;

$1)
       echo $user_date > ./out;;
*)
       echo "$1 is not currently logged in";;

Commented:
to track logins use the standard command "last"

Author

Commented:
I have tried this but the same error messages as above
OWASP: Avoiding Hacker Tricks

Learn to build secure applications from the mindset of the hacker and avoid being exploited.

Top Expert 2006

Commented:
remove set

user_in=0
user_date=0

and keep ;; ( thats the syntax )

Author

Commented:
removing the set gives me undefined variables.
I have added the ;; for each case and still same error
grrrr i am super frustrated this should work fine!

Author

Commented:
ps using redhat 8.0 (psyche)
if that helps
Top Expert 2006

Commented:
that is because you have space on both sides of = while there should be none ... look at my previous post carefully

Author

Commented:
#!/bin/sh
user_in=0
user_date=0
user_in=`who | grep $1 | tail -1 | awk '{printf $1}'`
user_date=`who | grep $1 | tail -1 | awk '{printf $3 " " $4 " " }'`
case $user_in in
$1)
        echo $user_date > ./out;;
*)
        echo "$1 is not currently logged in";;
esac

this code is still giving me the following errors:
: Command not found.
: Command not found.
: Command not found.
: Command not found.
user_in: Undefined variable.
Top Expert 2006
Commented:
your basic problem is inappropriate spaces ... ths works .. compare and correct

#!/bin/bash

user_in=0
user_date=0

user_in=`who | grep $1 | tail -1 | awk ' { printf $1 } '`
user_date=`who | grep $1 | tail -1 | awk ' { printf $3 " " $4 " " } '`

case $user_in in
$1)     echo $user_date;;
*)      echo not logged in;;
esac

Author

Commented:
if i use the set for each variable then i get the folowing error messages

: Command not found.
: Command not found.
case: Too many arguments.

Commented:
sunnycoder's script is 100% correct.
You're using /bin/sh (notice sunnycoder's specify bash). What gives

ls -la /bin/sh

?

Chances are that the "system" shell is actually tcsh.

What do you prefer to use ? if bash, just use sunnycoder's script in its integrality (with #!/bin/bash)
If tcsh, then you'll have to rewrite the switch-case statement, see man tcsh for details.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial