简体   繁体   中英

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM