D&C GLug - Home Page

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

[LUG]Re: writing shell scripts

 

Hey Paul,

Your approach is actually pretty close! Using sed to remove the first line works, though there are a few ways to make this cleaner and more robust.

For tracking which URL you've posted, I'd suggest keeping a simple counter in a separate file rather than modifying your URL list each time. That way you keep your original list intact and can easily reset or adjust things. Something like:

#!/bin/bash

URLS_FILE="urls.txt"
COUNTER_FILE="counter.txt"

# Initialize counter if it doesn't exist
if [ ! -f "$COUNTER_FILE" ]; then
    echo "1" > "$COUNTER_FILE"
fi

# Read current line number
LINE_NUM=$(cat "$COUNTER_FILE")

# Get the URL at that line
URL="" -n "${LINE_NUM}p" "$URLS_FILE")

# Check if we got a URL (in case we've run out)
if [ -z "$URL" ]; then
    echo "No more URLs in list"
    exit 1
fi

# Your toot command would go here
echo "Posting: $URL"
# toot post "Check out this week's Code Club activity: $URL"

# Increment counter for next time
echo $((LINE_NUM + 1)) > "$COUNTER_FILE"
```

For the wrapper text, you could either:
1. Have a template in the script itself (easy to maintain)
2. Store "URL|description" pairs in your file and split them
3. Have separate files like you mentioned, though that might get a bit messy

The cron entry for Monday 2am would be:
```
0 2 * * 1 /path/to/your/script.sh

One thing to consider - if you want this cycling forever, you might want to add logic to reset the counter when it reaches the end of your list. Also worth having the script log its output somewhere so you can check it's actually running.

Does that help point you in the right direction?

Cheers,

-Tim




On Fri, 24 Oct 2025 at 16:36, Paul Sutton via list <list@xxxxxxxxxxxxx> wrote:
Hi

I am trying to write a shell script that when run will read a text file,
which contains a list e.g

item1
item2
item3

and for now echo the first line to stdout and in the case of what I have
send to another text file

#display first line of a file
head -n 1 lines.txt > topline.txt
#delete first line of a file
sed -i -e "1d" lines.txt
cat topline.txt

But I don't think this is at all the correct way to do this.  I am
trying to find out how,  but

The ultimate aim of will be to

Take a text file or list of urls (lets say Code Club Activities)
Run the script say, every monday at 02:00 am
each time the script is run, it will use the toot (mastodon cli tool) to
send that url to a Mastodon account.
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.

I should not have a program getting a shell script to run at an allotted
time each week using cron.

So for example

https://projects.raspberrypi.org/en/projects/space-talk
https://projects.raspberrypi.org/en/projects/catch-the-bus
https://projects.raspberrypi.org/en/projects/find-the-bug
https://projects.raspberrypi.org/en/projects/silly-eyes
https://projects.raspberrypi.org/en/projects/surprise-animation

I had some help before with regard to reading an array containing a list
of files, which then runs on all entries in the array. However, in this
case the script will run and act on the first item,   then run again a
week later and act on the 2nd item.

So it needs a way to track which item to send.

So over a period of 5/6 weeks I send out a different activity each week.

However, I also appreciate that sending out just a url is not very
friendly or helpful,  so there should be some text wrapped around it.

I could perhaps create a series of text files with url and wrapper text.

Hopefully, this can be run on a Raspberry pi,  and it can just be left
on and do tasks at allotted times.

Could someone help / advise please.

Thanks for any help


Paul


--
Paul Sutton, Cert Cont Sci (Open)
https://zleap.net/
Mastodon : https://techhub.social/@zleap

--
The Mailing List for the Devon & Cornwall LUG
FAQ: https://www.dcglug.org.uk/faq/
-- 
The Mailing List for the Devon & Cornwall LUG
FAQ: https://www.dcglug.org.uk/faq/