"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

#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 "Hello Worlds!" 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?

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

#61 Post by musher0 »

MochiMoppel wrote:
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?
Hello MM.

In your "date-based" script, earlier, line 6, you have used the form
< date +%w >, without quotes, whereas I thought that single or double
quotes were needed around the date parameters, as in < date '+%w' >.

No big deal, BTW. TIA for any insight.
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

#62 Post by MochiMoppel »

Quotes are not needed as long as the parameter contains no spaces.
Much more important is to understand the difference between date +%d and date +%-d. All your scripts will fail if you don't fix this.

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

#63 Post by musher0 »

MochiMoppel wrote:Quotes are not needed as long as the parameter contains no spaces.
Much more important is to understand the difference between date +%d and date +%-d. All your scripts will fail if you don't fix this.
Please explain.
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

#64 Post by MochiMoppel »


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

#65 Post by musher0 »

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

Post Reply