How to use For Loop using Bash Script on Rocky Linux 9.2
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.
Comments ( 0 )
No comments available