Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

exit() with message and non-zero exit status

Tags:

php

I have two files: first.php:

#!/usr/bin/php
<?php
 exit("Unable"); //1
 #exit(1);  //2
 #exit(); //or exit(0) //3
?>

second.php:

#!/usr/bin/php
<?php
 exec("./first.php",$out,$err);
 var_dump($out);
 echo "\n".$err;
?>

Now, When I run second.php with line #1 in first.php I have "Unable" in $out and 0 in $err. but with two other exits I have that digit in $err.
How can I have non-zero in $err when I execute exit with string message?
I have tested 2>&1 but it's useless.

like image 978
Farhadix Avatar asked Sep 22 '12 14:09

Farhadix


People also ask

What is non-zero exit status?

A non-zero exit status indicates failure. This seemingly counter-intuitive scheme is used so there is one well-defined way to indicate success and a variety of ways to indicate various failure modes. When a command terminates on a fatal signal whose number is N , Bash uses the value 128+ N as the exit status.

How can I check my exit status?

To check the exit code we can simply print the $? special variable in bash. This variable will print the exit code of the last run command. $ echo $?

What is an exit status and with what command is it used?

Exit status is an integer number. 0 exit status means the command was successful without any errors. A non-zero (1-255 values) exit status means command was a failure.

How do I display exit status in last command?

The echo command is used to display the exit code for the last executed Fault Management command.


1 Answers

exit("hi");

Is the same as:

echo "hi";
exit(0);

So just echo the statement :)

echo "Unable";
exit(2);
like image 65
Evert Avatar answered Oct 12 '22 14:10

Evert