LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 10-02-2013, 06:16 AM   #1
omicky83
LQ Newbie
 
Registered: Oct 2013
Posts: 10

Rep: Reputation: Disabled
Smile Help with the last command


I'm new to scripting and wondered if I could get a little help. I would like to execute a script that will extract or display the past 7 days of login after issuing the "last" command

Linux OS used Redhat 4.
 
Old 10-02-2013, 06:35 AM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,842

Rep: Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309
is this your homework?
what have you done so far?
 
Old 10-02-2013, 06:37 AM   #3
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Well, first you have to determine the last day in the past you want to be printed out:
Code:
last_day=$(date -d "8 days ago" +"%a %b %e")
then you can parse the ouput of the last command using awk:
Code:
last | awk -v day="$last_day" '$0 ~ day {x = 1} !x'
However, this solution assumes there is at least one entry per day in the wtmp file. A more general solution is to parse the date in every line of the output and check if it is more than 7 days ago:
Code:
#!/bin/bash
while read line
do
  day=${line:39:10}
  if [[ $(( $(date -d "today 00:00:00" +%s) - $(date -d "$day" +%s) )) -le 604800 ]]
  then
    echo "$line"
  else
    break
  fi
done < <(last)
Hope this helps.
 
2 members found this post helpful.
Old 10-02-2013, 06:44 AM   #4
omicky83
LQ Newbie
 
Registered: Oct 2013
Posts: 10

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by pan64 View Post
is this your homework?
what have you done so far?
Hello Pan

I know i can use the last command


last |grep date -d -7days
 
Old 10-02-2013, 06:46 AM   #5
omicky83
LQ Newbie
 
Registered: Oct 2013
Posts: 10

Original Poster
Rep: Reputation: Disabled
Thanks Colucix

Thanks Colucix. Trying it out.

Mike
 
Old 10-02-2013, 07:01 AM   #6
omicky83
LQ Newbie
 
Registered: Oct 2013
Posts: 10

Original Poster
Rep: Reputation: Disabled
Hello Colucix

Hello Colucix


I get this error when i try to run the batch script given.


mike.sh: line 5: syntax error in conditional expression
'ike.sh: line 5: syntax error near `]]

Thanks

Michael.
 
Old 10-02-2013, 07:24 AM   #7
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Strange, it works for me using bash version:
Code:
$ bash --version
GNU bash, version 4.2.42(1)-release (i586-suse-linux-gnu)
Copyright (C) 2011 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>

This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
What version of bash have you got? Also, please show us the current version of your script using CODE tags to preserve indentation. To use CODE tags you can put [CODE] and [/CODE] before and after the lines of code, respectively.
 
Old 10-02-2013, 07:29 AM   #8
omicky83
LQ Newbie
 
Registered: Oct 2013
Posts: 10

Original Poster
Rep: Reputation: Disabled
Content of mike.sh file.


Code:
#!/bin/bash
while read line
do
  day=${line:39:10}
  if [[ $(( $(date -d "today 00:00:00" +%s) - $(date -d "$day" +%s) )) -le 604800 ]]
  then
    echo "$line"
  else
    break
  fi
done < <(last)



Details of my bash file

GNU bash, version 3.2.25(1)-release (x86_64-redhat-linux-gnu)
Copyright (C) 2005 Free Software Foundation, Inc.

---------- Post added 10-02-13 at 07:30 AM ----------

Can i get your skype details please ? I think it will be easier to work with you there.

Mike

Last edited by colucix; 10-02-2013 at 07:34 AM. Reason: Added CODE tags
 
Old 10-02-2013, 07:37 AM   #9
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
I tried your/mine code on a machine with the same bash version as yours
Code:
GNU bash, version 3.2.25(1)-release (x86_64-redhat-linux-gnu)
Copyright (C) 2005 Free Software Foundation, Inc.
and it works. At this point I have no clue. Maybe try to change the code?

Edit: I have no skype account, but in the spirit of the LQ community better to share the discussion for the benefit of others having similar problems!
 
Old 10-02-2013, 07:56 AM   #10
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
What if you try to use single square brackets instead of double square brackets in the if/then construct?

Another one could be:
Code:
#!/bin/bash
day_last=$(date -d "7 days ago 00:00:00" +%s)
while read line
do
  day_now=${line:39:10}
  day_now=$(date -d "$day_now" +%s)
  if [[ $day_now -ge $day_last ]]
  then
    echo "$line"
  else
    break
  fi
