D&C GLug - Home Page

[ Date Index ] [ Thread Index ] [ <= Previous by date / thread ] [ Next by date / thread => ]

[LUG]Re: writing shell scripts

 

Hi Paul,

A lot of my job involves writing scripts to do stuff, as does any sysadmin. One thing I've learned is that the correct way to do something is whatever way works for you.

Unless you're required to adhere to a particular standard (which isn't unusual), then one of the luxuries we can have is to use our own tools. For me, and this problem, it would be bash. I prefer writing code in perl, but there's a pleasing symmetry in chaining together linux's tools to do some pretty complex things. Things like head, sed, awk, date etc are astonishingly powerful when you start to read their manpages.

Enough philosophy; your outline:

1 Take a text file or list of urls (lets say Code Club Activities)
2 Run the script say, every monday at 02:00 am
3 each time the script is run, it will use the toot (mastodon cli tool) to
4 send that url to a Mastodon account.
5 then the next time it is run,  send the next url in the list.

> I think I need to start by identifying the correct way to do this,  then
write or get help writing the a script to do this.

For me, that would be something like (not tested)

Scheduling, crontab always;

0 2 * * MON /path/to/your/script.sh

One method in two steps

Read the first line of the text file and pass it to toot as an argument. Then delete the first line so you don't re-send it.

Eg;

---
#!/bin/bash

toot "$(head -n 1 /path/to/file.txt)"
sed -i '1d' /path/to/file.txt
---

(Add whatever arguments you need to toot to specify server and channel and auth if needed)

There are thousands of ways to do this, and the above is a very quick and dirty way.

And Oliver gives very useful advice. AI such as Chatgpt, Claudebot, Gemini et all are very very good at this sort of stuff. I've played around and written entire web apps using gemini within five minutes, using my preferred perl webstack (plack) and it did it in a way that I learned new stuff about plack despite having used it for over a decade.

Or just as a reminder. Eg, sed above, ask it, "bash, how delete first line of a text file". Boom, several answers. Honestly, this year has been a revelation for linux users in the ability of these assistants to genuinely offer good advice on technical aspects. Just... don't give them anything confidential...
-- 
The Mailing List for the Devon & Cornwall LUG
FAQ: https://www.dcglug.org.uk/faq/