LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   scripts file output format gone messy (https://www.linuxquestions.org/questions/linux-newbie-8/scripts-file-output-format-gone-messy-4175451073/)

slowerogue 02-21-2013 03:21 AM

scripts file output format gone messy
 
hi
i want to list last login at 7-19 everyday
i created script file like this (havent done because stuck somewhere)

Code:

#!/bin/bash
date=`date +%b" "%_d`
last=`last | grep "$date"`
echo $last

and the output:
Code:

[root@liang tmp]# sh last.sh
wikiadmi pts/1 192.168.238.1 Wed Feb 6 06:27 still logged in wikiadmi tty3 Wed Feb 6 05:40 still logged in wikiadmi tty2 Wed Feb 6 05:40 still logged in wikiadmi pts/0 :0.0 Wed Feb 6 05:25 still logged in wikiadmi tty1 :0 Wed Feb 6 05:25 still logged in reboot system boot 2.6.32-279.el6.x Wed Feb 6 05:24 - 05:50 (00:26) wikiadmi pts/0 192.168.238.1 Wed Feb 6 01:10 - 01:54 (00:44) reboot system boot 2.6.32-279.el6.x Wed Feb 6 01:08 - 05:50 (04:42)
[root@liang tmp]#

i have successfully grep the current date, but the "last command" output became 1 line, which is unreadable.
any help
thx

druuna 02-21-2013 03:41 AM

Try using echo "$last" instead of echo $last

BTW: It isn't wise to use variable names that are the same as the command name, make the vars upper case if you want to use that specific name.

slowerogue 02-21-2013 03:46 AM

hi ,
thanks alot!
next, i need to grep time from 7am-7pm, of everyday
how can i grep the specific time range ?

druuna 02-21-2013 04:02 AM

What are you actually trying to accomplish?

Your first post shows you are trying to grep a specific date and your last post talks about grepping a specific time range for every day. Are those two to be combined at a later stage?

Also: What do you mean by grep time from 7am-7pm? Should the time be the starting time or the ending time or both? Someone can log in at 18:59 and log out at 23:59, or log in at 06:59 and log out at 14:04. Should those be included? Or are you looking for people that logged in after 07:00 and logged out before 19:00?

slowerogue 02-21-2013 10:30 PM

hi, i am trying to acomplish the last login from 7am-7pm of everyday.
lets say i run the script on crontab 7.01pm everyday, so i will get the output from 12.00am-7pm, but i only want to get 7am-7pm

the first script i have already grep the current date's login, so next i would like to filter the output from only 7-7pm

if the person login before 7am, there is no need to grep it.
it doesnt matter when the user logout since the "last" will show the user is still logged on.

thats what i was told, request by client so i just follow their requirement.

thanks!

chrism01 02-21-2013 10:37 PM

In that case just create script that you can cron to run at eg 19:05 every day and run the last cmd and parse out the current date and the times you want.
You could use eg awk probably, although I'd use Perl.
I'm sure you can even do it in pure bash, but it would be messy.
Here's some links:

Bash
http://rute.2038bug.com/index.html.gz
http://tldp.org/LDP/Bash-Beginners-G...tml/index.html
http://www.tldp.org/LDP/abs/html/

Awk
http://www.grymoire.com/Unix/Awk.html

Perl
http://perldoc.perl.org/
http://www.perlmonks.org/?node=Tutorials
http://www.tizag.com/perlT/index.php


First choose your weapon, then go to it :)


All times are GMT -5. The time now is 02:31 PM.