1

An application called "vagrant packer" always downloads the iso and checksum file from the remote location every time I run it, so I'm creating a script that downloads the iso to a local location, but only if the published_at date is higher than my local one. If it is, I download the iso and checksum and then store the published date on a local .conf file.

So I try to get the download url of the checksum from the project's github api url, however the download_url needs to first not match and then match a pattern.

It needs to not contain "unstable" and end with ".iso.sha512".

run.sh

...
    echo downloading checksum..
    checksum_download_url=$(curl -s $github_latest_api_url | jq -r '.assets[].browser_download_url|select(match(".(?!unstable).iso.sha512"))')
...

As you can see, I'm trying to first get the correct download url, before doing a curl download command in the next line.

I've made an attempt, but I just can't figure out what the regex of the match function.
I'm still getting either no result, a syntax error, or the unstable line included.

#!/bin/bash

source ./uploads/mai-base.conf

prerelease=$(curl -s $github_latest_api_url | jq -r '.prerelease')

if [ !prerelease ]; then
  
  r_publish_date=$(date -d $(curl -s $github_latest_api_url | jq -r '.published_at') +%s)
  l_publish_date=$(date -d $publish_date +%s)

  if [ $r_publish_date -gt $l_publish_date ]; then
    echo downloading checksum..
    checksum_download_url=$(curl -s $github_latest_api_url | jq -r '.assets[].browser_download_url|select(match(".(?!unstable).iso.sha512"))')
    echo $checksum_download_url
    #curl -L $download_url 
    echo downloading iso..
    # downloading iso code goes here..

    # date saving to mai-base.conf goes here..
  fi
fi

# Start packer application..
# PACKER_LOG=1 packer build -var $ISO_VERSION manjaro-arm-installer.pkr.hcl

1 Answer 1

2

Here, I would probably do two separate tests for simplicity, instead of trying to get negative/positive look-behinds/aheads right. Also, use test() rather than match() if you're just interested in whether a regular expression matches or not:

jq '.assets[].browser_download_url|select( (test("unstable")|not) and test("iso.sha512$"))' 

As for your script, you use [ !prerelease ] which is not using the prerelease variable. What you probably wanted to say was

if [ "$prerelease" != false ]; then

This could be done without an extra variable, though:

if ! curl -s "$github_latest_api_url" | jq -e .prerelease >/dev/null
then
    ...
fi

The -e option to jq makes the utility exit with an exit status that depends on the last evaluation.

To avoid having to fetch your JSON document three times, save the fetched JSON document in a temporary file. To avoid having to parse it three times, get all the data out of it in one go:

curl -s ... |
jq -r '[.prerelease, .published_at, (.assets[].browser_download_url|select( (test("unstable")|not) and test("iso.sha512$"))) ] | @tsv' |
{
    IFS=$'\t' read -r prerelease r_publish_date checksum_download_url
    if [ "$prerelease" != false ]; then
        # use "$r_publish_date" and "$checksum_download_url"
    fi
}
1
  • With some minor tweaks this worked. ``` if [ $prerelease == false ]; the ``` Because it should run when it's not a prerelease. ``` test(".iso.sha512"), test("iso.z01"), test("iso.zip") ``` Because I want the iso file as well, but for unknown reasons it's split in two, so I need the z01 file as well.
    – Folaht
    Jan 2, 2023 at 6:55

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .