Saturday, February 1, 2014

Unix Shell Scripting-Conditional Command Execution

- It is possible in Unix shell scripting to specify that a command to run only if particular condition is met.

- Such conditions are always expressed in terms of the exit status of another program as follows

command1 && command2  ==> command2 runs only when command1 exit status is 0(when there are no errors in executing command1)

command3 || command4 ==> command4 runs only when command3 exit status is not 0(when there are errors in executing command3)

oracle@rac1 tmp]$ ls ffdsfdf && cp ffdsfdf /tmp
ls: cannot access ffdsfdf: No such file or directory

[oracle@rac1 ~]$ ls myscript && cp myscript /tmp
myscript


[oracle@rac1 ~]$ cd /tmp
[oracle@rac1 tmp]$ ls -lart myscript
-rwxr-xr-x 1 oracle dba 545 Feb  2 11:17 myscript

- The only problem with these constructs is that they are very limited

    - You cannot specify a second command to be run if the condition is not met

    - You can only perform one command if the condition is met. (however, it is possible to group the commands)

No comments:

Post a Comment