You are not logged in.

#1 2022-05-18 09:20:13

OlaffTheGreat
Member
Registered: 2013-06-03
Posts: 106

bashcript issues using strftime

Hi folk,

I wrote the following script in order to provide temp text files containing formatted dates (ready to be copied when writing papers),
along with a current month working folder linked to /tmp/papers for quick access (plus empty template pdf files with the corresponding date in the title's stem).

#!/bin/sh
 
toyear=$(date +"%Y")
todaymonth=$(date +"%Y%m")
#yesterdaymonth=$(yesterday date +"%Y%m")
todayday=$(date +"%d")
todaydayy=$(date +"%e")
yesterday=$(date -d "yesterday" +'%d')
yesterdayy=$(date -d "yesterday" +'%e')
tomonthen=$(LC_ALL=en_US.UTF-8 date +"%B")
#yesterdaytomonthen=$(LC_ALL=en_US.UTF-8 yesterday date +"%B")
#... (other localizations) eg:
#tomonthde=$(LC_ALL=de_DE.UTF-8 date +"%B")
#tomonthcn=$(LC_ALL=zh_CN.UTF-8 date +"%B")

val1=$(date +%Y%m%d --date="yesterday")

mkdir /tmp/papers
ln -s $OFFICE/WorkFolder/$todaymonth /tmp/papers/.
touch /tmp/papers/$todaymonth$todayday\ -\ TodayPaper\ -\ .pdf
touch /tmp/papers/$todaymonth$yesterday\ -\ TodayPaper\ -\ .pdf

echo "– Published $todayday $tomonthen $toyear" > /tmp/papers/.timeden.txt
echo "– Published $yesterdayy $tomonthen $toyear" > /tmp/papers/.timeyen.txt
#(same lines with other localization)
xed /tmp/papers/.timeden.txt /tmp/papers/.timeyen.txt
#... (other localizations)

Some issues to address:
- I can't get "st/nd/rd" for "1st/2nd/3rd" and such (no option in strftime?).
- On the 1st of the month, I get a line with 30/31 of the same month instead of the yesterday's (preceding) month
  (my yesterdaymonth variable does not work, or I get last month always, even on following days).
- For the one digit dates (%e) (from the 1st to the 9th of the month),
  the echo command gives the intended gap for the 0, but without suppressing it
  (ie: I get  " – Published May  1st, 2022"
   instead of " - Published May 1st, 2022"
   notice the overnumerous space before 1st).

Any tip would is welcome.


Lenovo Thinkpad x230 i5-3320M 2.6GHz 250GB SSD (M4) 16GB
SSD | BIOS | GPT | BTRFS | Xfce4 | Zsh | Tmux | SpaceVim
* "Aware Newbie" *
Ibus IM for language script support (e.g. 日本語 - 中文)

Offline

#2 2022-05-20 07:20:37

seth
Member
Registered: 2012-09-03
Posts: 29,771

Re: bashcript issues using strftime

- I can't get "st/nd/rd" for "1st/2nd/3rd" and such (no option in strftime?).

None that I'm aware of and it's also a very peculiar crank, https://en.wikipedia.org/wiki/Ordinal_indicator
You'll have to implement that yourself

Try

date +'%Y %B  %e' | read YEAR MONTH DAY

to limit the "date" invocation and also do away w/ pointless whitespace.

On the 1st of the month, I get a line with 30/31 of the same month instead of the yesterday's (preceding) month

To no surprise given the present script.

#yesterdaymonth=$(yesterday date +"%Y%m")

What is "yesterday"?? plan9 binary?

Offline

#3 2022-05-22 12:20:17

OlaffTheGreat
Member
Registered: 2013-06-03
Posts: 106

Re: bashcript issues using strftime

Hi, thank you for your reply.

Ordinals are indeed a tad teedious. Implement it myself will not be within my habilities, so I'll keep changing them manually.

I added a variable valb=$(date +'%Y %B  %e' | read YEAR MONTH DAY) (I noticed the extra space before %e) and fiddle with it. I'll come back with more results.

About that "yesterday", it is only the remnant of experiments from the non-programmer fellow that I am, so, no Plan9 here.


Lenovo Thinkpad x230 i5-3320M 2.6GHz 250GB SSD (M4) 16GB
SSD | BIOS | GPT | BTRFS | Xfce4 | Zsh | Tmux | SpaceVim
* "Aware Newbie" *
Ibus IM for language script support (e.g. 日本語 - 中文)

Offline

#4 2022-05-22 13:44:19

seth
Member
Registered: 2012-09-03
Posts: 29,771

Re: bashcript issues using strftime

date +'%Y %B %e' | read YEAR MONTH DAY

will read the date into the variables $YEAR, $MONTH and $DAY
The stray blank was just for demonstration (cause we've the 21st)

English ordinals are fairly simply to do

case $DAY in
1)
DAY=1st
;;
2)
DAY=2nd
;;
3)
DAY=3rd
*)
DAY=${DAY}st
;;
esac

