Sunday, February 16, 2014

Unix Shell Scripting–While Loop

- The while loop repeats the execution of a code block in the same way that the if statement conditionally executes a code block

- The syntax for the while loop is as follows

    while command  <—when command condition is true (status zero), then entered into loop

    do

        code block

     done

- The given code block will be repeatedly executed instill the given command returns an exit status that is non-zero

- Example

echo -n 'Please enter your answer : '

read answer

while [ "$answer" != tiger ]

do

      echo That answer is incorrect

      echo "Please try again, you entered $answer"

       read answer

done

Output:

Please enter your answer : Tiger
That answer is incorrect
Please try again, you entered Tiger
Chicken
That answer is incorrect
Please try again, you entered Chicken
tiger <—Exits the program as the condition is met, the while loop is not entered

Other Example:

echo -n "Please enter the directory name :"
read dir

while [ ! -d $dir ]
do
echo "You didn't enter a directory name"
echo -n "Please try again by entering a directory name :"
read dir
done

echo "Congratulation!! Your Directory Name is $dir"

Output:

Please enter the directory name :..
Congratulation!! Your Directory Name is ..

Please enter the directory name :sdfsdfdsf
You didn't enter a directory name
Please try again by entering a directory name :fdsg
You didn't enter a directory name
Please try again by entering a directory name :

- Using while loop we can pipe the output of a Unix command, read the values and loop them

Example:

Output of who command has 3 columns

- User – Details regarding the user who currently logged in

- Terminal – Details of the terminal the user is using

- Time – Details of time the user has logged in since.

[oracle@rac1 ~]$ who
oracle   tty1         2014-02-16 10:20 (:0)
oracle   pts/0        2014-02-16 10:20 (:0.0)

who | while read user term time
do
     echo $user is using $term logged in since $time

done

Output:

[oracle@rac1 ~]$ . PipedWhile
oracle is using tty1 logged in since 2014-02-16 10:20 (:0)
oracle is using pts/0 logged in since 2014-02-16 10:20 (:0.0)

- Break & Continue can be used in conjunction with while loop (for that matter any other loop in Unix Shell Programming)

Break – The break statement will cause the loop to immediately terminate. Execution will recommence with the next line after the done

Continue – The continue statement will cause the loop to abandon the current iteration of the loop and begin the next one (the loop condition is retested)

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)

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