Obtain other program return values in Linux

Source: Internet
Author: User
Example:

Status = system ("./test. Sh ");


1. Two statements are unified: (1) system return value: the return value after the system function is called. For example, in the previous example, status is system return value (2) Shell return value: the Return Value of the shell command called by system, such as test. sh returns the shell return value.
2. How to correctly determine whether test. Sh is correctly executed? Only determine whether status is = 0? Or, you can only determine whether the status is correct! =-1?
All errors!
3. System Description in man
Return Value
The value returned is-1 on error (e.g. Fork () failed), and the return
Status of the command otherwise. This latter return status is in
Format specified in wait (2). Thus, the exit code of the command will
Be wexitstatus (Status). In case/bin/sh cocould not be executed,
Exit status will be that of a command that does exit (127). Are you dizzy?
The processing of return values by the system function involves three phases: Phase 1: preparation for creating sub-processes. If it fails,-1 is returned. Phase 2: Call/bin/sh to pull the shell script. If the pulling fails or the shell fails to run normally (see note 1), the cause value is written to the lower status 8 ~ 15 bits. System man only indicates that the value 127 is written, but the actual test shows that the value 126 is also written. Phase 3: If the shell script runs normally and ends, fill in the shell return value as low as 8 ~ 15 bits. Note 1: as long as it can be called to/bin/sh and the shell is not interrupted by other signals during execution, it is considered normal. For example, no matter what reason value is returned in the shell script, whether it is 0 or not 0, the execution ends normally. Even if the shell script does not exist or has no execution permission, the execution ends normally. If the shell script is forced to kill during execution, the exception ends.
How can I determine whether the shell script is successfully executed in Stage 2? The system provides a macro: wifexited (Status ). If wifexited (Status) is true, the process ends normally. How to obtain the shell return value in stage 3? You can achieve this directly by shifting 8 bits to the right, but the security method is to use the macro wexitstatus (Status) provided by the system ).

Generally, in shell scripts, the return value is used to determine whether the script is executed normally. If 0 is returned successfully, a positive number is returned for failure. To sum up, the method to determine whether a system function calls the shell script normally ends should be the following three conditions: (1)-1! = Status (2) wifexited (Status) True (3) 0 = wexitstatus (Status)
Note: according to the above analysis, when the shell script does not exist or has no execution permission, the first two conditions will still be true. In this case, wexitstatus (Status) is 127,126 and other values. Therefore, in shell scripts, values such as 127,126 cannot be defined as return values. Otherwise, it cannot be used to identify whether it is the return value of shell or the cause value of the shell script exception. The return value in the shell script should start to increase at 1.
The following is a sound code used to determine whether a shell script is successfully executed:
    #include <stdio.h>       #include <stdlib.h>       #include <sys/wait.h>       #include <sys/types.h>             int main()      {          pid_t status;                      status = system("./test.sh");                if (-1 == status)          {              printf("system error!");          }          else          {              printf("exit status value = [0x%x]\n", status);                    if (WIFEXITED(status))              {                  if (0 == WEXITSTATUS(status))                  {                      printf("run shell script successfully.\n");                  }                  else                  {                      printf("run shell script fail, script exit code: %d\n", WEXITSTATUS(status));                  }              }              else              {                  printf("exit status = [%d]\n", WEXITSTATUS(status));              }          }                return 0;      }  

 

Wifexited (stat_val) evaluates to a non-zero value if status
Was returned for a child process that
Terminated normally.

Wexitstatus (stat_val) if the value of wifexited (stat_val) is
Non-zero, this macro evaluates to
Low-order 8 bits of the Status argument
That the child process passed to _ exit ()
Or exit (), or the value the child
Process returned from main ().

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.