To run the given example live online please visit the following link: https://goo.gl/mD69Gj
There are basic loops in shell script.
BASH WHILE LOOP
The Syntax for while loop is as follows:
Syntax
while expression
do
command
done
In the above syntax I am trying to explain three major components of while loop
#!/bin/sh
a=0
while [ $a -lt 10 ] #Spaces are very important here so keep that in mind.
do
echo $a
a=`expr $a + 1`
done
The another syntax for while loop can be termed in this following sequence.
while IFS= read -r line
do
command 1;
command 2;
.
.
.
command n;
done < "path/to/dir/filename with or without space"
You may also write the following syntax.
while IFS= read -r field1 field2 field3 .... fieldN
do
command1 on any one of the field
command2 on any of the other fields.
.
.
command3 on any of the fields
done < "path/to/dir/filename with or without space"
Here, IFS is used to set field separtor (by default the field seperator is white space). The -r option is to read command disables backslashes escaping such as \n,\t etc. This is failsafe while read loop for reading text files.
Here, IFS means Internal Field Separator. If time permits I will write a post for IFS too in future.
You may also use expression format in while Loop as.
filename: while2.sh
#!/bin/sh
a=0
while (( $a -lt 10 )) #Spaces are very important here so keep that in mind.
do
echo $a
a=`expr $a + 1`
done
So, as we now know about the above loops let us take care of one more syntax which I have described above.
For that we need a file to read. Just look at the simplicity on how we can read the file in most efficient manner in Linux compared to C LANGUAGE.
Reading a text file using "space" as default field separator.
filename: while3.sh
#!/bin/sh
file="namefile"
while read -r f1 f2
do
echo "Field 1 is: $f1 and Field 2 is: $f2"
done< "$file"
Reading a text file using separator field.
filename: while4.sh
#!/bin/sh
file="commafile"
while IFS="," read -r line
do
echo $line
done < "$file"
Here, Internal Field Separator is set as "," with every comma it finds it will treat as a line and print its value.
Even though we have "," as a separator, the entire lines are printed because everytime it takes the entire line from the file and stores in in "line" variable.
Let us view one more example for IFS using ":" separator file. As we all know the information of all the users are stored in /etc/passwd file in linux.
So, Let us view that file using while loop
filename: while5.sh
#/bin/sh
file="/etc/passwd"
while IFS=: read -r user enpass uid gid desc home shell
do
echo "User $user ($uid) it's home directory is $home with shell as $shell";
done < "$file"
With all lot of experience shared above, I can also term that different shell in linux have different while loop syntax.
Such as.
While Loop Syntax in ksh
while [[ condition ]]; do
command-1
command-2
.
.
.
command-n
done
While loop syntax in csh is as:
while ( condition )
command
end;
Now, let us view the simple examples of the above syntax one by one.
KSH While Loop Example
filename: while6.sh
#!/bin/ksh
index=1
while [[ $index -le 5 ]]; do
echo "Welcome to e-shellscript: $index times";
((index++ ))
done
CSH While Loop Example
filename: while7.sh
#!/bin/csh
index=1
while ( $index <=5 )
echo "Welcome to e-shellscript $index times";
@ c = $c + 1
end
Let us view another example for c-shell
filename: while8.sh
#!/bin/csh
set yourname="eshellscript"
while ( $yourname != "" )
echo -n "Enter your cute name: "
set yourname = $<
if ($yname != "" ) then
echo "Hello , $yourname"
endif
end
There are basic loops in shell script.
- For Loop
- While Loop
- Until Loop
BASH WHILE LOOP
The Syntax for while loop is as follows:
Syntax
while expression
do
command
done
In the above syntax I am trying to explain three major components of while loop
- keywords - while, do and done
- Expression is any expression which returns scalar values
- The statement will execute all the commands between do and done until the condition or expression is true.
#!/bin/sh
a=0
while [ $a -lt 10 ] #Spaces are very important here so keep that in mind.
do
echo $a
a=`expr $a + 1`
done
The another syntax for while loop can be termed in this following sequence.
while IFS= read -r line
do
command 1;
command 2;
.
.
.
command n;
done < "path/to/dir/filename with or without space"
You may also write the following syntax.
while IFS= read -r field1 field2 field3 .... fieldN
do
command1 on any one of the field
command2 on any of the other fields.
.
.
command3 on any of the fields
done < "path/to/dir/filename with or without space"
Here, IFS is used to set field separtor (by default the field seperator is white space). The -r option is to read command disables backslashes escaping such as \n,\t etc. This is failsafe while read loop for reading text files.
Here, IFS means Internal Field Separator. If time permits I will write a post for IFS too in future.
You may also use expression format in while Loop as.
filename: while2.sh
#!/bin/sh
a=0
while (( $a -lt 10 )) #Spaces are very important here so keep that in mind.
do
echo $a
a=`expr $a + 1`
done
So, as we now know about the above loops let us take care of one more syntax which I have described above.
For that we need a file to read. Just look at the simplicity on how we can read the file in most efficient manner in Linux compared to C LANGUAGE.
Reading a text file using "space" as default field separator.
filename: while3.sh
#!/bin/sh
file="namefile"
while read -r f1 f2
do
echo "Field 1 is: $f1 and Field 2 is: $f2"
done< "$file"
Reading a text file using separator field.
filename: while4.sh
#!/bin/sh
file="commafile"
while IFS="," read -r line
do
echo $line
done < "$file"
Here, Internal Field Separator is set as "," with every comma it finds it will treat as a line and print its value.
Even though we have "," as a separator, the entire lines are printed because everytime it takes the entire line from the file and stores in in "line" variable.
Let us view one more example for IFS using ":" separator file. As we all know the information of all the users are stored in /etc/passwd file in linux.
So, Let us view that file using while loop
filename: while5.sh
#/bin/sh
file="/etc/passwd"
while IFS=: read -r user enpass uid gid desc home shell
do
echo "User $user ($uid) it's home directory is $home with shell as $shell";
done < "$file"
With all lot of experience shared above, I can also term that different shell in linux have different while loop syntax.
Such as.
While Loop Syntax in ksh
while [[ condition ]]; do
command-1
command-2
.
.
.
command-n
done
While loop syntax in csh is as:
while ( condition )
command
end;
Now, let us view the simple examples of the above syntax one by one.
KSH While Loop Example
filename: while6.sh
#!/bin/ksh
index=1
while [[ $index -le 5 ]]; do
echo "Welcome to e-shellscript: $index times";
((index++ ))
done
CSH While Loop Example
filename: while7.sh
#!/bin/csh
index=1
while ( $index <=5 )
echo "Welcome to e-shellscript $index times";
@ c = $c + 1
end
Let us view another example for c-shell
filename: while8.sh
#!/bin/csh
set yourname="eshellscript"
while ( $yourname != "" )
echo -n "Enter your cute name: "
set yourname = $<
if ($yname != "" ) then
echo "Hello , $yourname"
endif
end
0 comments:
Post a Comment