Linux system function return value

Source: Internet
Author: User

Cases: [CPP]View PlainCopy
    1. Status = System ("./test.sh");
1, first unified two statements: (1) System return value: Refers to the return value after invoking the system function, such as the status system return value (2) Shell return value in the previous example: Refers to the return value of the shell command called by the system, such as the above example, The value returned in test.sh is the shell return value. 2, how to correctly judge whether test.sh is executed correctly? Just determine if status is ==0?  Or are you just judging if the status is!=-1? All wrong! 3. Description of system 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 was in the
Format specified in wait (2). Thus, the exit code of the command would
Be Wexitstatus (status). In case/bin/sh could not being executed, the
       exit status'll be, that's a command that does exit (127). You look dizzy, don't you? The  system function handles the return value, involving 3 phases: Stage 1: Preparation for creating a child process. If it fails, return-1. Phase 2: Call/bin/sh to pull up the shell script, if the pull fails or the shell does not perform properly (see note 1), the reason value is written to the low 8~15 bit of status. The man in system only stated that it would write a value of 127, but the measured findings would also write 126 equivalents. Phase 3: If the shell script finishes gracefully, fill the shell return value into the low 8~15 bit of status. Note 1: As long as the ability to call to/bin/sh, and the execution of the shell process is not abnormal interruption of other signals, the normal end is counted. For example: Regardless of the reason returned in the shell script, whether the value is 0 or not 0, the normal execution ends. Even if the shell script does not exist or does not have execute permissions, it is the end of normal execution. If the shell script is forced to kill during execution, the case is ended abnormally.   How can I tell if the shell script is performing properly at Phase 2? The system provides macros: wifexited (status). If wifexited (status) is true, the normal end is indicated. How do I get the shell return value in Phase 3? You can do this directly by moving the 8bit right, but it's safe to use the system-provided macros: Wexitstatus (status).    since we typically determine whether this script is performing properly in a shell script by the return value, if 0 is returned successfully, the failure returns a positive number. So in summary, the method that determines whether a system function calls the shell script to end normally is the following 3 conditions: (1)-1! = status (2) wifexited (status) is True (3) 0 == wexitstatus ( Status)   Note: According to the above analysis, when the shell script does not exist, do not execute permissions and other scenarios, to the first 2 conditions will still be established, at this time Wexitstatus (status) is 127,126 and other values. Therefore, we cannot define a 127,126 value as a return value in a shell script, or we cannot distinguish between the return value of the shell or the reason for calling the shell script exception. The return value in the shell script should be 1 more than the beginning increment.   Judge the shell script to be executed at the end of normalThe sound code is as follows:  [CPP]View PlainCopy
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <sys/wait.h>
  4. #include <sys/types.h>
  5. int main ()
  6. {
  7. pid_t status;
  8. Status = System ("./test.sh");
  9. if ( -1 = = status)
  10. {
  11. printf ("System error!");
  12. }
  13. Else
  14. {
  15. printf ("exit status value = [0x%x]\n", status);
  16. if (wifexited (status))
  17. {
  18. if (0 = = Wexitstatus (status))
  19. {
  20. printf ("Run shell script successfully.\n");
  21. }
  22. Else
  23. {
  24. printf ("Run shell script fail, script exit code:%d\n", Wexitstatus (status));
  25. }
  26. }
  27. Else
  28. {
  29. printf ("Exit status = [%d]\n", Wexitstatus (status));
  30. }
  31. }
  32. return 0;
  33. }
Wifexited (Stat_val) evaluates to a Non-zero value if status
Was returned-a child process, that
Terminated normally.

Wexitstatus (stat_val) If the value of wifexited (Stat_val) is
Non-zero, this macro evaluates to the
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 ().

Linux system function return value

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.