It is currently Tue Jan 17, 2017 1:13 pm

Help script with "Timestamp"

All times are UTC - 6 hours

Post new topic Reply to topic  [ 4 posts ] 
Author Message
 PostPosted: Fri Sep 11, 2009 9:22 am   

Joined: Fri Sep 11, 2009 8:47 am
Posts: 1
Location: London
Hi i am actually new to bash, i have just gone through tutorials and trying to come up with a script which recognizes me when I login to the system, It timestamps me after every five minutes, I know for that I have to set a cronjob actually I want to find out the total time i spend on my system. I have come up with a script so far which actually does not recognizes my name, please let me know my mistake.


if ($USER == hira); then
    $USER >> hira.txt
    date "+ %m.%d.%y at %H:%M:%S: " >> hira.txt
    echo "You are not  $USER"   

Please help me :( i am trying my best to work on it..Thank u :)

 PostPosted: Fri Sep 11, 2009 4:42 pm   

Joined: Mon Mar 02, 2009 3:03 am
Posts: 633
You might want to take a look at the who command.

Isn't $USER already a shell variable?

 PostPosted: Mon Oct 05, 2009 7:23 pm   

Joined: Mon Oct 05, 2009 6:31 pm
Posts: 6
Couple of points here...

First, as a previous poster has pointed out, $USER is already a system variable. Try some variation like $TUSER or $VUSER instead.

Second, BASH can be really funny about variable types. It wants to see quotes ("") around text strings when operating with them. So, with this, it would be...

           if [  "$TUSER" == "kira" ]; then

Lastly, aforementioned previous poster mentioned "who", which is a great, though often forgotton, 'nix command. Another that may suit your needs is "w". Yes, that's actually a command and not a typo, just "w". Check it out :)

 PostPosted: Tue Oct 06, 2009 12:32 am   

Joined: Mon Nov 17, 2008 7:25 am
Posts: 221
Bash isn't really all that funny when comparing strings.
[email protected]:~> test="test"
[email protected]:~> if [ $test == test ]; then echo $test; fi

This will work, but! This is not usable when a whitespace character is introduced into the script, making it best practice to quote your strings.

One of the errors I've seen so far in his scripts this
if($USER == "kira")

This won't work because bash will ($USER == "kira") give you an "unary operator expected"-error. The correct syntax as described above is using [ and ] (with spaces between next "option" and them).
Second thing is that $USER >> file.txt won't work either, you're not making any output by doing that way unless you $USER variable contains a command which will create output.
Correct syntax here would be "echo $USER >> file.txt".
Third thing setting up a cronjob for this is not gonna help your form. Cronjobs will run as your user even thou you're not logged in.
Try adding something like this:
while [ true ]; do
   <check commands goes here>
   sleep 300

Execute this with the ampersand and it'll run every 5 minutes :)

Best regards
Fredrik Eriksson

Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC - 6 hours

Who is online

Users browsing this forum: Google [Bot], Yahoo [Bot] and 5 guests

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  

BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group