"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
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]

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

#41 Post by musher0 »

Hello, puppy_apprentice and all.
puppy_apprentice wrote:(...)I don't have <nl> command in my Slacko 5.7 (...)
If anybody needs it:
nl (GNU coreutils) 8.25
Copyright (C) 2016 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Written by Scott Bartram and David MacKenzie.
The attached one is from my XenialPup-706. If it does not work on your
Pup, the latest original is in this pile! ;) You'll have to compile it, though.

IHTH.

~~~~~

@fredx181:
Doing our best to get a smile out of you! :D
You're more than welcome to chip in if inspired, BTW.

@puppy_apprentice:
puppy_apprentice wrote:(...)I (...) have idea for console pim - THE OSMO KILLER
Go-go-go, puppy_apprentice!
With your creativity, OSMO's domination is sooo over! :lol: ;)

BFN.
Attachments
nl.zip
Unzip in /usr/bin and make executable.
(18 KiB) Downloaded 76 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

#42 Post by puppy_apprentice »

I just can't get enough ;) I'm starting to be addicted to bash.

Version with 2 random colors for headers:

Code: Select all

sn=$(echo -e "0\n15"); s=($sn); en=$(echo -e "9\n24"); e=($en); r=$((RANDOM%2)); l=$(cal | tail -n +3 | grep -wn --color=always "$(date +%-d)"); echo "${l:s[$r]:6}"; cal | head -n 2; echo -e "${l:e[$r]:3}${l:30} (${l:0:12})\n"

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

#43 Post by musher0 »

Hello, puppy_apprentice_"no_more"!

You lost me, there!
Please explain what you're doing, to us simple mortals? :)

Thanks.
musher0
~~~~~~~~~~
"You want it darker? We kill the flame." (L. Cohen)

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

#44 Post by puppy_apprentice »

I think that you know all the bash magic like Mochi, but ok i will explain my tricks:

1)

Code: Select all

l=$(cal | tail -n +3 | grep -wn --color=always "$(date +%-d)")
this store in var l eg:

Code: Select all

X14X2Y1:Y218 19 20 21 22 23 24
where X1/X2 - starts/ends ANSI code for color green and Y1/Y2 starts/ends ANSI code for color light blue.

2)

Code: Select all

sn=$(echo -e "0\n15"); s=($sn); en=$(echo -e "9\n24"); e=($en); r=$((RANDOM%2));
looks better in that view:

Code: Select all

sn=$(echo -e "0\n15")
s=($sn)
en=$(echo -e "9\n24")
e=($en)
r=$((RANDOM%2));
make two arrays with colors coordinates in string stored in var l, s for starting coordinates and e - for ending coordinates:
for green: X1 = 0, X2 = 9
for l. blue: Y1 = 15, Y2 = 24

r store 0 or 1, so if we get 0 we get start/end X1/X2 coordinates for ANSI code for color green

3) that we can put there:

Code: Select all

echo "${l:s[$r]:6}"; cal | head -n 2; echo -e "${l:e[$r]:3}${l:30} (${l:0:12})\n"
Last edited by puppy_apprentice on Wed 21 Nov 2018, 22:24, edited 1 time in total.

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

#45 Post by nosystemdthanks »

useful: https://explainshell.com/

but this example is too much for it.
[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

#46 Post by puppy_apprentice »

musher0 this code you can use in your scripts:

Code: Select all

#!/bin/sh
colors=$(echo -e "34\n35\n36\n37"); colmatrix=($colors); r=$((RANDOM%4)); echo -e "\e[${colmatrix[$r]}m Hello World! \e[0m"
Attachments
Screenshot_2018-11-22_000937.jpg
Immortal &quot;Hello Worlds!&quot; in 4 ANSI colors
(46.9 KiB) Downloaded 255 times

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

#47 Post by musher0 »

@puppy_apprentice:

Your modesty is quite to be commended. But judging by your recent output,
I'd say that you're well on your way of surpassing at least this supposed
"Bash magic" master... (I.e. me.)

That's fine, and please go for it! I have serious reservations about the
traditional concept of "competition", but I'm all for "emulation".

My best wishes to you in this! :D

BFN.
musher0
~~~~~~~~~~
"You want it darker? We kill the flame." (L. Cohen)

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

#48 Post by puppy_apprentice »

I was thinking about your ThisWeek.sh script in wich you use ANSI codes.

Ok. I have some experience with other programming languages but i'm not fluent in any.

I think that for example i won't write this kind of sed game:

https://aurelio.net/projects/sedsokoban/

Have a nice day/evening all.

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

#49 Post by musher0 »

Thanks for the feedback, puppy_apprentice.

~~~~~~~~~~~
@all:

If anyone is interested, I have made available the latest version of
coreutils, version 8-30, for PuppyLinux, here.

To anyone wondering: the link with this thread is that the GNU coreutilis
contain, among many others, three utilities used in various scripts in this
thread: cal (the full version), date and nl.

BFN.
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

#50 Post by musher0 »

Hello all.

In lieu of < nl > to number lines, one can use < cat -n >:

Code: Select all

>echo;cal | grep -v [[:alpha:]] | cat -n | grep --color=always "$(date '+%d')"
Also possible for this is the use of < grep -n >, as mentioned somewhere
above by puppy_apprentice IIRC:

Code: Select all

echo;cal | grep -v [[:alpha:]] | grep -n --color=always "$(date '+%d')" | tr ":" "\t";echo -e "\e[1A\e[1Cth wk:"
This is completed by a replacement, through < tr >, of the colon by a tab,
and then we insert for information the string "th wk:" between the week
number and the line of dates, through a couple of ANSI escape codes.

We can decorate it like so:

Code: Select all

echo;echo -e "\t      \e[33m\e[4m`date '+%b %Y'`\e[0m";cal | grep -v [[:alpha:]] | grep -n --color=always "$(date '+%d')" | tr ":" "\t";echo -e "\e[1A\e[1Cth wk:";echo -e "\t\e[33m`cal | grep -v [[:digit:]]`\e[0m"
Scrot attached.

BFN.
Attachments
ThisWeek-example4.jpg
(23.91 KiB) Downloaded 221 times
musher0
~~~~~~~~~~
"You want it darker? We kill the flame." (L. Cohen)

Post Reply