In OSX on the terminal, how can cal be used to obtain the calendar of the next month?
i.e. the following
command: "cal -m && date +%m +1"
should equate to
command: "cal -m 7"
and produce:
July 2014
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
Use this:
m=$(date +'%m'); ((m++)) ; cal -m $m
or this:
cal $(date -v+1m +'%m %y')
Related
I need that the output of the command cal stay highlighted in a variable in order print it into a wallpaper so I can disable the calendar software to save resources of a low spec computer. How can I make the cal output stay highlighted?
Perhaps something like:
cal -h | sed 's/ \('"$(date +%_d)"'\) /{\1}/'
outputs
June 2021
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
*****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'm pretty new to bash and all the terminal in general - I've been messing around with cal and the date scripts wondering if there is anyway to list all the dates of monday of the current month .
My thought process is going thru the cal command, listing out the dates and maybe cutting a column from that input. Is that possible ?
You can do it with date command. Print 10 mondays since month ago:
for x in $(seq 0 9)
do
date -d "$x monday 5 week ago"
done
And grep only current month. Full command: for x in $(seq 0 9); do; date -d "$x monday 5 week ago"; done | grep $(date +%b)
Output:
Mon Jun 5 00:00:00 MSK 2017
Mon Jun 12 00:00:00 MSK 2017
Mon Jun 19 00:00:00 MSK 2017
Mon Jun 26 00:00:00 MSK 2017
Given:
$ cal
June 2017
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
You can do:
$ cal | awk 'NF>5{print $2}'
Mo
5
12
19
26
If you want something that will support any day of cal, use the field width (gawk only this):
$ cal | gawk -v n=5 '
BEGIN{
FIELDWIDTHS = "3 3 3 3 3 3 3"
}
FNR>1{print $n}'
Th
1
8
15
22
29
Or, as pointed out in comments:
$ ncal | awk '/^Mo/'
Mo 5 12 19 26
Combination of cal,cut commands to achieve the output.
cal -h| cut -c'4,5'
Remove the highlight and cut the characters which suits in the fields of monday.
ncal | sed -n '/^Mo/p'
The output as below:
Mo 5 12 19 26
How can I use brace-expansion to make the equivalent of cal 8 2014; cal 9 2014? cal {8,9} 2014 expands to cal 8 9 2014 (showing the 9th of September.) cal {08 2014,09 2014} isn't a valid brace expansion, and I can't use cal {"08 2014","09 2014"} because that expands to cal "08 2014" "09 2014" which is two parameters that obviously don't make sense together here :/ I'm starting to question the utility of brace expansion....
brace-expansion is great, for loops are also great, but add printf|sh to your repertoire and
you'll be in great shape:
printf "cal %s 2014\n" {8,9}|sh
One clear benefit is you can run it without |sh first to see if it looks right.
You could also escape spaces so that the brace-expansion includes cal and 2014:
$ echo cal\ {8,9}\ 2014\;
cal 8 2014; cal 9 2014;
And then pipe through sh:
$ echo cal\ {8,9}\ 2014\;|sh
August 2014
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
September 2014
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
Script for counting weekends (saturdays AND sundays) in a given month, by passing in month and year. How to write it?
New in scripting, can't find solution to this easy task.
You want to count weekends Sat and Sun, So I am guessing you mean 2 day weekends.
Hope this helps. VonBell
In unix/linux you can easily parse the cal command for the given month.
$ cal 04 2013
April 2013
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
The numerical date always start on Line 3, the cols for each day are constant
When I used the commands below to extract the weekend (sun, Sat)
As you can see Sun is cols 1,2,3 (with space included) and Sat is cols 19,20
$ cal 04 2013|tail +3|cut -c1-3,19,20
.. 6
7 13
14 20
21 27
28
When I execute the script below as cntWkEnd.sh The output is as follows
1 equals one day wkend and 2 equals 2 day wkend sat and sun
$ ./cntWkEnd.sh 04 2013
1
2
2
2
1
0
To count only 2 day weekends you can add at the command line or in the script
the following
./cntWkEnd.sh 04 2013|grep "2"|wc -l (Output of 3 shown below)
3
This the contents of cntWkEnd.sh
#!/bin/bash
cal $1 $2|tail +3|cut -c1-3,19,20 |\
while read WkEnd
do
echo $WkEnd|wc -w
done