"This Week" script: in bash, awk, or whatever.

For discussions about programming, programming questions/advice, and projects that don't really have anything to do with Puppy.
Message
Author
User avatar
MochiMoppel
Posts: 2084
Joined: Wed 26 Jan 2011, 09:06
Location: Japan

#21 Post by MochiMoppel »

Apart from bash uses only date.
I didn't bother to localize "week" in the header.
Unicode line and bold format may not work in some consoles.

Code: Select all

#!/bin/bash
DW=$(date +%w)                                      # day of week
CW=$(date +%U)                                      # week number of today since year start
SW=$(date -d "tomorrow $(date +%d) days ago" +%U)   # week number of 1st day of month since year start
MW=$((CW-SW+1))                                     # week number of today since month start
sunday=$(date -d "$(date +%u) days ago" +%F)        # date of current week's sunday (= first date in line) 

date +"%B %Y, week $MW%n──────────────────────────"
echo $(
for i in {0..6}; do 
date -d "$sunday + $i day" +%a
done
)

for i in {0..6}; do
((i!=DW))&& format="${format}%3d " || format="${format}\e[1;41;97m%3d\e[0m "
done

printf "$format" $(
for i in {0..6}; do
date -d "$sunday + $i day" +%d
done
)
Attachments
thisweek.png
(1.08 KiB) Downloaded 224 times
Last edited by MochiMoppel on Tue 20 Nov 2018, 10:21, edited 1 time in total.

musher0
Posts: 14629
Joined: Mon 05 Jan 2009, 00:54
Location: Gatineau (Qc), Canada

#22 Post by musher0 »

Thanks for this, MM.

However, I have to run your script preceded by a LANG variable.
LANG=en_CA or LANG=fr_CA are ok, but not LANG=C and not
LANG=fr_CA.utf8.

Otherwise I get errors. Please see screen capture.
My system language is fr_CA.utf8. Mysterious.

BFN.
Attachments
thisweek-MM-quite-strange.jpg
(152.76 KiB) Downloaded 193 times
musher0
~~~~~~~~~~
"You want it darker? We kill the flame." (L. Cohen)

musher0
Posts: 14629
Joined: Mon 05 Jan 2009, 00:54
Location: Gatineau (Qc), Canada

#23 Post by musher0 »

Hello all.

Here is a simple one-liner inspired by puppy_apprentice's approach,
without coloring:

Code: Select all

cal | head -n 2;cal | tail -n 6 | grep $(date '+%d')
   November 2018
Su Mo Tu We Th Fr Sa
18 19 20 21 22 23 24
BFN.
musher0
~~~~~~~~~~
"You want it darker? We kill the flame." (L. Cohen)

User avatar
MochiMoppel
Posts: 2084
Joined: Wed 26 Jan 2011, 09:06
Location: Japan

#24 Post by MochiMoppel »

musher0 wrote:Otherwise I get errors. Please see screen capture.
This should work better:
sunday=$(date -d "$(date +%u) days ago" +%F)

User avatar
MochiMoppel
Posts: 2084
Joined: Wed 26 Jan 2011, 09:06
Location: Japan

#25 Post by MochiMoppel »

musher0 wrote:Here is a simple one-liner inspired by puppy_apprentice's approach,
without coloring:

Code: Select all

cal | head -n 2;cal | tail -n 6 | grep $(date '+%d')
   November 2018
Su Mo Tu We Th Fr Sa
18 19 20 21 22 23 24
.
A bit shorter:

Code: Select all

cal | egrep "[a-z]|$(date +%d)"
   November 2018
Su Mo Tu We Th Fr Sa
18 19 20 21 22 23 24

musher0
Posts: 14629
Joined: Mon 05 Jan 2009, 00:54
Location: Gatineau (Qc), Canada

#26 Post by musher0 »

Yep. Thanks MM.

Getting fancy colors, with a mix of approaches and the help of ANSI code
\e[2A (hehe):

Code: Select all

echo -e "\n\t      \e[33m`date '+%b %Y'`";echo -e "\t`cal | head -n 2 | tail -1`\e[0m";cal | tail -n 6 | nl| grep --color=always $(date '+%d');echo;echo -e "\e[2A\e[33mweek\e[0m"
Image of result attached.

BFN.
~~~~~~~~~~~
10 minutes later -- As a proper script --

Code: Select all

#!/bin/bash
# /root/my-applications/bin/fancyWeek.sh
####
echo -e "\n\t      \e[33m`date '+%b %Y'`"
echo -e "\t`cal | head -n 2 | tail -1`\e[0m"
cal | tail -n 6 | nl | grep --color=always $(date '+%d')
echo
echo -e "\e[2A\e[33mweek\e[0m"
Attachments
Fancy.jpg
(9.31 KiB) Downloaded 178 times
Last edited by musher0 on Tue 20 Nov 2018, 10:38, edited 1 time in total.
musher0
~~~~~~~~~~
"You want it darker? We kill the flame." (L. Cohen)

musher0
Posts: 14629
Joined: Mon 05 Jan 2009, 00:54
Location: Gatineau (Qc), Canada

#27 Post by musher0 »

MochiMoppel wrote:
musher0 wrote:Otherwise I get errors. Please see screen capture.
This should work better:
sunday=$(date -d "$(date +%u) days ago" +%F)
Ok now, thanks.
Attachments
thisweek-MM-not-strange.jpg
(10.74 KiB) Downloaded 162 times
musher0
~~~~~~~~~~
"You want it darker? We kill the flame." (L. Cohen)

User avatar
puppy_apprentice
Posts: 299
Joined: Tue 07 Feb 2012, 20:32

#28 Post by puppy_apprentice »

Other variations:

Code: Select all

#!/bin/sh
#without coloring
cal | head -n 2 ; line=$(cal | grep -n " $(date +%d) ") ; echo ${line:2} \(week $((${line:0:1}-2))\)

echo

#with coloring
cal | head -n 2 ; today=$(date +%d) ; line=$(cal | grep -n " $today ") ; echo $(echo ${line:2} | grep --color=always " $today ") \(week $((${line:0:1}-2))\)

echo

# <tail -n 6> won't work for other months?
echo "    " $(cal | head -n 1) ; echo " " $(cal | head -n 2 | tail -1) ; cal | tail -n 6 | grep -n --color=always " $(date +%d) "

echo

#without coloring
cal | head -n 2 ; line=$(cal | tail -n +3 | grep -n " $(date +%d) ") ; echo ${line:2} \(week ${line:0:1}\)

echo

#with coloring
cal | head -n 2 ; today=$(date +%d) ; line=$(cal | tail -n +3 | grep -n " $today ") ; echo $(echo ${line:2} | grep --color=always " $today ") \(week ${line:0:1}\)

echo

#changed <tail -n 6> into <tail -n +3>
echo "    " $(cal | head -n 1) ; echo " " $(cal | head -n 2 | tail -1) ;  cal | tail -n +3 | grep -n --color=always " $(date +%d) "
Above code doesn't find eg. 18 like on picture ;)
Attachments
Screenshot_2018-11-20_154727.jpg
(37.12 KiB) Downloaded 135 times
Last edited by puppy_apprentice on Tue 20 Nov 2018, 19:16, edited 11 times in total.

User avatar
nosystemdthanks
Posts: 703
Joined: Thu 03 May 2018, 16:13
Contact:

#29 Post by nosystemdthanks »

edit: ok, you fixed it.

musher0
Posts: 14629
Joined: Mon 05 Jan 2009, 00:54
Location: Gatineau (Qc), Canada

#30 Post by musher0 »

@nosystemdthanks: Euh... Who fixed what, please?!

@all: Do you guys think that we can top Beethoven's 33 Variations (on a
theme by Diabelli)? (tongue-in-cheek icon here!)
We are already at 15 variants for this "This Week" script. :lol:
Last edited by musher0 on Tue 20 Nov 2018, 19:05, edited 2 times in total.

User avatar
puppy_apprentice
Posts: 299
Joined: Tue 07 Feb 2012, 20:32

#31 Post by puppy_apprentice »

Maybe me. Because his comment is under my post. But i'm fixed one bug and there is one more ;)

musher0
Posts: 14629
Joined: Mon 05 Jan 2009, 00:54
Location: Gatineau (Qc), Canada

#32 Post by musher0 »

puppy_apprentice wrote:Maybe me. Because his comment is under my post. But i'm fixed one bug and there is one more ;)
I see.
~~~~
Ah yes, bugs.
We amateur devs have quite the insect collection, don't we! :lol:
musher0
~~~~~~~~~~
"You want it darker? We kill the flame." (L. Cohen)

