Skip to content

Instantly share code, notes, and snippets.

@viviparous
Last active May 4, 2024 16:58
Show Gist options
  • Save viviparous/efa21bd6374824ba8332a3a4ac7b4585 to your computer and use it in GitHub Desktop.
Save viviparous/efa21bd6374824ba8332a3a4ac7b4585 to your computer and use it in GitHub Desktop.
Use bash , ncal , Perl to list years, months, and weeks with week numbers
#! /usr/bin/env bash
#use ncal and perl to list weeks in a year range
#can also use cal in cygwin
#arg 1 year_start
#arg 2 year_end
#arg 3 mode "cal" # WIP
function showhelp() {
echo "Usage:"
echo "$0 help|h|-h <-- this message"
echo "$0 <no parameter> <-- current month (current year implicit)"
echo "$0 year1 <-- year and current month"
echo "$0 year1 year2 <-- year span and current month"
echo "$0 year1 year2 cal <-- mode for cygwin with cal"
exit
}
argcount=$#
ystt=$1
yend=$2
currY=$(date +"%Y")
# # # Initial Logic Checks, Invariants
if [ $argcount -eq 0 ]; then
ystt=$currY
yend=$currY
echo "mode 1 year $ystt"
elif [ $argcount -eq 1 ]; then
# echo "$@"
args=("$@");
arg1=${args[0]}
re='^[0-9]+$'
if ! [[ $arg1 =~ $re ]] ; then
showhelp
fi
fi
# # # Values have been established
if [[ $argcount -eq 2 && "$yend" -lt "$ystt" ]]; then
echo "arg 2 ($yend) cannot be less than arg 1 ($ystt)"
exit
elif [[ "$OSTYPE" == "linux-gnu"* && $argcount -eq 3 ]]; then
echo "Mode with 3 args is for cal in cygwin"
exit
#ref other OSTYPE values: "darwin cygwin msys win32 freebsd"
fi
currD=$(date +"%d")
currW=$(date +"%-W")
if [ $currW -lt 10 ]; then
currW=" "$currW
fi
currM=$(date +"%-m")
if [ $currM -lt 10 ]; then
currM=" "$currM
fi
if [ $argcount -eq 0 ]; then
echo "Current month: $currM"
ncal -h -b -w -M -m $currM $currY | awk -v dd=$currD -v ww=$currW '{if(match($0,"^" ww)) {print $0"<<<<<<<<<<<< [w="ww",d="dd"]"} else {print $0}}'
exit
elif [ $argcount -eq 1 ]; then
#same year
yend=$1
for im in {1..12} ; do ncal -h -b -w -M -m $im $ystt; done | sed 's/ /-/g' | perl -Mfeature=say -ne 'BEGIN { $sM=(); } chomp($_); if($_ =~ /^-+$/) { $skip=$_;} elsif( $_ =~ /^-+[A-Z]/) { $sM=$_; } else { say $_ . $sM ; }'
elif [ $argcount -eq 3 ]; then
#mode="cal" not "ncal"
echo "ToDo: fix for cal in cygwin, $3"
for iy in $(eval echo {$ystt..$yend}) ; do for im in {1..12} ; do cal -w -m $im $iy ; done ; done | sed 's/ /-/g' | perl -Mfeature=say -ne 'BEGIN { $sM=(); } chomp($_); if( $_ =~ /^-+$/ || $_ =~ /^---[A-Z]/ ) { $skip=$_; say ""; } elsif( $_ =~ /^----+[A-Z]/ ) { $sM=$_; } else { say $_ . $sM ; }'
# for iy in $(eval echo {$ystt..$yend}) ; do for im in {1..12} ; do ncal -b -w -M -m $im $iy; done ; done | sed 's/ /-/g' | perl -Mfeature=say -ne 'BEGIN { $sM=(); } chomp($_); if($_ =~ /^-+$/) { $skip=$_;} elsif( $_ =~ /^-+[A-Z]/) { $sM=$_; } else { say $_ . $sM ; }'
cal -w -m
exit
else
# ref _ for i in {01..10}; do echo $i; _ yields numbers like 01, 02, 03, ..., 10.
# ref _ iterate over the range of indices of an array, in bash _ myarray=('a' 'b' 'c'); for i in ${!myarray[@]}; do echo $i; done
# ref _ for iy in $(seq $ystt $yend) ; do for im in {1..12} ; do ncal -b -w -M -m $im $iy; done ; done | sed 's/ /-/g' | perl -Mfeature=say -ne 'BEGIN { $sM=(); } chomp($_); if($_ =~ /^-+$/) { $skip=$_;} elsif( $_ =~ /^-+[A-Z]/) { $sM=$_; } else { say $_ . $sM ; }'
for iy in $(eval echo {$ystt..$yend}) ; do for im in {1..12} ; do ncal -b -w -M -m $im $iy; done ; done | sed 's/ /-/g' | perl -Mfeature=say -ne 'BEGIN { $sM=(); } chomp($_); if($_ =~ /^-+$/) { $skip=$_;} elsif( $_ =~ /^-+[A-Z]/) { $sM=$_; } else { say $_ . $sM ; }'
fi
echo -e "\nCurrent month: $currM\n"
# h switch suppresses highlight
#ncal -M -b -w -h
ncal -M -b -w
echo -e "\nCurrent week: $currW\n"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment