0

I've tried pretty much everything I know about .sh to get this script to work and I'm now beyond clueless to why it doesn't. A .sh lint says its should work completely fine except for "{" being literal, which I'm not worried about. A cronjob runs this .sh script every 12 hours and each time I test it, all that comes up is "Command 200 not found." as if the 200 error code that i told the .sh script to look for is a command in itself. Any ideas?

#!/bin/bash

webhook_url="webhook-url"
websites_list="website.com"

for website in ${websites_list}; do
    status_code=$(curl --write-out %{http_code} --silent --output /dev/null -L ${website})
    if "$status_code" 200 ; then
        curl -H "Content-Type: application/json" -X POST -d '{"content":"'"${website} did its usual 12 hour check. Code: ${status_code}"'"}'  $webhook_url
    fi
done

Pretty clueless to be honest, I'm relatively new to all of this anyway.

CherryDT
  • 25,571
  • 5
  • 49
  • 74
DashU
  • 1
  • 1
    Assuming the status code is 200, `if "$status_code" 200` will execute the command `"200" 200` and check its exit code. Obviously, `200` isn't a command, hence the error. – CherryDT Apr 10 '22 at 16:58
  • also ... are you looking to pass the literal string `%{http_code}` to `curl`? or is `http_code` a variable? if this is a variable reference then you want `"$http_code"` or `"${http_code}"` (double quotes recommended in case the value in `http_code` contains white space) – markp-fuso Apr 10 '22 at 17:18
  • also ..., cron job run dash or ksh not bash, unless you explicitely use teh bash command to call your script within the cron job. Next `websites_list="website.com"` is a string and not an array, so cannot be used to hold a websites list or iterate. You need to declare it as `websites_list=("website.com")`, then also quote the array expansion as `for website in "${websites_list[@]}"; do`. You also need to quote `null -L "${website}")`, quote all your variable expansions. Same for `" $webhook_url"` .... Check your script with `https://shellcheck.net/` – Léa Gris Apr 10 '22 at 19:17

0 Answers0