User avatar
nosystemdthanks
Posts: 703
Joined: Thu 03 May 2018, 16:13
Contact:

#33 Post by nosystemdthanks »

musher: first i noticed that grep wouldnt apply if it was the 8th, then i noticed other code that fixed it.

even if i was right about the code in question, the code that fixed it meant that the author was aware of the issue and had a fix on another line, so pointing it out was moot.
[color=green]The freedom to NOT run the software, to be free to avoid vendor lock-in through appropriate modularization/encapsulation and minimized dependencies; meaning any free software can be replaced with a user’s preferred alternatives.[/color]

User avatar
puppy_apprentice
Posts: 299
Joined: Tue 07 Feb 2012, 20:32

#34 Post by puppy_apprentice »

It seems that will works:

Code: Select all

echo "     " $(cal | head -n 1) ; echo "  " $(cal | head -n 2 | tail -1) ;  cal | tail -n +3 | grep -n "$(date +%d)" | tr ':' ' ' | echo "w"$(grep --color=always " $(date +%d) ")
Checked for eg. 8th:

Code: Select all

echo "     " $(cal | head -n 1) ; echo "  " $(cal | head -n 2 | tail -1) ;  cal | tail -n +3 | grep -n "8" | tr ':' ' ' | echo "w"$(grep --color=always " 8 ")
Arrghh. Not working. Look on the pic for 8th. I have to clean my glasses ;) It sems that without sed/awk i won't colorize my output.


Maybe this (check this somebody before i start to be insane ;)):

Code: Select all

echo "    " $(cal | head -n 1) ; echo "w:"$(cal | head -n 2 | tail -1) ;  cal | tail -n +3 | grep -Ewn --color=always "$(date +%d)"
echo "    " $(cal | head -n 1) ; echo "w:"$(cal | head -n 2 | tail -1) ;  cal | tail -n +3 | grep -Ewn --color=always "8"
echo "    " $(cal | head -n 1) ; echo "w:"$(cal | head -n 2 | tail -1) ;  cal | tail -n +3 | grep -Ewn --color=always "18"
Attachments
Screenshot_2018-11-20_224953.jpg
Working! At last. I can go to sleep ;)
(37.01 KiB) Downloaded 263 times
Screenshot_2018-11-20_203354.jpg
and for 18th too
(9.51 KiB) Downloaded 273 times
Screenshot_2018-11-20_203128.jpg
(9.28 KiB) Downloaded 272 times

musher0
Posts: 14629
Joined: Mon 05 Jan 2009, 00:54
Location: Gatineau (Qc), Canada

#35 Post by musher0 »

Hi Puppy_Apprentice.

Nope. You're not insane!!! :twisted: Your line

Code: Select all

echo "    " $(cal | head -n 1) ; echo " " $(cal | head -n 2 | tail -1) ;  cal | tail -n +3 | grep -Ewn --color=always "$(date +%d)"
does not show the "w" abbreviation on my set-up, though.

As to my own kind of insanity... ;) here is my 3rd iteration. Hopefully the
ANSI escape code magic provided by

Code: Select all

echo -e "\e[1A\e[C$Sem"
at line 49 will show on someone else's box. (Scrot included.)

Code: Select all

#!/bin/bash
# /root/my-applications/bin/ThisWeek.sh
# Lien : /root/my-applications/bin/CetteSemaine
#
# Purpose: Show the week's dates.
# Objectif : Montrer les dates de la semaine.
#
# Result --
# First shows the name of the month and the
# abbreviations of the week days in the system's
# default language. Then it shows the dates for
# the week, a separator, and the week number.
#
# Résultat --
# Montre d'abord le nom du mois et les abréviations
# des jours dans la langue par défaut du système.
# Puis les dates de la semaine, un séparateur et
# le numéro de la semaine.
#
# Required / Requis : awk, busybox cal, nl & regex.
#
# © Christian L'Écuyer, Gatineau (Qc), Canada, 18 nov. 2018.
# (Alias musher0 [forum Puppy].) # Rév. : 19, 20 nov. 2018.
# GPL3. S.v.p. voir ci-dessous. / Please see below.
####
Ligne () { echo -e "\t\e[33m-- -- -- -- -- -- --\e[0m"; }

