(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 Fri 26 Feb 2021, 23:17
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 5 of 7 [97 Posts]   Goto page: Previous 1, 2, 3, 4, 5, 6, 7 Next
Author Message
musher0

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

PostPosted: Fri 23 Nov 2018, 01:00    Post subject:  

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)
Back to top
View user's profile Send private message 
MochiMoppel


Joined: 26 Jan 2011
Posts: 2084
Location: Japan

PostPosted: Fri 23 Nov 2018, 02:58    Post subject:  

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

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

PostPosted: Fri 23 Nov 2018, 04:12    Post subject:  

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)
Back to top
View user's profile Send private message 
MochiMoppel


Joined: 26 Jan 2011
Posts: 2084
Location: Japan

PostPosted: Fri 23 Nov 2018, 04:17    Post subject:  

http://www.murga-linux.com/puppy/viewtopic.php?p=1010839#1010839
Back to top
View user's profile Send private message 
musher0

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

PostPosted: Fri 23 Nov 2018, 04:27    Post subject:  

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: Fri 23 Nov 2018, 04:38    Post subject:  

musher0 wrote:
@MochiMoppei and puppy_apprentice:
I studied it, but I really do not understand the random color part in
puppy_apprentice's script.


Forgot for the while my earlier script where i extracted colors from 'cal | grep -wn --color=always "$(date +%-d)"' output and check again this, where colors are written stright in escape sequences:

Code:

#!/bin/sh
colors=$(echo -e "34\n35\n36\n37"); colmatrix=($colors); r=$((RANDOM%4)); echo -e "\e[${colmatrix[$r]}m Hello World! \e[0m"


this is simpler version without bash arrays:

Code:
echo -e "\e[1;3$((RANDOM%9))m Hello World! \e[0m"


this '$((RANDOM%9))' every time the script is ran will give numbers from 0 to 9 so bash will execute eg. this:

Code:
echo -e "\e[1;35m Hello World! \e[0m"


musher0 wrote:
Worse: I do not understand the need for it! Sad


To make scripts more fancy Wink

Yesterday, i've changed my terminal prompt to this:

Code:
PS1="\[\e[1;3$((RANDOM%9))m\][\w]>\[\e[0m\]"


You can ask why? It is not practical and worthless.

My answer is: Because i can and know how to do this! Wink

musher0 wrote:
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.


No hex editor. All colors in console are ANSI escape sequences. Try this:
Code:
cal | grep -wn --color=always "$(date +%-d)" >output.txt


If you look into output txt you will see escape sequences, where "ESC" you have to read as '\e' and from now you will know how to ectract colors using bash '${string:start:end}' Wink

musher0 wrote:
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.


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.


There was misunderstanding between you both. Mochi was thinking about option 'w' in grep not in date command.
Back to top
View user's profile Send private message 
rufwoof


Joined: 24 Feb 2014
Posts: 3725

PostPosted: Fri 23 Nov 2018, 08:08    Post subject:  

This is mine
ps1.png
 Description   
 Filesize   41.91 KB
 Viewed   280 Time(s)

ps1.png


_________________
( ͡° ͜ʖ ͡°) :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 
trapster


Joined: 28 Nov 2005
Posts: 2125
Location: Maine, USA

PostPosted: Fri 23 Nov 2018, 08:40    Post subject:  

Vertical verson Very Happy

Code:
# for days in {0..6}; do date "+%a %d" --date="$days day" ; done
Fri      23
Sat    24
Sun   25
Mon  26
Tue    27
Wed  28
Thu   29
#

_________________
trapster
Maine, USA

Asus eeepc 1005HA PU1X-BK
Frugal install: Slacko
Currently using full install: DebianDog
Back to top
View user's profile Send private message Visit poster's website 
puppy_apprentice


Joined: 07 Feb 2012
Posts: 300

PostPosted: Fri 23 Nov 2018, 08:58    Post subject:  

rufwoof wrote:
This is mine


Nice Wink

but my prompt changing colors every time (almost, it depends on seed of random i think) i start console Wink

I wanted do make this in one console session, but PS1 var is loaded only once ;(
various-colors-prompt.jpg
 Description   
 Filesize   54.27 KB
 Viewed   274 Time(s)

various-colors-prompt.jpg

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

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

PostPosted: Fri 23 Nov 2018, 14:50    Post subject:  

Hello all.

Here is an article in the Linux Journal about calculating days.
It's tangent to the subject of this thread, I think.
The author uses some awk and some sed.

BFN.

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

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

PostPosted: Fri 23 Nov 2018, 16:25    Post subject:  

@puppy_apprentice:

Regarding your first line in your 2nd cur-week script
Code:
sn=$(echo -e "0\n15")
I think I'm beginning to understand:
Code:
a=(echo blabla);echo ${a[*]}
echo blabla
But:
Code:
a=(echo blabla);${a[*]}
blabla

I was not used to perceiving arrays this way. Your array can be
conceptualized in two ways: as data storage and as a command.

"Eureka" moment! Smile

BFN.

_________________
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: Fri 23 Nov 2018, 17:03    Post subject:  

I've used them as data storage:

Code:
# list of elements a:
a=$(echo -e "blabla\nmamba") #this is version for one liners scripts
#normally is:
#a="blabla
mamba"
#
# list a is assigned to array b:
b=($a)
echo ${b[0]} #prints first (index 0) element from array b - 'blabla'
echo ${b[1]} #prints second (index 1) element from array b - 'mamba'


but it could be written simpler like this:

Code:
b=(blabla mamba)
echo ${b[0]}
echo ${b[1]}


Arrays are good you can sometimes use them instead of IF or CASE:

Code:
#!/bin/sh
daysofweek=(Monday Tuesday Wendesday Thursday Friday Saturday Sunday) # $1 = 0 to 6
echo "You have choosen ${daysofweek[$1]}"

#it is simpler and shorter than this:
echo "You have choosen:"
case $1 in
  0) echo Monday ;;
  1) echo Tuesday ;;
  2) echo Wendesday ;;
  3) echo Thursday ;;
  4) echo Friday ;;
  5) echo Saturday ;;
  *) echo Sunday
