CITS2002 Systems Programming  
prev
CITS2002 CITS2002 schedule  

Why the exit status of a program is important, continued

Most operating system shells are, themselves, programming languages, and they may use a program's exit status to direct control-flow within the shells - thus, the programming language that is the shell, is treating your programs as if they are external functions.

Shells are typically programmed using files of commands named shellscripts or command files and these will often have conditional constructs, such as if and while, just like C. It's thus important for our programs to work with the shells that invoke them.

We now compile our program, and invoke it with combinations of zero, and non-zero arguments:


prompt>  mycc -o status status.c   

prompt>  ./status 0 && ./status 1
exiting(0)
exiting(1)

prompt>  ./status 1 && ./status 0
exiting(1)

prompt>  ./status 0 || ./status 1
exiting(0)

prompt>  ./status 1 || ./status 0
exiting(1)
exiting(0)

  • Example1 - consider the sequence      prompt>  cd mydirectory  &&  rm -f *

  • Example2 - consider the actions in a Makefile (discussed in a later lecture).
    If a target has more than one action, then the make program executes each until one of them fails (or until all succeed).

 


CITS2002 Systems Programming, Lecture 9, p8, 21st August 2023.