If you’ve done any programming using the C programming language, you’re probably surprised by the way the bash shell uses the for command. In the C language, a for loop normally defines a variable, which it then alters automatically during each iteration. Normally, programmers use this variable as a counter, and either increment or decrement the counter by one in each iteration.The bash for command can also provide this functionality. This section shows how you can usea C-style for command in a bash shell script.
The C language for command
The C language for command has a specific method for specifying a variable, a condition that must remain true for the iterations to continue, and a method for altering the variable for each iteration. When the specified condition becomes false, the for loop stops. The condition equationis defined using standard mathematical symbols. For example, take the C language code:for (i = 0; i ‹ 10; i++)
This code produces a simple iteration loop, where the variable i is used as a counter. The first section assigns a default value to the variable. The middle section defines the condition under which the loop will iterate. When the defined condition becomes false, the for loop stops iterations.
The last section defines the iteration process. After each iteration, the expression defined in the last section is executed. In this example, the i variable is incremented by one after each iteration.
The bash shell also supports a version of the for loop that looks similar to the C-style for loop, although it does have some subtle differences, including a couple of things that’ll confuse shell script programmers. Here’s the basic format of the C-style bash for loop:
for (( variable assignment condition iteration process ))
The format of the C-style for loop can be confusing for bash shell script programmers, as it usesC-style variable references instead of the shell-style variable references. Here’s what a C-style for command looks like:
for (( a = 1; a ‹ 10; a++ ))
Notice that there are a couple of things that don’t follow the standard bash shell for method:
The shell developers created this format to more closely resemble the C-style for command. While this is great for C programmers, it can throw even expert shell programmers into a tizzy. Be careful when using the C-style for loop in your scripts. Here’s an example of using the C-style for command in a bash shell program:$ cat test8
The for loop iterates through the commands using the variable defined in the for loop (the letteri in this example). In each iteration, the $i variable contains the value assigned in the for loop. After each iteration, the loop iteration process is applied to the variable, which in this example, increments the variable by one.
Using multiple variables
The C-style for command also allows you to use multiple variables for the iteration. The loop handles each variable separately, allowing you to define a different iteration process for each variable.While you can have multiple variables, you can only define one condition in the for loop:$ cat test9
The a and b variables are each initialized with different values, and different iteration processes are defined. While the loop increases the a variable, it decreases the b variable for each iteration.
Shell Scripting Related Interview Questions
|Perl Scripting Interview Questions||Python Interview Questions|
|Linux Interview Questions||Linux Embedded systems Interview Questions|
|AWK Interview Questions||BioPerl Interview Questions|
|Sed (Stream Editor) Interview Questions||Advanced Linux Interview Questions|
|Unix/Linux Interview Questions||Unix Shell Scripting Interview Questions|
Shell Scripting Tutorial
Starting With Linux Shells
Getting To The Shell
Basic Bash Shell Commands
More Bash Shell Commands
Using Linux Environment Variables
Basic Script Building
Understanding Linux File Permissions
Working With Editors
Using Structured Commands
More Structured Commands
Handling User Input
Adding Color To Scripts
Introducing Sed And Gawk
The Ash Shell
The Tcsh Shell
The Korn Shell
The Zsh Shell
Using The Web
Using A Database
Shell Script For Administrators
All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd
Wisdomjobs.com is one of the best job search sites in India.