done < <(last)
 
Old 10-02-2013, 08:07 AM   #11
omicky83
LQ Newbie
 
Registered: Oct 2013
Posts: 10

Original Poster
Rep: Reputation: Disabled
Code:
#!/bin/bash
day_last=$(date -d "7 days ago 00:00:00" +%s)
while read line
do
  day_now=${line:39:10}
  day_now=$(date -d "$day_now" +%s)
  if [[ $day_now -ge $day_last ]]
  then
    echo "$line"
  else
    break
  fi
done < <(last)
Output when i try to run this

mike.sh: line 7: syntax error in conditional expression
'ike.sh: line 7: syntax error near `]]
'ike.sh: line 7: ` if [[ $day_now -ge $day_last ]]

Can you paste your output here please ?

Michael.

Last edited by colucix; 10-02-2013 at 08:10 AM. Reason: Added CODE tags
 
Old 10-02-2013, 08:11 AM   #12
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Please try without double brackets:
Code:
#!/bin/bash
day_last=$(date -d "7 days ago 00:00:00" +%s)
while read line
do
  day_now=${line:39:10}
  day_now=$(date -d "$day_now" +%s)
  if [ $day_now -ge $day_last ]
  then
    echo "$line"
  else
    break
  fi
done < <(last)
My output is exactly what is supposed to be: the first N lines of the output of the last command until Wed Sep 25.
 
Old 10-02-2013, 08:19 AM   #13
omicky83
LQ Newbie
 
Registered: Oct 2013
Posts: 10

Original Poster
Rep: Reputation: Disabled
Code:
#!/bin/bash
day_last=$(date -d "7 days ago 00:00:00" +%s)
while read line
do
  day_now=${line:39:10}
  day_now=$(date -d "$day_now" +%s)
  if [ $day_now -ge $day_last ]
  then
    echo "$line"
  else
    break
  fi
done < <(last)
We are making progress here .

Please find the output below

Code:
bash mike.sh
mike.sh: line 13: syntax error near unexpected token `done'
'ike.sh: line 13: `done < <(last)

Last edited by colucix; 10-02-2013 at 08:30 AM. Reason: Added CODE tags
 
Old 10-02-2013, 08:30 AM   #14
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Ok. It looks like you are running the bash shell in compatibility mode with the old /bin/sh, that lacks the [[ command and the process substitution. Try this now, it should work:
Code:
#!/bin/bash
day_last=$(date -d "7 days ago 00:00:00" +%s)
last | while read line
do
  day_now=${line:39:10}
  day_now=$(date -d "$day_now" +%s)
  if [[ $day_now -ge $day_last ]]
  then
    echo "$line"
  else
    break
  fi
done
Anyway, I'm curious at this point about the shell version. Please show us the output of the following three commands
Code:
ls -l /bin/bash
file /bin/bash
alias bash
and please, use CODE tags!
 
Old 10-02-2013, 08:35 AM   #15
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Code:
bash mike.sh
mike.sh: line 13: syntax error near unexpected token `done'
'ike.sh: line 13: `done < <(last)
Another clue: I noticed the name of the script in the error message lacks the initial "m". I suspect at this point that there is a problem of format: maybe are you writing these script on a windows machine? If this is the case, take in mind that Windows and Unix/Linux have different line terminators and the windows terminator \r\n (that is Carriage Return + Newline) often triggers weird errors in bash.

Please try to write down your script in linux or use the dos2unix command to convert it to Unix format:
Code:
dos2unix mike.sh
then run again (any version of the scripts posted above).
 
1 members found this post helpful.
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Command/Script required to send an email alert in case command will not respond rajaniyer123 Linux - General 1 05-19-2012 01:12 PM
Executing shell command from JSP file with command line arg from URL orcusomega Programming 2 01-13-2012 03:38 PM
Bash Command Line Editor, while typing run another command before executing current? gumaheru Linux - General 5 04-13-2010 11:21 AM
how to copy drive using dd and tee command parallely? source code of dd command mdfakkeer Linux - Software 1 02-10-2010 01:31 PM
LXer: The Linux Command Shell For Beginners: Fear Not The Command Line! LXer Syndicated Linux News 0 12-22-2008 06:30 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 10:47 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration