D&C GLug - Home Page

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

[LUG]Re: writing shell scripts

 

On 2025-10-29 23:29, Tim Dobson wrote:

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 

Hi Tim

Thanks for this,  For the moment I have run out of material to send out so am going to work on more material and take a look at the solutions presented on the list as I can make adaptations.

I think it depends what I am sending out if there is a single message, message + Image or just am image,  which will of course need the additional alt-textl.

Thanks again

Paul

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