# if [ "${LANG:0:2}" != "en" ];then
#    LangVx="$LANG";export LANG=en_CA
# fi
# Comment out the three lines above if you wish to have
# the output in the system's default language, if the
# system's default language is not English.

case "${LANG:0:2}" in fr)Sem="e sem.:" ;;
     *)Sem="th wk:" ;;
esac

TodaY="`date '+%d'`" # echo $TodaY

> temp1
echo -e "\n\e[33m`cal | grep '[[:alpha:]]'`" >> temp1 # \e[1A # \e[4m
while read line;do
     echo -e "\t$line"
done < temp1
Ligne
cal | grep -v '[[:alpha:]]' | nl -w2 -nln | grep --color=always  $(date '+%d')
# | awk '$0 ~ /'$TodaY'/ { print $2,$3,$4,$5,$6,$7,$8" -- '$Sem' "$1}' >> temporaire
echo -e "\e[1A\e[C$Sem"
Ligne

read # Press Enter to exit
# rm -f temporaire
rm -f temp1

# If for a strange reason you do not have busybox on your system,
# replace the < busybox cal > command above with the syntax for
# the GNU cal, which is < cal -s >.

# [ "${LangVx}" != "" ] && export LANG="$LangVx"
# Comment out the line above to restore the original LANG var., if need be.
exit

Notes --
Pseudo-GUI --
rxvt -g 45x8+200+200 +sb -tr -tint snow -sh 50 -fn xft:LiberationMono:pixelsize=15:antialias=true:autohint=true -e CetteSemaine

Line for an aemenu --
cmd \"    Week\" \"rxvt -g 45x8+200+200 +sb -tr -tint snow -sh 50 -fn xft:LiberationMono:pixelsize=15:antialias=true:autohint=true -e CetteSemaine\"

Licence --
#################   # https://opensource.org/licenses/GPL-3.0
This program is free software: you can redistribute it and/or modify it under the
terms of the GNU General Public License as published by the Free Software
Foundation, either version 3 of the License, or  (at your option) any later version.
     This program is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
A PARTICULAR PURPOSE. See the GNU General Public License for more details.
     You should have received a copy of the GNU General Public License along with
this program. If not, see <http://www.gnu.org/licenses/>.
##########
Ce programme est libre : vous pouvez le redistribuer ou modifier selon les termes
de la Licence Publique Générale GNU publiée par la Free Software Foundation (v. 3
ou toute version ultérieure choisie par vous).
    Ce programme est distribué dans l''espoir qu''il sera utile, mais SANS AUCUNE
GARANTIE, ni explicite ni implicite, y compris des garanties de commercialisation
ou d''adaptation à un but spécifique. Pour plus de détails, veuillez vous reporter
au texte officiel de cette licence à https://opensource.org/licenses/GPL-3.0, à
http://www.linux-france.org/article/these/gpl.html pour une traduction et, pour une
explication en français, à https://fr.wikipedia.org/wiki/Licence_publique_générale_GNU.
################
Attachments
ThisWeek-example3.jpg
(11.68 KiB) Downloaded 260 times
musher0
~~~~~~~~~~
"You want it darker? We kill the flame." (L. Cohen)

User avatar
puppy_apprentice
Posts: 299
Joined: Tue 07 Feb 2012, 20:32

#36 Post by puppy_apprentice »

@musher0

I don't have <nl> command in my Slacko 5.7 (devx not loaded) - picture 1

Ok, my last solutions (i think that i can't make them shorter without loose colors and don't use awk/sed) - picture 2

Code: Select all

#!/bin/sh

# version 1 (grep is last so print colors normally):
echo "    " $(cal | head -n 1) ; echo "w:"$(cal | head -n 2 | tail -1) ;  cal | tail -n +3 | grep -Ewn --color=always "$(date +%d)"

# version 2 (grep isn't last so color and number for week are extracted from line variable):
cal | head -n 2 ; line=$(cal | tail -n +3 | grep -Ewn --color=always "$(date +%d)") ; echo ${line:30} \(${line:0:12}\)
Thx musher0 for topic. I had fun and have idea for console pim - THE OSMO KILLER ;)
Attachments
Screenshot_2018-11-21_140321.jpg
1
(31.69 KiB) Downloaded 222 times
Screenshot_2018-11-21_135745.jpg
2
(31.49 KiB) Downloaded 220 times

User avatar
MochiMoppel
Posts: 2084
Joined: Wed 26 Jan 2011, 09:06
Location: Japan

