In this article we will see the basic and advance syntax of for loop and how we can use the for loop utility in shell scripting.
1. How to loop through a series of numbers using for loop
for k in 1 2 3 4 5
do
echo "Output Value=> $k"
done
Output: Will display the series on terminal.
Output Value=> 1
Output Value=> 2
Output Value=> 3
Output Value=> 4
Output Value=> 5
2. How to loop through a range of numbers using for loop
for k in {1..5}
do
echo "Output Value=> $k"
done
Output: Will display the range on terminal.
Output Value=> 1
Output Value=> 2
Output Value=> 3
Output Value=> 4
Output Value=> 5
3. How to loop through sequence (seq function) value using for loop
for k in $(seq 1 5)
do
echo "Output Value=> $k"
done
[OR]
for k in `seq 1 5`
do
echo "Output Value=> $k"
done
Output: Sequence function will generate values from 1 to 5 and for loop will treat as a range and will display it on terminal.
Output Value=> 1
Output Value=> 2
Output Value=> 3
Output Value=> 4
Output Value=> 5
4. How to increment for loop by a interval in range
for k in {0..10..2}
do
echo "Output Value=> $k"
done
Output: Will display the range of 0 to 10 with the increment of 2 every time.
Output Value=> 0
Output Value=> 2
Output Value=> 4
Output Value=> 6
Output Value=> 8
Output Value=> 10
5. How to loop through the character series using for loop.
It is not like you can only loop through Number, infect you can loop through anything.
for k in a b c d e
do
echo "Output Value=> $k"
done
Output: Will loop through the series one by one and display on terminal.
Output Value=> a
Output Value=> b
Output Value=> c
Output Value=> d
Output Value=> e
6. How to loop through the character range
for k in {a..c}
do
echo "Output Value=> $k"
done
Output: Will display the all characters of range a to c.
Output Value=> a
Output Value=> b
Output Value=> c
We can use upper case range as well.
7. How to loop through the Words series
for k in unix linux windows
do
echo "Output Value=> $k"
done
Output: Will loop through each word and display accordingly.
Output Value=> unix
Output Value=> linux
Output Value=> windows
8. How to list down all the files of directory
for k in `ls`
do
echo "Output Value=> $k"
done
[OR]
for k in *
do
echo "Output Value=> $k"
done
Output: * stands for everything in the directory. it will display ever thing where for loop is executed.
Output Value=> xyz.txt
Output Value=> abc.txt
Output Value=> Tgt
9. How to list down all the files of directory
for k in `ls *.txt`
do
echo "Output Value=> $k"
done
Output:
Output Value=> abc.txt
10. How to implement an C-style loop in UNIX script
for (( k=1; k<=5; k++ ))
do
echo "Value of k is =>$k"
done
11. How to implement an infinite loop in UNIX script
for (( ; ; ))
do
echo "infinite loops"
done
Output: Will go in infinite loop, If you want to menu bases scripts use infinite for loop. ctr + c will get you out from infinite loop execution.
12. How to read a file using for loop
for V_Line in `cat abc.txt`
do
echo "Output Line=>"$V_Line
done
IFS=$'\n';
for V_Line in `cat abc.txt`
do
echo "Output Line=>"$V_Line
done
[OR]
IFS=$'\n';
for V_Line in $(cat abc.txt);
do
echo "Output Line=>"$V_Line
done
IFS= Internal Field Separator
This just tells the shell to split on newlines only, not spaces which is by default.
13. How to use a variable in Range limit of for loop.
V_Upper_Limit=5
for k in {1..${V_Upper_Limit}}
do
echo "First Value=> $k"
done
Will not produced as required instead will display {1..5}, to over come this issue we can use below approaches.
V_Upper_Limit=5
for k in `seq 1 ${V_Upper_Limit}`
do
echo "First Value=> $k"
done
[OR]
V_Upper_Limit=5
for k in `eval "echo {1..${V_Upper_Limit}}"`
do
echo "First Value=> $k"
done
14. How to loop through script parameter
for k in $@
do
echo "First Script Parameter $k"
done
Output: Will display all the parameter to the a.sh. $@ holds all the parameter.
15 . How to loop through the output of any Unix/Linux command.
for k in `grep -i "error" abc.log`
do
echo "Output is=>"$k
done
IFS=$'\n';
for k in `grep -i "error" abc.log`
do
echo "Output is=>"$k
done
Now it will print complete line which grep produced as a search.
[OR]
for k in `find . -name "*.txt"`
do
echo "Output is=>"$k
done
Output: Will list down all the .txt file from current directory.
We can write for block in single line as below only thing is we need to put semi colon(;) after every action.
$ for k in `find . -name "*.txt"`; do echo "Output is=>"$k; done
$ for k in `grep -i "Error" abc.log`; do echo "Output is=>"$k; done
Conclusion:
Unix/Linux for loop is very powerful utility and can be used in multiple situations as explained above. It reduces the code length and provides an easy syntax. Keep practising to become a champ in for loop.
1. How to loop through a series of numbers using for loop
for k in 1 2 3 4 5
do
echo "Output Value=> $k"
done
Output: Will display the series on terminal.
Output Value=> 1
Output Value=> 2
Output Value=> 3
Output Value=> 4
Output Value=> 5
2. How to loop through a range of numbers using for loop
for k in {1..5}
do
echo "Output Value=> $k"
done
Output: Will display the range on terminal.
Output Value=> 1
Output Value=> 2
Output Value=> 3
Output Value=> 4
Output Value=> 5
3. How to loop through sequence (seq function) value using for loop
for k in $(seq 1 5)
do
echo "Output Value=> $k"
done
[OR]
for k in `seq 1 5`
do
echo "Output Value=> $k"
done
Output: Sequence function will generate values from 1 to 5 and for loop will treat as a range and will display it on terminal.
Output Value=> 1
Output Value=> 2
Output Value=> 3
Output Value=> 4
Output Value=> 5
4. How to increment for loop by a interval in range
for k in {0..10..2}
do
echo "Output Value=> $k"
done
Output: Will display the range of 0 to 10 with the increment of 2 every time.
Output Value=> 0
Output Value=> 2
Output Value=> 4
Output Value=> 6
Output Value=> 8
Output Value=> 10
5. How to loop through the character series using for loop.
It is not like you can only loop through Number, infect you can loop through anything.
for k in a b c d e
do
echo "Output Value=> $k"
done
Output: Will loop through the series one by one and display on terminal.
Output Value=> a
Output Value=> b
Output Value=> c
Output Value=> d
Output Value=> e
6. How to loop through the character range
for k in {a..c}
do
echo "Output Value=> $k"
done
Output: Will display the all characters of range a to c.
Output Value=> a
Output Value=> b
Output Value=> c
We can use upper case range as well.
7. How to loop through the Words series
for k in unix linux windows
do
echo "Output Value=> $k"
done
Output: Will loop through each word and display accordingly.
Output Value=> unix
Output Value=> linux
Output Value=> windows
8. How to list down all the files of directory
for k in `ls`
do
echo "Output Value=> $k"
done
[OR]
for k in *
do
echo "Output Value=> $k"
done
Output: * stands for everything in the directory. it will display ever thing where for loop is executed.
Output Value=> xyz.txt
Output Value=> abc.txt
Output Value=> Tgt
9. How to list down all the files of directory
for k in `ls *.txt`
do
echo "Output Value=> $k"
done
Output:
Output Value=> abc.txt
10. How to implement an C-style loop in UNIX script
for (( k=1; k<=5; k++ ))
do
echo "Value of k is =>$k"
done
11. How to implement an infinite loop in UNIX script
for (( ; ; ))
do
echo "infinite loops"
done
Output: Will go in infinite loop, If you want to menu bases scripts use infinite for loop. ctr + c will get you out from infinite loop execution.
12. How to read a file using for loop
for V_Line in `cat abc.txt`
do
echo "Output Line=>"$V_Line
done
IFS=$'\n';
for V_Line in `cat abc.txt`
do
echo "Output Line=>"$V_Line
done
[OR]
IFS=$'\n';
for V_Line in $(cat abc.txt);
do
echo "Output Line=>"$V_Line
done
IFS= Internal Field Separator
This just tells the shell to split on newlines only, not spaces which is by default.
13. How to use a variable in Range limit of for loop.
V_Upper_Limit=5
for k in {1..${V_Upper_Limit}}
do
echo "First Value=> $k"
done
Will not produced as required instead will display {1..5}, to over come this issue we can use below approaches.
V_Upper_Limit=5
for k in `seq 1 ${V_Upper_Limit}`
do
echo "First Value=> $k"
done
[OR]
V_Upper_Limit=5
for k in `eval "echo {1..${V_Upper_Limit}}"`
do
echo "First Value=> $k"
done
14. How to loop through script parameter
for k in $@
do
echo "First Script Parameter $k"
done
Output: Will display all the parameter to the a.sh. $@ holds all the parameter.
15 . How to loop through the output of any Unix/Linux command.
for k in `grep -i "error" abc.log`
do
echo "Output is=>"$k
done
Output: Will list down all the lines from abc.log where error is found but break the output line based on space not on newline. We need to set Internal Field separator IFS=$'\n';
IFS=$'\n';
for k in `grep -i "error" abc.log`
do
echo "Output is=>"$k
done
Now it will print complete line which grep produced as a search.
[OR]
for k in `find . -name "*.txt"`
do
echo "Output is=>"$k
done
Output: Will list down all the .txt file from current directory.
We can write for block in single line as below only thing is we need to put semi colon(;) after every action.
$ for k in `find . -name "*.txt"`; do echo "Output is=>"$k; done
$ for k in `grep -i "Error" abc.log`; do echo "Output is=>"$k; done
Conclusion:
Unix/Linux for loop is very powerful utility and can be used in multiple situations as explained above. It reduces the code length and provides an easy syntax. Keep practising to become a champ in for loop.
No comments:
Post a Comment