esac

commands=($(echo ${RANDOM}) $(date +%d)) #$1 =  0 or 1
output=${commands[$1]}
echo $output
Back to top
View user's profile Send private message 
puppy_apprentice


Joined: 07 Feb 2012
Posts: 300

PostPosted: Fri 23 Nov 2018, 21:08    Post subject:  

musher0 wrote:
Hello all.

Here is an article in the Linux Journal about calculating days.
It's tangent to the subject of this thread, I think.
The author uses some awk and some sed.

BFN.


I've written script for that without sed and awk:
Code:
#!/bin/sh
#dow.sh

day=$1
month=$2
year=$3

# array with names of days
nod=($(cal $month $year | head -2 | tail -1))

# vertical list of days in week that include $day
cdl=$(cal $month $year | grep -w "$day" | grep -Eo "[0-9]*[0-9]*")

# number of days in week that include $day
nad=$(echo "$cdl" | grep -Ec "[0-9]*[0-9]*")

# number of days in week before $day
ndb=$(echo "$cdl" | grep -wB 7 "$day" | grep -Ec "[0-9]*[0-9]*")

# number of days in week after $day
nda=$(echo "$cdl" | grep -wA 7 "$day" | grep -Ec "[0-9]*[0-9]*")

if (( $nad == 7 ));
then
   echo "$day/$month/$year is ${nod[ndb-1]}"
fi

if (( $nad < 7 )) && (( $day < 7 ));
then
   echo "$day/$month/$year is ${nod[7-nda]}"
fi

if (( $nad < 7 )) && (( $day > 7 ));
then
   echo "$day/$month/$year is ${nod[ndb-1]}"
fi


Works for tests from article:

Code:
DOW of 3 june 2011 = 6
DOW of 1 july 2011 = 6
DOW of 2 may 2011 = 2
DOW of 16 may 2011 = 2
Back to top
View user's profile Send private message 
MochiMoppel


Joined: 26 Jan 2011
Posts: 2084
Location: Japan

PostPosted: Sat 24 Nov 2018, 03:39    Post subject:  

musher0 wrote:
Here is an article in the Linux Journal about calculating days.

Laughing What I liked best is his remark "The solution I'm going to work with is likely more complicated than necessary, but it's mine and I'm sticking with it."

Complicated indeed. He didn't explain why he uses cal. He *could* have done this with a single date command, but I guess that would have been too easy - and wouldn't fill the page in the Linux Journal.

puppy_apprentice wrote:
I've written script for that without sed and awk:

Nice exercise!
Why do you stress that you didn't use sed or awk? A challenge? IMO grep is the least powerful tool for this kind of job, but as we see it can get the work done.

I've also written a script, using even less tools. Should produce the same result. No grep, no head, no tail, Just bash and cal. To increase the fun, no comments either Twisted Evil
Code:
#!/bin/bash
day=3
month=6
year=2011

CAL=$(cal $month $year)
CAL=${CAL#*$'\n'}
CAL=(${CAL//   / x })
while [[ ${CAL[$i]} != $day ]] ;do ((i++)); done
echo "$day/$month/$year is ${CAL[$((i%7))]}"
Back to top
View user's profile Send private message 
puppy_apprentice


Joined: 07 Feb 2012
Posts: 300

PostPosted: Sat 24 Nov 2018, 04:19    Post subject:  

MochiMoppel wrote:

Nice exercise!
Why do you stress that you didn't use sed or awk? A challenge? IMO grep is the least powerful tool for this kind of job, but as we see it can get the work done.


It is mine and i'm sticking with it. Laughing

I've thought too that it can be written simpler. We have calendar so we don't need many calculations. We need only search trough it.

But yours is much simpler. Did i call you a Ceasar of Console Empire earlier? Wink

But my (algorithm) is easier to translate to other languages (on first view).

Your solution is nice (you used modulo, very nice function eg. in ciphers eg. ROT13):
[debugging mode on]
Code:
#!/bin/bash
day=3
month=6
year=2011

CAL=$(cal $month $year)

echo "$CAL"

CAL=${CAL#*$'\n'}

echo ${CAL[*]}

CAL=(${CAL//   / x })

echo ${CAL[*]}

while [[ ${CAL[$i]} != $day ]] ;do ((i++)); done
echo "$day/$month/$year is ${CAL[$((i%7))]}"


[debugging mode off]

There are some other solutions.

Some theory and history:
https://en.wikipedia.org/wiki/Perpetual_calendar

Programming algorithms, in Polish, but people familiar with programming languages should understand them:
https://pl.wikipedia.org/wiki/Kalendarz_wieczny

Last edited by puppy_apprentice on Sat 24 Nov 2018, 08:09; edited 1 time in total
Back to top
View user's profile Send private message 
Display posts from previous:   Sort by:   
Page 5 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.3285s ][ Queries: 12 (0.0999s) ][ GZIP on ]