*****Shell Script*******
Given a month and the day of the week that's the first of that month, print a calendar for the month. (Remember, number of days in months is different and use \n to go to a new line.)
Unix has a cal command especially for this purpose.
By default, cal shows the current month's calendar.
mayankp#mayank:~/$ cal
November 2018
Su Mo Tu We Th Fr Sa
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30
If you want a calendar for a specific month of a specific year, do this:
mayankp#mayank:~/$ cal 1 2018
January 2018
Su Mo Tu We Th Fr Sa
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31
This displays the calendar for January 2018.
So, your shell script would be:(ex: calendar.sh)
#!/usr/bin/env bash
month=$1
year=$2
cal $1 $2
Run the script like this:
mayankp#mayank:~/$ sh calendar.sh 3 2018
March 2018
Su Mo Tu We Th Fr Sa
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
Let me know if this helps.
I am not sure if that applicable but I need to arrange and sort below output by timestamp below in column 2 under From , the newer should be on first line and the older on last line, what is needed is to keep the time format as it is, only I need to arrange by date
COUNT FROM TO
97 Oct 10 10:00:56 Oct 10 10:18:35
9 Mar 10 10:02:09 Oct 10 10:02:55
768 Oct 10 10:01:09 Oct 10 10:18:24
764 Oct 10 10:00:53 Oct 10 10:18:24
33 Oct 10 10:18:35 Oct 10 10:18:39
306 May 10 10:00:52 Oct 10 10:21:20
3 Oct 10 10:00:52 Oct 10 10:00:52
3 Oct 12 15:33:26 Nov 2 03:30:06
2 Oct 17 09:16:53 Oct 17 09:17:05
18 Nov 2 00:07:24 Nov 2 01:03:13
11 Oct 10 10:00:52 Oct 10 10:00:56
10095 Jun 10 10:00:52 Oct 10 10:18:24
10 Oct 10 10:18:40 Oct 10 10:18:45
1 Nov 2 03:21:32 Nov 2 03:21:32
1 Feb 2 01:31:53 Nov 2 01:31:53
1 Aug 2 03:26:24 Nov 2 03:26:24
1 Nov 2 03:21:32 Nov 2 03:21:32
1 Oct 10 10:18:05 Oct 10 10:18:05
1 Oct 17 09:16:52 Oct 17 09:16:52
1 Jan 10 10:02:55 Oct 10 10:02:55
1 Nov 2 23:24:09 Nov 2 23:29:09
1 Oct 10 10:00:52 Oct 10 10:00:52
1 Oct 10 10:00:53 Oct 10 10:00:53
1 Nov 2 03:22:22 Nov 2 03:22:22
1 Apr 2 06:41:29 Nov 2 06:41:29
The output should be with the same header with below as first line
1 Nov 2 23:24:09 Nov 2 23:29:09
, and below as the last line.
1 Jan 10 10:02:55 Oct 10 10:02:55
Take a look at man sort and you will see that you can sort by columns using the -k option.
This option supports a column number, and optional sort method.
For your case this might work:
sort -k2Mr -k3nr -k4r file.txt
-k2Mr do month sort on column two and reverse it.
-k3nr do numeric sort on column three and reverse it.
-k4r sort on column four and reverse it.
There is a question happened this week on my crontab job.
It's be set as below and works normal every two weeks until now.
10 06 * * 1 test $(($(date +\%W)\%2)) -eq 0 && echo 'test' > /tmp/test.log
The problem is
$(($(date +\%W)\%2)) would be 08, over 7 in February.
And it will show error message if you run in bash: value too great for base (error token is "08").
There is also not working when I try to revise it for forcing decimal base purpose :
10 06 * * 1 test $((10#$(date +\%W)\%2)) -eq 0 && echo 'test' > /tmp/test.log
Does someone know how to solve this issue? Many thanks.
Every two week at midnight
0 0 */15 * * echo 'test' > tmp.txt
Your cron job will be run at: (5 times displayed)
2016-03-15 00:00:00 UTC
2016-03-30 00:00:00 UTC
2016-04-15 00:00:00 UTC
2016-04-30 00:00:00 UTC
2016-05-15 00:00:00 UTC
Make condition in your script
The use of % sign is discouraged in crontab!
To ensure having jod started on monday, every two weeks you've better to create a small script.
#!/bin/bash
if [ "$1" == "-f" ] ;then # use "-f" switch to force execution on odd weeks
shift
else
printf -v d '%(%W)T' -1
((10#$d%2)) || exit 0 # stop here if odd week
fi
printf "Real job begin at %(%c)T, there...\n" -1
This could be shortened to one single line:
#!/bin/bash
[ "$1" == "-f" ]&&shift||{ printf -v d '%(%W)T' -1;((10#$d%2))||exit;}
So you could add standard lines in your crontab:
10 06 * * 1 path/to/myScript > /tmp/test.log
Sample: year 2016
date -d 2016-1-4\ 10:06 +%s
1451898360
for i in {0..52};do
printf -v d '%(%W)T' $((c=i*7*86400+1451898360))
((10#$d%2)) && printf "%(%a %d %b, week: %W)T\n" $c
done | cat -n
1 Mon 04 Jan, week: 01
2 Mon 18 Jan, week: 03
3 Mon 01 Feb, week: 05
4 Mon 15 Feb, week: 07
5 Mon 29 Feb, week: 09
6 Mon 14 Mar, week: 11
7 Mon 28 Mar, week: 13
8 Mon 11 Apr, week: 15
9 Mon 25 Apr, week: 17
10 Mon 09 May, week: 19
11 Mon 23 May, week: 21
12 Mon 06 Jun, week: 23
13 Mon 20 Jun, week: 25
14 Mon 04 Jul, week: 27
15 Mon 18 Jul, week: 29
16 Mon 01 Aug, week: 31
17 Mon 15 Aug, week: 33
18 Mon 29 Aug, week: 35
19 Mon 12 Sep, week: 37
20 Mon 26 Sep, week: 39
21 Mon 10 Oct, week: 41
22 Mon 24 Oct, week: 43
23 Mon 07 Nov, week: 45
24 Mon 21 Nov, week: 47
25 Mon 05 Dec, week: 49
26 Mon 19 Dec, week: 51
27 Mon 02 Jan, week: 01
For comparission with idea of using 15th and 30th each months:
for i in {1..12}/{15,30};do
date -d '2016/'$i +'%a %d %b, week: %W' 2>/dev/null
done | cat -n
1 Fri 15 Jan, week: 02
2 Sat 30 Jan, week: 04
3 Mon 15 Feb, week: 07
4 Tue 15 Mar, week: 11
5 Wed 30 Mar, week: 13
6 Fri 15 Apr, week: 15
7 Sat 30 Apr, week: 17
8 Sun 15 May, week: 19
9 Mon 30 May, week: 22
10 Wed 15 Jun, week: 24
11 Thu 30 Jun, week: 26
12 Fri 15 Jul, week: 28
13 Sat 30 Jul, week: 30
14 Mon 15 Aug, week: 33
15 Tue 30 Aug, week: 35
16 Thu 15 Sep, week: 37
17 Fri 30 Sep, week: 39
18 Sat 15 Oct, week: 41
19 Sun 30 Oct, week: 43
20 Tue 15 Nov, week: 46
21 Wed 30 Nov, week: 48
22 Thu 15 Dec, week: 50
23 Fri 30 Dec, week: 52
As you could see, there miss one operation on weeks: 6, 9, 10, 21, 32 and 45. At least there is 3 operation less in one year.
This is my table wys_attendence:
id studid adate amonth ayear acls_id attendence
1 28 02 07 2015 10 1
2 31 02 07 2015 10 0
4 32 02 07 2015 10 1
5 28 13 07 2015 10 0
6 31 13 07 2015 10 1
7 32 13 07 2015 10 1
9 28 14 07 2015 10 1
10 31 14 07 2015 10 1
11 32 14 07 2015 10 1
13 28 15 07 2015 10 1
14 31 15 07 2015 10 0
15 32 15 07 2015 10 1
17 28 16 07 2015 10 0
18 31 16 07 2015 10 1
19 32 16 07 2015 10 1
21 28 17 07 2015 10 1
22 31 17 07 2015 10 1
23 32 17 07 2015 10 0
24 28 20 08 2015 10 1
25 31 20 08 2015 10 1
26 32 20 08 2015 10 0
I want to check if every day of a specific year and month is in the table, and to display all the dates of a selected month and year.
if I am select july 2015.
The output I get is incorrect. This is what I get:
studid 02 02 02 13 13 13 14 14 14 15 15 15 16 16 16 17 17 17
But I want it like this:
studid 2 13 14 15 16 17
my controller code is here
`$amonth = Input::get('amonth');
$ayear = Input::get('ayear');
$teacher_attend=WysTeacherattendance::all();
$attendance = DB::table('wys_teacherattendances')
->where('t_amonth', $amonth)
->where('t_ayear',$ayear)
->get();`
my view.blade.php code is here
<th>studid</th>
#foreach($attendance as $attendances)
<th>{{$attendances->t_adate}}</th>
#endforeach
How can I modify my query to achieve the above result as well as check if days of my selected month and year are in the database or not if in database then day display only once.?
If you want to group records by date then use groupBy() as explained on http://laravel.com/docs/4.2/queries#selects
$attendance = DB::table('wys_teacherattendances')
->where('t_amonth', $amonth)
->where('t_ayear',$ayear)
->groupBy('t_adate')
->get();
I have log file like this:
...
Tue Nov 18 11:54:59 2014 1 10.8.0.6 148 /home/spy/test/spy.csv b _ i r spy ftp 0 * c
Tue Nov 18 11:55:00 2014 1 10.8.0.6 428 /home/spy/test/spy-atma.csv b _ i r spy ftp 0 * c
Tue Nov 18 11:55:01 2014 1 10.8.0.6 289 /home/spy/test/spy-xfer.csv b _ i r spy ftp 0 * c
Tue Nov 18 11:55:02 2014 1 10.8.0.6 148 /home/spy/test/spy.csv b _ o r spy ftp 0 * c
Tue Nov 18 11:55:03 2014 1 10.8.0.6 428 /home/spy/test/spy-atma.csv b _ o r spy ftp 0 * c
Tue Nov 18 11:55:04 2014 1 10.8.0.6 289 /home/spy/test/spy-xfer.csv b _ o r spy ftp 0 * c
END OF FILE
I need to start print last 5 mins interval(from last date) like this:
Tue Nov 18 11:55:00 2014 1 10.8.0.6 428 /home/spy/test/spy-atma.csv b _ i r spy ftp 0 * c
Tue Nov 18 11:55:01 2014 1 10.8.0.6 289 /home/spy/test/spy-xfer.csv b _ i r spy ftp 0 * c
Tue Nov 18 11:55:02 2014 1 10.8.0.6 148 /home/spy/test/spy.csv b _ o r spy ftp 0 * c
Tue Nov 18 11:55:03 2014 1 10.8.0.6 428 /home/spy/test/spy-atma.csv b _ o r spy ftp 0 * c
Tue Nov 18 11:55:04 2014 1 10.8.0.6 289 /home/spy/test/spy-xfer.csv b _ o r spy ftp 0 * c
I have function for date:
entry_time() {
date -d "$(cut -c 1-24 <<< "$1")" +%s
}
And I can print start date like this:
cutoff=$(( $(entry_time "$(tail -n1 "$LOG")") - $(entry_time "$(tail -n1 "$LOG")") % (5 * 60) ))
I can check all lines date is >= cutoff or not, print and going loop for something. But log file is so big and when I check like this, it's working 10-15 hours. Maybe it's work, but I need more simple way. How can I do this?
If i understand what you want properly then this should work
awk '{
Month=index("JanFebMarAprMayJunJulAugSepOctNovDec",$2)
Month=(Month+2)/3
split($4,a,":")
Time=mktime($5" "Month" "$3" "a[1]" "a[2]" "a[3])
}
(systime()-Time)<300 ' file