Wednesday, December 26, 2012

Difference of Unix and Linux                                   Unix Shells

If you are already familiar with C or C++ coding then it is very easy to understand while loop in shell scripting. The Shell script while loop is same as other languages but different with syntax only. In bash shell scripting, while loop helps to run loop till the criteria fails. It is very useful when you want to run same command multiple times with change of some of the commands or its input.
The basic syntax of while loop in linux as follows:

while [ condition ]
do

                Our command 1;
                Command 2
                Command3
done

 Above while loop works as : if condition is satisfied then control comes inside the while loop and it will execute the command1, command then command3. And finally loop will exit.

While loop example in Linux:

root@hello:~/hello/test/test1# i=1;
root@hello:~/hello/test/test1#  while [ $i -lt 5 ]
> do
> echo $i;
> (( i++ ));
> done
1
2
3
4
root@hello:~/ctier/test/test1#

There are some changes in syntax of while loop as shell changes. In bash scripting, above syntax will work but in ksh it will not.
For KSH, you need to run the while as follows:

while [[ condition ]] ;
do
command 1 ;
command 2;
done

while loop with multiple conditions as follows:
while [ "$var1" != "$var2" || "$var1" == "$var3" || "$var2" != "$var4" ]
do
                commands here;
done
You can use counter inside the while loop as :
let counter=counter+1;

or

(( i++))
Posted by Machindra Dharmadhikari On 12/26/2012 07:18:00 PM 1 comment

1 comment:

  1. Appreciate this post. Let me try it out.

    my webpage - mzansi cleaners

    ReplyDelete

  • RSS
  • Delicious
  • Digg
  • Facebook
  • Twitter
  • Linkedin
  • Youtube

    Chitika Ads 2

    Histat

    About

    Enter your email address:

    Delivered by FeedBurner