Home > OS >  How to use output of Laravel-(artisan)-command in bash-script?
How to use output of Laravel-(artisan)-command in bash-script?

Time:03-11

I am trying to write a small startup-script for one of my docker-containers. The problem is that the bash-script has to wait until a artisan-command echoes "1". The artisan-commands handle-function looks like this:

$condition = something-to-check;
if($condition){ echo 1; } else { echo 0; }

What I had in mind was something like like this:

#!/bin/bash
while php artisan myapp:mycommand == "0"
do
   sleep 1m
done
do-something-else

How can I achieve this?

EDIT:

For anyone comming here via Google - James Taylor's answer pointed me in the right direction, which is why I accepted it and edit my solution in the question. The approach was to edit the handle-function like this:

/**
 * Execute the console command.
 *
 * @return int
 */
public function handle()
{
    $mycondition = true; //or whatever
    try{
        if($mycondition == false){
            echo "Some fancy status message based on the condition is false \n";
            exit(1);
        } else {
            echo "Some fancy status message based on the condition is true \n";
            exit (0);
        }
    } catch (\Exception){
        echo "Some fancy status message based on the condition with an exception\n";
        exit(1);
    }
}

And set up the bash-script like this:

#!/bin/bash
until php /path/to/artisan myapp:mycommand
do
   echo "Condition is false!" 
   sleep 1m   #or whatever you wanna do
done
echo "Condition is true!"
do-something

CodePudding user response:

Update your my app:mycommand to return an exit code instead of echo.

exit(1);

Reference the PHP exit function: https://www.php.net/manual/en/function.exit.php

  • Related