Saturday, February 1, 2014

Unix Shell Scripting-Conditional Code

- When every UNIX command completes, it invisibly returns a value to the program that started it (usually the shell) informing that program of the “status” of completion of the command.

- That value is a number, and is known as the “exit” status of the command

- Typically an exit status of 0 means that the program completed without any errors, while an exit status of some other value (other than 0) means errors has occurred

- The exit status is stored in a build-in variable called “?” and can be examined at any time with the command -   echo $?  as below

- The contents of this variable(?) as updated every time a program is run (including the echo command)

[oracle@rac1 ~]$ ls dffsdf
ls: cannot access dffsdf: No such file or directory


[oracle@rac1 ~]$ echo $?
2

[oracle@rac1 ~]$ echo $?
0  <--  The value 0 in here means that the above echo command has run successfully without any errors

- It is often useful for shell programming to think of exit status of 0 & 1 as below

0 – True – Executed without any errors

1 – False – Executed with errors

[oracle@rac1 ~]$ true
[oracle@rac1 ~]$ echo $?
0


[oracle@rac1 ~]$ false
[oracle@rac1 ~]$ echo $?
1

No comments:

Post a Comment