💾 Archived View for yretek.com › english › 2023-04-11_microblog_script.en.gmi captured on 2023-09-28 at 15:53:54. Gemini links have been rewritten to link to archived content

View Raw

More Information

⬅️ Previous capture (2023-04-19)

🚧 View Differences

-=-=-=-=-=-=-

Yretek - My microblog.sh micro-blogging script

Warning: Average to veteran coders will find this one pretty boring and too detailed.

So, this one seems to work. It is meant to facilitate keeping a micro-blog of sorts in my own gemini capsule. I don't get to like it a lot because BASH is out of my comfort zone, and besides, I'm not too happy about the way BASH syntax works with variables, concatenation and all that. But it seems to work, and that's good enough to share. Perhaps it might help somebody who knows even less than me, or, hopefully somebody can catch a bug lurking in it, and/or improve it.

Anyway, for whatever it's worth:

Part 1: getting the data

#!/usr/bin/env bash

echo "Entry: "
read -r entry

echo "Link: leave empty for no link"
read -r alink


entrydate=$(date -Iseconds)
shortdate=$(date -I)

As a very BASIC ;) program it starts asking input from the user: give me an entry. This can be as long as a single paragraph; anything else, in my book, should have its own entry in my capsule. Again, I coded this for my own needs and visions... Then, it asks you for an optional link. What I mean for link is a reference for my entry, so the reader can know what I'm talking about, as in "Visited Ullapool yesterday \n => example.org/ullapool.gmi".

Second, it gets two date/time values from the system. date -Iseconds makes linux give you the time in this convenient format 2023-04-11T15:51:06+01:00, so it's the date and the time up to seconds. That will be the value for $entrydate. date -I gives a date in the "gemini-friendly" way of YYYY-MM-DD, so 2023-04-11 in our case. That's what $shortdate will contain.

Part 2 back it up, stupid

Just a single, but critical, line.

cp old.gmi "bu/${entrydate}_old.gmi"

Next, there's a file called old.gmi . That one contains all the old (previous) entries in the microblog. I copy that one to a buck up folder, with entrydate included in the name. That way it would be, I hope, easy to rebuild everything up in case I manage to mess it all up, again.

Part 3 Build the entry

echo "" > new.gmi
echo "## $entrydate " >> new.gmi
echo "$entry" >> new.gmi
if [ "$alink" != '' ]; then
	echo "=> ${alink}" >> new.gmi
fi

cat old.gmi >> new.gmi
cat head.gmi > output.gmi
cat new.gmi >> output.gmi

The command 'echo' is working as 'print' would in more sensible languages. Anyway, I begin by creating a new file called... new.gmi; I could have used 'touch' but... I don't know, everything's fine citizen, move on. On the next line notice my masterful use of '>>'. That thing appends entrydate as a level 2 header to the new.gmi.

Watch out!

The '>>' thingy appends to a file,'>' saves the file with new content, but destroying everything that was there before. (Another good reason to back things up).

Moving on

So, of course, echo "$entry" >> new.gmi adds my new entry to new.gmi. Then there's an if that will add a link if there's a link to add. I suppose my reader knows what an "if" and else she or he or hän can search the net for conditional statements and such.

Next, cat will append the contents of old.gmi into new.gmi. Which means the contents of old.gmi be placed after the new content in new.gmi, placing my new entry at the top, as it's the common practice in the Internet.

Finally there's this little thing head.gmi which contains the title of my microblog and little else. I put that into the output file, called, output.gmi . Uncreative, but easy to determine what's for. And to that output I append new.gmi. So now I have, my microblog header, the new entry, and the list of the old entries, all conveniently stored in output.gmi

And everything goes to its place

mv new.gmi old.gmi
mv output.gmi /home/someuser/yretek/english/weekens.en.gmi

sed -i  "3s/202[0-9]-[0-1][0-9]-[0-3][0-9]/$shortdate/" /home/someuser/yretek/english/index.gmi 

Nothing fancy here. First new become the new old, literally. (Remember, I backed it up first thing). Second I move output.gmi to its place (it's actually fake, but you get the idea, but my microblog is actually called weekens, because I love Alba and such). The .en is there so my server (agate) knows that one is in English (or something similar enough).

Finally, that sed line goes to the third (3) line of my index.gmi and replaces an old date (the last time weekens was updated) with the new ones. So my readers know there's something new to read, if they are so inclined..

So that's all. The whole thing should be bellow these lines for you to do whatever you feel is best to.

The obligatory self-promoting link.

The whole beast below

#!/usr/bin/env bash

echo "Entry: "
read -r entry

echo "Link: leave empty for no link"
read -r alink


entrydate=$(date -Iseconds)
shortdate=$(date -I)

cp old.gmi "bu/${entrydate}_old.gmi"

echo "" > new.gmi
echo "## $entrydate " >> new.gmi
echo "$entry" >> new.gmi
if [ "$alink" != '' ]; then
	echo "=> ${alink}" >> new.gmi
fi

cat old.gmi >> new.gmi
cat head.gmi > output.gmi
cat new.gmi >> output.gmi

mv new.gmi old.gmi
mv output.gmi /home/someuser/yretek/english/weekens.en.gmi

sed -i  "3s/202[0-9]-[0-1][0-9]-[0-3][0-9]/$shortdate/" /home/someuser/yretek/english/index.gmi 

Footer

~ Miguel de Luis Espinosa

yretek@proton.me

@yretek@im-in.space My Mastodon.