There's just no locale for that (and the concept doesn't exist in most locales)

"yesterday", it is only the remnant of experiments

You should get proper variables for yesterday by using "date -d yesterday"

Offline

#5 2022-05-22 15:25:09

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 26,877
Website

Re: bashcript issues using strftime

What's the end goal of all of this?  I suspect this could be *much* cleaner and simpler.  Also by simplifying you'll avoid errors that you wont have to work around later.  I doubt you really want the second pdf 'touched' to have a name with today's year and month and yesterday's day number.  On January 1 2023 that would give a the "yesterday" pdf a value of January 31 2023 rather than December 31 2022 which is almost certainly what you'd really want - your approach would give the wrong value for yesterday's pdf on the first day of every month.

Last edited by Trilby (2022-05-22 15:32:13)


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#6 2022-06-01 17:57:27

OlaffTheGreat
Member
Registered: 2013-06-03
Posts: 106

Re: bashcript issues using strftime

The goal is to have template files with pre-formatted title and date ready to use (everyday) for writing papers (with different locales).
Obviously, it can be *much* simpler, that's an easy one. Programmer beginner here.

So, I tried the following code, which lead to a pdf file titled " - TodayPaper - .pdf", and two txt files containing both "– Published ",
plus a link to the parent folder "WorkFolder" instead of the intended current month:

#!/bin/sh

val=$(date +'%Y %B %e' | read YEAR MONTH DAY)

mkdir /tmp/papers
ln -s $OFFICE/WorkFolder/$MONTH /tmpr/papers/.

touch /tmpr/papers/$val\ -\ TodayPaper\ -\ .pdf
echo "_ Published $val" > /tmpr/.timeden.txt

This is a work in progress.
I'll focus on 1st on the month yesterdays, ordinals and locales when I'll get this right first.
For the workload, I use the falty script as it is.

I may require some time to figure all that out, but I'll get it.

Last edited by OlaffTheGreat (2022-06-01 17:58:53)


Lenovo Thinkpad x230 i5-3320M 2.6GHz 250GB SSD (M4) 16GB
SSD | BIOS | GPT | BTRFS | Xfce4 | Zsh | Tmux | SpaceVim
* "Aware Newbie" *
Ibus IM for language script support (e.g. 日本語 - 中文)

Offline

#7 2022-06-01 18:48:21

seth
Member
Registered: 2012-09-03
Posts: 29,771

Re: bashcript issues using strftime

Again

val=$(date +'%Y %B %e' | read YEAR MONTH DAY)

is nonsense. Try

date +'%Y %B %e' | read YEAR MONTH DAY
echo $YEAR
echo $MONTH
echo $DAY

What did you hope to fin in $val?

Offline

#8 2022-06-01 19:28:51

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 26,877
Website

Re: bashcript issues using strftime

Seth, that will not work as the "read" runs in a different subshell due to the pipe.  All in all, I'd avoid all the variables and calculations, and just call date when the filenames / paths are needed, e.g.

mkdir -p /tmp/papers
ln -s $OFFICE/WorkFolder/$(date +%B) /tmp/papers/.

touch /tmp/papers/$(date "+%Y %B %e) - TodayPaper - .pdf
echo "_ Published $(date "+%Y %B %e")" > /tmpr/.timeden.txt
touch /tmp/papers/$(date -d yesterday "+%Y %B %e) - TodayPaper - .pdf

But this still doesn't make any sense.  I'm curious about your end goal, not the steps that you currently are planning to take to acheive the end goal ... because frankly, these steps don't make any sense and are highly error-prone.

Perhaps there could be a script that at the start would define a couple date format strings that could then be used throughout the script for different locales and different days - but this would be written quite a bit differently that what we're currently talking about.  Most likely this would involve a loop through all the locales.  Using a template and a loop would mean there would only be one (or maybe two) actual "touch" commands rather than many - if there are many, any change needs to be made in many places (thus error prone).


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#9 2022-06-01 19:42:24

seth
Member
Registered: 2012-09-03
Posts: 29,771

Re: bashcript issues using strftime

Ah, bash inferiority…

read YEAR MONTH DAY < <(date +'%Y %B %e')

Offline

Board footer

Powered by FluxBB