Search code examples
phpbashfile-exists

Loop bash script until cURL response doesn't equal string


I'm trying to loop a bash script (cURL below) until my remote PHP script doesn't equal failure. I've tried the following, and it works when $fileName exists.. but doesn't loop when it returns failure.

My PHP:

<?php
$videoFile = $_GET["name"] . ".mp4";
if (file_exists($videoFile)) {
    echo "success";
} else {
    echo "failure";
}
?>

Bash command:

until $(curl --output /dev/null --silent --head --fail "my-remote-php-script.php"); do
    printf '.'
    sleep 1
done

Solution

  • This should work:

    while [ "$(curl -s 'http://your-url.php')" == "failure" ]; do
      echo -n '.'
      sleep 1
    done
    

    The $() places the curl in a sub-shell. The resulting string, comming from your php script, is compared with failure.