• Categories
    Category
    {{ postCtrl.tags }}
    • {{ category.tag_type }}

      • {{tag.tag_name}}
      • View more
  • Categories
    Category
    {{ postCtrl.tags }}
    • {{ category.tag_type }}

      • {{tag.tag_name}}
      • View more
  • News
  • Tutorials
  • Forums
  • Tags
  • Users
Tutorial News Comments FAQ Related Articles

How to use For Loop using Bash Script on Rocky Linux 9.2

  • 00:26 Cat /etc/os-release
  • 00:42 vim forloop.sh
  • 01:33 chmod +x forloop.sh
  • 01:44 ll
  • 01:58 ./forloop.sh
  • 02:23 vim /etc/resolve.conf
  • 02:57 ./forloop.sh
{{postValue.id}}

To Use For Loop using Bash Script On Rocky Linux 9.2

Introduction:

A bash for loop is a bash programming language statement that allows code to be repeatedly executed. A for loop is classified as an iteration statement i.e. it is the repetition of a process within a bash script.

Procedure Steps:

Step 1: Check the OS version by using the following command.

[root@Linuxhelp ~]# cat /etc/os-release
NAME="Rocky Linux"
VERSION="9.2 (Blue Onyx)"
ID="rocky"
ID_LIKE="rhel centos fedora"
VERSION_ID="9.2"
PLATFORM_ID="platform:el9"
PRETTY_NAME="Rocky Linux 9.2 (Blue Onyx)"
ANSI_COLOR="0;32"
LOGO="fedora-logo-icon"
CPE_NAME="cpe:/o:rocky:rocky:9::baseos"
HOME_URL="https://rockylinux.org/"
BUG_REPORT_URL="https://bugs.rockylinux.org/"
SUPPORT_END="2032-05-31"
ROCKY_SUPPORT_PRODUCT="Rocky-Linux-9"
ROCKY_SUPPORT_PRODUCT_VERSION="9.2"
REDHAT_SUPPORT_PRODUCT="Rocky Linux"
REDHAT_SUPPORT_PRODUCT_VERSION="9.2"

Step 2: Creating a script file named forloop.sh to count nameservers in /etc/resolv.conf by using for loop statement.

[root@Linuxhelp ~]# vim forloop.sh
Insert the following Lines. 
#!/bin/bash
# Count dns name server in the /etc/resolv.conf if found
for file in /etc/*
do
        # check if file exists in bash using the if #
    if [ "${file}" == "/etc/resolv.conf" ]
    then
        countNameservers=$(grep -c nameserver /etc/resolv.conf)
        echo "Total dns ${countNameservers} nameservers defined in ${file}"
        break
    fi
done

Step 3: Give the executable permission for the script file by using the following command

[root@Linuxhelp ~]# chmod +x ./forloop.sh

Step 4: Long list the files by using the following command

[root@Linuxhelp ~]# ll
total 8
-rw-------. 1 root root 1039 Aug 13 22:24 anaconda-ks.cfg
drwxr-xr-x. 2 root root    6 Aug 13 22:33 Desktop
drwxr-xr-x. 2 root root  185 Oct 12 14:18 Documents
drwxr-xr-x. 2 root root    6 Aug 13 22:33 Downloads
-rwxr-xr-x. 1 root root  363 Oct 12 16:26 forloop.sh
drwxr-xr-x. 2 root root    6 Aug 13 22:33 Music
drwxr-xr-x. 2 root root    6 Aug 13 22:33 Pictures
drwxr-xr-x. 2 root root    6 Aug 13 22:33 Public
drwxr-xr-x. 2 root root    6 Oct  7 18:30 sample
drwxr-xr-x. 2 root root    6 Aug 13 22:33 Templates
drwxr-xr-x. 2 root root    6 Oct  7 20:15 testdir
drwxr-xr-x. 2 root root    6 Aug 13 22:33 Videos

Step 5: Execute the script to count nameservers in /etc/resolv.conf file by using ./command

[root@Linuxhelp ~]# ./forloop.sh 
Total dns 1 nameservers defined in /etc/resolv.conf

Our script shows there is one nameserver present in this file.

Step 6: Enter into /etc/resolve.file and check whether the execute value is correct or not and then add another nameserver in this file.

[root@Linuxhelp ~]# vim /etc/resolv.conf
# Generated by NetworkManager
search 8.8.4.4
nameserver 8.8.8.8
nameserver 8.8.4.4

Step 7: Again run the script to check our script executes the correct values after the changes occurs in the above configuration file.

[root@Linuxhelp ~]# ./forloop.sh 
Total dns 2 nameservers defined in /etc/resolv.conf
Now our scrip exetcutes the correct values.

Conclusion

We have reached the end of this article. In this guide, we have walked you through the steps required to use For Loop using Bash Script on Rocky Linux 9.2. Your feedback is much welcome.

Tags:
owen
Author: 

Comments ( 0 )

No comments available

Add a comment
{{postCtrl.cmtErrMsg}}

Frequently asked questions ( 5 )

Q

What is a for loop in Bash?

A

A for loop in Bash is a control structure that allows you to repeatedly execute a set of commands a specified number of times or over a range of values.

Q

What's the basic syntax of a for loop in Bash?

A

The basic syntax of a for loop in Bash is:
for variable in list
do
# Commands to be executed
done

Q

How do I break out of a for loop prematurely?

A

You can use the break statement to exit a for loop before it completes all iterations. You can include a conditional statement within the loop to determine when to break.

Q

How do I break out of a for loop prematurely?

A

You can use the break statement to exit a for loop before it completes all iterations. You can include a conditional statement within the loop to determine when to break.

Q

How do I loop through a range of numbers in a for loop?

A

You can use the seq command to generate a sequence of numbers and then iterate through it in a for-loop

Load more

Back To Top!
Rank
User
Points

Top Contributers

userNamenaveelansari
135850

Top Contributers

userNameayanbhatti
92510

Top Contributers

userNamehamzaahmed
32150

Top Contributers

1
userNamelinuxhelp
31040

Top Contributers

userNamemuhammadali
24500
Can you help Luke ?
workbench for debian

I am using workbench in CentOS whereas now I need to use Debian Operating system so could you please help to install and use in Debian?

Networking
  • Routing
  • trunk
  • Netmask
  • Packet Capture
  • domain
  • HTTP Proxy
Server Setup
  • NFS
  • KVM
  • Memory
  • Sendmail
  • WebDAV
  • LXC
Shell Commands
  • Cloud commander
  • Command line archive tools
  • last command
  • Shell
  • terminal
  • Throttle
Desktop Application
  • Linux app
  • Pithos
  • Retrospect
  • Scribe
  • TortoiseHg
  • 4Images
Monitoring Tool
  • Monit
  • Apache Server Monitoring
  • EtherApe 
  • Arpwatch Tool
  • Auditd
  • Barman
Web Application
  • Nutch
  • Amazon VPC
  • FarmWarDeployer
  • Rukovoditel
  • Mirror site
  • Chef
Contact Us | Terms of Use| Privacy Policy| Disclaimer
© 2025 LinuxHelp.com All rights reserved. Linux™ is the registered trademark of Linus Torvalds. This site is not affiliated with linus torvalds in any way.