Loop of FOR examples in shell script

Mihir SuratwalaMihir Suratwala
3 min read

Last Blog Review

In the last blog we understood, why for loop is required and what are the multiple ways we can execute the for loop to be used in shell scripting.

Examples of using For Loop →

  1. For loop Shell Script Example

Create a simple script called loop.sh at /home/bob. The script should make use of a loop and print the numbers 31 to 33 with each number in a new line.

i-0....b (EC2)
-------------------------        
----lp10:~$ vim /home/bob/loop.sh
----lp10:~$ cat /home/bob/loop.sh
#!/bin/bash

for num in {31..33}
do
        echo $num
done
----lp10:~$ chmod 700 loop.sh
----lp10:~$ ./loop.sh
31
32
33
bob@caleston-lp10:~$
  1. For loop Shell Script Example

We have a few different applications running on this system. The list of applications are stored at /tmp/assets/apps.txt file. Each application has it's logs stored in /var/log/apps directory under a file with its name. A simple version of the script has been developed for you /home/bob/count-requests.sh that inspects the log file of an application and prints the number of GET, POST, and DELETE requests. Update the script to use a for loop to read the list of applications from the apps.txt file and count number of requests for each application and display it in a tabular format like this.

i-0........b (EC2)
-------------------------
----lp10:~$ cat /tmp/assets/apps.txt
finance
marketing
partners
pay
----lp10:~$ cd /var/log/apps
----lp10:/var/log/apps$ ls    
finance_app.log  marketing_app.log  partners_app.log  pay_app.log
----lp10:/var/log/apps$ cd
----lp10:~$ vim /home/bob/count-requests.sh
----lp10:~$ cat /home/bob/count-requests.sh
#!/bin/bash

echo -e " Log name   \t      GET      \t      POST    \t   DELETE "
echo -e "------------------------------------------------------------"

for app in $(cat /tmp/assets/apps.txt)
do
      get_requests=$(cat /var/log/apps/${app}_app.log | grep "GET" | wc -l)
      post_requests=$(cat /var/log/apps/${app}_app.log | grep "POST" | wc -l)
      delete_requests=$(cat /var/log/apps/${app}_app.log | grep "DELETE" | wc -l)
      echo -e " ${app}    \t ${get_requests}    \t    ${post_requests}   \t   ${delete_requests}"

done
----lp10:~$ chmod 700 count-requests.sh
bob@caleston-lp10:~$ ./count-requests.sh 
Log name             GET         POST         DELETE 
------------------------------------------------------------
finance             7          15             18
marketing           9          8              16
partners            14         11             17
pay                 5          8              11
  1. For loop Shell Script Example

We have some images under the directory /home/bob/images. Develop a script /home/bob/rename-images.sh to rename all files within the images folder that has extension jpeg to jpg. A file with any other extension should remain the same. Tip: Use a for loop to iterate over the files within /home/bob/images. Tip: Use an if conditional to check if the file extension is jpeg. Tip: Use mv to rename a file. To replace jpeg to jpg in a filename use echo user1.jpeg | sed 's/jpeg/jpg/g'.

i-002978a646c30dd8b (EC2)
-------------------------
----lp10:~$ cd /home/bob/images
----lp10:~/images$ ls
user1.jpeg  user2.jpeg  user3.png  user4.jpeg  user5.png
----lp10:~/images$ cd 
----lp10:~$ vim /home/bob/rename-images.sh
----lp10:~$ cat /home/bob/rename-images.sh
#!/bin/bash

for file in $(ls images)
do
       if [[ $file = *.jpeg ]]
               then
               new_name=$(echo $file| sed 's/jpeg/jpg/g')
               mv images/$file images/$new_name
        fi
done
----lp10:~$ chmod 700 /home/bob/rename-images.sh
----lp10:~$ ./rename-images.sh
----lp10:~$ cd /home/bob/images
----lp10:~/images$ ls
user1.jpg  user2.jpg  user3.png  user4.jpg  user5.png

Conclusion →

In the this blog we understood, few examples of shell script where for loop is used.

💡
That’s a wrap for today’s post! I hope this has given you some valuable insights. Be sure to explore more articles on our blog for further tips and advice. See you in the next post!
0
Subscribe to my newsletter

Read articles from Mihir Suratwala directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Mihir Suratwala
Mihir Suratwala

Hi, How are you !! Hope you doing good.... I got introduced to Cloud initially. As I went ahead learning what is cloud and how it works, then got to know a field which is DevOps that makes Cloud model more effective. So, as I started working & got good experience on AWS. I have been learning the DevOps tool and technologies on how to use it with the Cloud, which will give me good understanding on how Cloud and DevOps go hand in hand to deploy my applications.