Followers

Linux Shell Script for Loops

  Loops are repeated set of actions for a range of values. For loop Below are some Examples echo "Fix values in for loop" for i ...

 Loops are repeated set of actions for a range of values.

For loop

Below are some Examples

echo "Fix values in for loop"
for i in 1 2 3 4 5
do
        echo $i
done

echo "Range values"
for i in {1..10}
do
        echo $i
done

echo "Step values with Range"
for i in {0..10..2}
do
        echo $i
done

echo "c style for loop"
for (( c=1; c<=5; c++ ))
do
        echo $c
done

echo "break statement example which exit from the loop whenever break statement is encountered"
for i in {1..10}
do
    if [ $i -eq 5 ]
    then
      break
    fi

        echo $i
done

echo "continue statement to continue the loop"
for i in {1..10}
do
    var1=`expr $i % 2`
    if [ $var1 -eq 0 ]
    then
      continue
    fi

        echo $i
done

echo "Looping with array"
Zones=('zone1' 'zone2' 'zone3')
for i in "${Zones[@]}"
do
        echo $i
done

Execute

./loop.sh

Output

Fix values in for loop

1

2

3

4

5

Range values

1

2

3

4

5

6

7

8

9

10

Step values with Range

0

2

4

6

8

10

c style for loop

1

2

3

4

5

break statement example which exit from the loop whenever break statement is encountered

1

2

3

4

continue statement to continue the loop

1

3

5

7

9

Looping with array

zone1

zone2

zone3

While loop

It repeats the set of statements on the basis of conditions which are supplied with the while loop

choice="Y"
while [ $choice == "Y" ]
do
        echo "Enter the choice(Y/N)"
        read choice
done
echo "Program ends here"

Execute

./loop.sh







COMMENTS

BLOGGER: 2
  1. BREAK and CONINUE are acting like commands in IF statement what are they?

    ReplyDelete

Name

Ansible,6,AWS,1,Azure DevOps,1,Containerization with docker,2,DevOps,2,Docker Quiz,1,Docker Swarm,1,DockerCompose,1,ELK,2,git,2,Jira,1,Kubernetes,1,Kubernetes Quiz,5,SAST DAST Security Testing,1,SonarQube,3,Splunk,2,vagrant kubernetes,1,YAML Basics,1,
ltr
item
DevOpsWorld: Linux Shell Script for Loops
Linux Shell Script for Loops
DevOpsWorld
https://www.devopsworld.co.in/2022/04/linux-shell-script-for-loops.html
https://www.devopsworld.co.in/
https://www.devopsworld.co.in/
https://www.devopsworld.co.in/2022/04/linux-shell-script-for-loops.html
true
5997357714110665304
UTF-8
Loaded All Posts Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS PREMIUM CONTENT IS LOCKED STEP 1: Share to a social network STEP 2: Click the link on your social network Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy Table of Content