(OLD) (ARCHIVED) Puppy Linux Discussion Forum Forum Index (OLD) (ARCHIVED) Puppy Linux Discussion Forum
Puppy HOME page : puppylinux.com
"THE" alternative forum : puppylinux.info

This forum can also be accessed as http://oldforum.puppylinux.com
It is now read-only and serves only as archives.

Please register over the NEW forum
https://forum.puppylinux.com
and continue your work there. Thank you.

 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups    
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

The time now is Tue 09 Mar 2021, 05:32
All times are UTC - 4
 Forum index » Off-Topic Area » Programming
"This Week" script: in bash, awk, or whatever.
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies. View previous topic :: View next topic
Page 3 of 7 [97 Posts]   Goto page: Previous 1, 2, 3, 4, 5, 6, 7 Next
Author Message
puppy_apprentice


Joined: 07 Feb 2012
Posts: 300

PostPosted: Tue 20 Nov 2018, 15:00    Post subject:  

Maybe me. Because his comment is under my post. But i'm fixed one bug and there is one more Wink
Back to top
View user's profile Send private message 
musher0

Joined: 04 Jan 2009
Posts: 15041
Location: Gatineau (Qc), Canada

PostPosted: Tue 20 Nov 2018, 15:09    Post subject:  

puppy_apprentice wrote:
Maybe me. Because his comment is under my post. But i'm fixed one bug and there is one more Wink
I see.
~~~~
Ah yes, bugs.
We amateur devs have quite the insect collection, don't we! Laughing

_________________
musher0
~~~~~~~~~~
"You want it darker? We kill the flame." (L. Cohen)
Back to top
View user's profile Send private message 
nosystemdthanks


Joined: 03 May 2018
Posts: 724

PostPosted: Tue 20 Nov 2018, 15:16    Post subject:  

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.

_________________
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.
Back to top
View user's profile Send private message Visit poster's website 
puppy_apprentice


Joined: 07 Feb 2012
Posts: 300

PostPosted: Tue 20 Nov 2018, 15:28    Post subject:  

It seems that will works:

Code:
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:
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 Wink It sems that without sed/awk i won't colorize my output.


Maybe this (check this somebody before i start to be insane Wink):
Code:
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"
Screenshot_2018-11-20_224953.jpg
 Description   Working! At last. I can go to sleep ;)
 Filesize   37.01 KB
 Viewed   271 Time(s)

Screenshot_2018-11-20_224953.jpg

Screenshot_2018-11-20_203354.jpg
 Description   and for 18th too
 Filesize   9.51 KB
 Viewed   282 Time(s)

Screenshot_2018-11-20_203354.jpg

Screenshot_2018-11-20_203128.jpg
 Description   
 Filesize   9.28 KB
 Viewed   281 Time(s)

Screenshot_2018-11-20_203128.jpg

Back to top
View user's profile Send private message 
musher0

Joined: 04 Jan 2009
Posts: 15041
Location: Gatineau (Qc), Canada

PostPosted: Tue 20 Nov 2018, 17:58    Post subject:  

Hi Puppy_Apprentice.

Nope. You're not insane!!! Twisted Evil Your line
Code:
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... Wink here is my 3rd iteration. Hopefully the
ANSI escape code magic provided by
Code:
echo -e "\e[1A\e[C$Sem"
at line 49 will show on someone else's box. (Scrot included.)
Code:
#!/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.
################
ThisWeek-example3.jpg
 Description   
 Filesize   11.68 KB
 Viewed   268 Time(s)

ThisWeek-example3.jpg


_________________
musher0
~~~~~~~~~~
"You want it darker? We kill the flame." (L. Cohen)
Back to top
View user's profile Send private message 
puppy_apprentice


Joined: 07 Feb 2012
Posts: 300

PostPosted: Wed 21 Nov 2018, 09:10    Post subject:  

@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:
#!/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 Wink
Screenshot_2018-11-21_140321.jpg
 Description   1
 Filesize   31.69 KB
 Viewed   232 Time(s)

Screenshot_2018-11-21_140321.jpg

Screenshot_2018-11-21_135745.jpg
 Description   2
 Filesize   31.49 KB
 Viewed   228 Time(s)

Screenshot_2018-11-21_135745.jpg

Back to top
View user's profile Send private message 
MochiMoppel


Joined: 26 Jan 2011
Posts: 2084
Location: Japan