#37 Post by MochiMoppel »

puppy_apprentice wrote:Ok, my last solutions (i think that i can't make them shorter without loose colors and don't use awk/sed) - picture 2

Code: Select all

#!/bin/sh

# version 1 (grep is last so print colors normally):
echo "    " $(cal | head -n 1) ; echo "w:"$(cal | head -n 2 | tail -1) ;  cal | tail -n +3 | grep -Ewn --color=always "$(date +%d)"

# version 2 (grep isn't last so color and number for week are extracted from line variable):
cal | head -n 2 ; line=$(cal | tail -n +3 | grep -Ewn --color=always "$(date +%d)") ; echo ${line:30} \(${line:0:12}\)
Your scripts will not work for dates < 10
The command "$(date +%d)" will return zero padded numbers (e.g. 01) while cal returns the first day of the month as '1'. Your grep command will not find a match.

- You should force date to return unpadded numbers by adding a '-' after the % :"$(date +%-d)"
- You must surround the echoed expression in version 2 with doublequotes to prevent echo from stripping leading/trailing spaces (e.g. the first week of this month would be printed left aligned, with '1' showing as a Sunday ...)
- Removing 2 backslashes and 1 E option offsets the three added characters :lol:

Your 2nd version would look like this:

Code: Select all

cal | head -n 2; line=$(cal | tail -n +3 | grep -wn --color=always "$(date +%-d)"); echo "${line:30}  (${line:0:12})"

User avatar
fredx181
Posts: 4448
Joined: Wed 11 Dec 2013, 12:37
Location: holland

#38 Post by fredx181 »

The prize for the "bash script of the week" goes to...

musher0 (and everybody else :lol: ) Great thread !

Next week another, maybe "bash script of the month" ? :wink:

User avatar
puppy_apprentice
Posts: 299
Joined: Tue 07 Feb 2012, 20:32

#39 Post by puppy_apprentice »

Code: Select all

# Mochi adjustments with more colors
l=$(cal | tail -n +3 | grep -wn --color=always "$(date +%-d)"); echo "${l:0:6}"; cal | head -n 2; echo -e "${l:9:3}${l:30} (${l:0:12})\n"
l=$(cal | tail -n +3 | grep -wn --color=always "$(date +%-d)"); echo "${l:15:6}"; cal | head -n 2; echo -e "${l:24:3}${l:30} (${l:0:12})\n"
Learn from Mochi kids, he knows things ;)
Attachments
Screenshot_2018-11-21_165454.jpg
A tribute to Mochi! Hail to the console Caesar!
(31.93 KiB) Downloaded 190 times

User avatar
rufwoof
Posts: 3690
Joined: Mon 24 Feb 2014, 17:47

#40 Post by rufwoof »

Thanks (generally). I've modified my console (terminal) on which I run tmux and the default window shows a tput based menu, alongside a current month cal that previously I'd set to be just cyan - but now that has the current day 'highlighted' in a different colour by using the concepts detailed in this thread.

Image is a actual android phone (somewhat wobbly) snap as OpenBSD by default for security reasons prevents software based snaps of the console.

ksh snippet for the date and time functions

Code: Select all

_show_date_time () {

    while : 
    do
	       tput sc
	       tput bold
               tput setaf 3
	       tput cup 11 5
	       echo `date +" %R %a %d %b"`
	       tput rc
	       sleep 15
    done &
    PIDofdatetime=$!

}

_show_cal () {
    
    tput bold
    tput setaf 6
    echo;echo;echo
    B=$(tput setaf 5;tput setab 7;tput rev;tput bold)
    U=$(tput sgr0;tput setaf 6;tput bold)
    DAY=$(date +%e | tr -d " ")
    cal | sed -E -e "s|(.*[^0-9])($DAY)([^0-9].*)|\1$B\2$U\3|" | sed 's/^/   /g'

}
Attachments
console.jpg
(24.78 KiB) Downloaded 198 times
[size=75]( ͡° ͜ʖ ͡°) :wq[/size]
[url=http://murga-linux.com/puppy/viewtopic.php?p=1028256#1028256][size=75]Fatdog multi-session usb[/url][/size]
[size=75][url=https://hashbang.sh]echo url|sed -e 's/^/(c/' -e 's/$/ hashbang.sh)/'|sh[/url][/size]

Post Reply