Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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
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)
$ 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.
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)
#!/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
#!/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.
#!/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
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
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).
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.