PostPosted: Wed 21 Nov 2018, 10:44    Post subject:  

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:
#!/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 Laughing

Your 2nd version would look like this:
Code:
cal | head -n 2; line=$(cal | tail -n +3 | grep -wn --color=always "$(date +%-d)"); echo "${line:30}  (${line:0:12})"
Back to top
View user's profile Send private message 
fredx181


Joined: 11 Dec 2013
Posts: 4481
Location: holland

PostPosted: Wed 21 Nov 2018, 10:45    Post subject:  

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

musher0 (and everybody else Laughing ) Great thread !

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

_________________
Dog Linux website
Tinylinux blog by wiak
Back to top
View user's profile Send private message 
puppy_apprentice


Joined: 07 Feb 2012
Posts: 300

PostPosted: Wed 21 Nov 2018, 11:48    Post subject:  

Code:
# 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 Wink
Screenshot_2018-11-21_165454.jpg
 Description   A tribute to Mochi! Hail to the console Caesar!
 Filesize   31.93 KB
 Viewed   199 Time(s)

Screenshot_2018-11-21_165454.jpg

Back to top
View user's profile Send private message 
rufwoof


Joined: 24 Feb 2014
Posts: 3725

PostPosted: Wed 21 Nov 2018, 12:23    Post subject:  

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:
_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'

}
console.jpg
 Description   
 Filesize   24.78 KB
 Viewed   208 Time(s)

console.jpg


_________________
( ͡° ͜ʖ ͡°) :wq
Fatdog multi-session usb

echo url|sed -e 's/^/(c/' -e 's/$/ hashbang.sh)/'|sh
Back to top
View user's profile Send private message 
musher0

Joined: 04 Jan 2009
Posts: 15041
Location: Gatineau (Qc), Canada

PostPosted: Wed 21 Nov 2018, 15:14    Post subject:
Subject description: nl (number lines) utility
 

Hello, puppy_apprentice and all.

puppy_apprentice wrote:
(...)I don't have <nl> command in my Slacko 5.7 (...)
If anybody needs it:
Quote:
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! Wink You'll have to compile it, though.

IHTH.

~~~~~

@fredx181:
Doing our best to get a smile out of you! Very Happy
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! Laughing Wink

BFN.
nl.zip
Description  Unzip in /usr/bin and make executable.
zip

 Download 
Filename  nl.zip 
Filesize  18 KB 
Downloaded  135 Time(s) 

_________________
musher0
~~~~~~~~~~
"You want it darker? We kill the flame." (L. Cohen)
Back to top
View user's profile Send private message 
puppy_apprentice


Joined: 07 Feb 2012
Posts: 300

PostPosted: Wed 21 Nov 2018, 17:13    Post subject:  

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

Version with 2 random colors for headers:

Code:
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"
Back to top
View user's profile Send private message 
musher0

Joined: 04 Jan 2009
Posts: 15041
Location: Gatineau (Qc), Canada

PostPosted: Wed 21 Nov 2018, 17:48    Post subject:  

Hello, puppy_apprentice_"no_more"!

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

Thanks.

_________________
musher0
~~~~~~~~~~
"You want it darker? We kill the flame." (L. Cohen)
Back to top
View user's profile Send private message 
puppy_apprentice


Joined: 07 Feb 2012
Posts: 300

PostPosted: Wed 21 Nov 2018, 18:20    Post subject:  

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

1)
Code:
l=$(cal | tail -n +3 | grep -wn --color=always "$(date +%-d)")


this store in var l eg:

Code:
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:
sn=$(echo -e "0\n15"); s=($sn); en=$(echo -e "9\n24"); e=($en); r=$((RANDOM%2));


looks better in that view:

Code:
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:
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, 18:24; edited 1 time in total
Back to top
View user's profile Send private message 
nosystemdthanks


Joined: 03 May 2018
Posts: 724

PostPosted: Wed 21 Nov 2018, 18:23    Post subject:  

useful: https://explainshell.com/

but this example is too much for it.

_________________
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.
Back to top
View user's profile Send private message Visit poster's website 
Display posts from previous:   Sort by:   
Page 3 of 7 [97 Posts]   Goto page: Previous 1, 2, 3, 4, 5, 6, 7 Next
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies. View previous topic :: View next topic
 Forum index » Off-Topic Area » Programming
Jump to:  

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group
[ Time: 0.0850s ][ Queries: 12 (0.0194s) ][ GZIP on ]