Issue
I'm trying to catch the output of the command and throw error/success accordingly.
$resultFromServer = [];
SSH::run($command, function($line)
{
echo $line.PHP_EOL;
if($line!= null) {
array_push($resultFromServer, $line); // crash
}
});
dd($resultFromServer);
Check out my output
WARNING: Kubernetes configuration file is group-readable. This is insecure. Location: /root/.kube/config WARNING: Kubernetes configuration file is world-readable. This is insecure. Location: /root/.kube/config Error: open test.yaml: no such file or directory
[]
I'm trying to look for this string Error:
and whatever to the right of it, I will display in my alert message.
Error: open test.yaml: no such file or directory
I'm trying
to store each line into an array and loop through and check for string contain, but I can't even store it into the array.
I kept getting a crash - if - I uncomment that line
How do I prevent the crash ? Do I need to add some sleep() ?
Solution
Because SSH::run uses a closure, $resultFromServer
does not exist within the scope of the sub function. You need to pass it in with the use
keyword as well as pass it in as a reference since you're modifying it on the inside:
SSH::run($command, function($line) use (&$resultFromServer)
{
echo $line.PHP_EOL;
if($line!= null) {
array_push($resultFromServer, $line); // crash
}
});
Answered By - aynber Answer Checked By - Katrina (WPSolving Volunteer)