"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
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 77 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)

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

#51 Post by fredx181 »

musher0 wrote: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.
Hi musher0, I couldn't find 'cal' in your pet.
On my Debian stretch system none of codes here work unless I change 'cal' to 'busybox cal' (not a problem, btw, just curious about 'cal (the full version)'.
(on my system /usr/bin/cal is symlink to /usr/bin/ncal part of package bsdmainutils)

Fred

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

#52 Post by musher0 »

fredx181 wrote:
musher0 wrote: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.
Hi musher0, I couldn't find 'cal' in your pet.
On my Debian stretch system none of codes here work unless I change 'cal' to 'busybox cal' (not a problem, btw, just curious about 'cal (the full version)'.
(on my system /usr/bin/cal is symlink to /usr/bin/ncal part of package bsdmainutils)

Fred
By golly, you're right, Fred.
I mistook cat for cal...
Sorry about that. (Note to myself : must get new glasses...)

Back hunting. TWYL.

~~~~~~~~~~~
Edit, 5 minutes later --
cal is part if the utils-linux package, whose latest source is here.

Edit, an hour later --
I have compiled the latest utils-linux package, version 2.33, for
PuppyLinux. It is available through here.

Again thanks to Fredx181 for having a falcon's eye.

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

#53 Post by MochiMoppel »

musher0 wrote: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:"
There is more to learn from puppy_apprentice's code:
Reread how he fixed the date command to prepare for single digit dates and how he uses the -w option for the grep command, a clever trick. On the first day of the month grep will match only '1' but not '10' or '21'

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

#54 Post by musher0 »

Hi, MM.

I'm sure. But not now, please.

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

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

#55 Post by fredx181 »

musher0 wrote:I have compiled the latest utils-linux package, version 2.33, for
PuppyLinux. It is available through here.
Thanks, the cal included works fine for me.

P.S. see screenshot , running just cal, note that 'busybox cal' is not translated, cal from utils-linux and ncal are in Dutch, as my system locale is set (and ncal looks very different)
(and translated, the week goes from monday to sunday)

Fred
Attachments
2018-11-22-201728_839x563_scrot_639x429.png
Different cal's
(79.24 KiB) Downloaded 137 times

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

#56 Post by musher0 »

@fredx181:
With the "real" < cal >, you have to say < cal -s > for the week to start
on Sunday.
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

#57 Post by musher0 »

@MochiMoppei and puppy_apprentice:
I studied it, but I really do not understand the random color part in
puppy_apprentice's script. Worse: I do not understand the need for it! :(

In particular, the part X1X2, etc. and the line of dates. How do you get
that XIX2, etc. part to appear? In an hex editor? I could not reproduce
it by any means I know.

Sorry.

~~~~~~~~~~
I did catch the need for < grep -wn $(date '+%d'>, however; not just
< grep -n >.

And MM, any reason why your syntax is different than mine for
< date '+%d' >? They give the same result.

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

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

#58 Post by fredx181 »

musher0 wrote:@fredx181:
With the "real" < cal >, you have to say < cal -s > for the week to start
on Sunday.
Ah, ok, btw for me with LANG=en_US the week starts automatically on Sunday without the -s switch :

Code: Select all

root@live:~# LANG=en_US cal  # full 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  
So somehow it seems to detect which locale.

Fred

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

#59 Post by musher0 »

Hi Fred.

Thanks for the feedback.

The authors must've improved it. In older versions that I've used, the -s
was needed if you wanted the week to start on Sunday.

As well, you may have noticed that the real cal highlights today's date,
whereas the busybox cal does not.

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

#60 Post by MochiMoppel »

musher0 wrote:And MM, any reason why your syntax is different than mine for
< date '+%d' >? They give the same result.
Exactly which syntax are you referring to?

Post Reply