How to use for loop in Bash Script on Debian 12
To Use For Loop In Bash Script On Debian 12
Introduction:
Similar to other programming languages, Bash shell scripting offers support for ‘for loops’ to accomplish iterative tasks. It allows us to iterate through a specific set of statements across a range of words in a string or elements in an array.
Procedure Steps:
Step 1: Check the OS version by using following command.
root@linuxhelp:~/linuxhelp# cat /etc/os-release
PRETTY_NAME="Debian GNU/Linux 12 (bookworm)"
NAME="Debian GNU/Linux"
VERSION_ID="12"
VERSION="12 (bookworm)"
VERSION_CODENAME=bookworm
ID=debian
HOME_URL="https://www.debian.org/"
SUPPORT_URL="https://www.debian.org/support"
BUG_REPORT_URL=https://bugs.debian.org/
Step 2: Create a script file using for loop by using following command.
root@linuxhelp:~/linuxhelp# vim for_loop
#!/bin/bash
read i
for name in txt{1..7}
do
echo "The .txt files will be creating from 1 to 7 while running the script"
touch $i.$name
name=$(($name+1))
sleep 1
done
Step 3: Make the executable permission by using following command.
root@linuxhelp:~/linuxhelp# chmod +x for_loop
Step 4: Long list the files to check the executable permission by using following command.
root@linuxhelp:~/linuxhelp# ls -la
total 12
drwxr-xr-x 2 root root 4096 Dec 7 05:05 .
drwx------ 8 root root 4096 Dec 7 05:05 ..
-rwxr-xr-x 1 root root 173 Dec 7 05:05 for_loop
Step 5: Run the script file by using following command.
root@linuxhelp:~/linuxhelp# ./for_loop
test
The .txt files will be creating from 1 to 7 while running the script
The .txt files will be creating from 1 to 7 while running the script
The .txt files will be creating from 1 to 7 while running the script
The .txt files will be creating from 1 to 7 while running the script
The .txt files will be creating from 1 to 7 while running the script
The .txt files will be creating from 1 to 7 while running the script
The .txt files will be creating from 1 to 7 while running the script
Step 6: Long list the files to check the created files by using following command.
root@linuxhelp:~/linuxhelp# ls -la
total 12
drwxr-xr-x 2 root root 4096 Dec 7 05:06 .
drwx------ 8 root root 4096 Dec 7 05:05 ..
-rwxr-xr-x 1 root root 173 Dec 7 05:05 for_loop
-rw-r--r-- 1 root root 0 Dec 7 05:06 test.txt1
-rw-r--r-- 1 root root 0 Dec 7 05:06 test.txt2
-rw-r--r-- 1 root root 0 Dec 7 05:06 test.txt3
-rw-r--r-- 1 root root 0 Dec 7 05:06 test.txt4
-rw-r--r-- 1 root root 0 Dec 7 05:06 test.txt5
-rw-r--r-- 1 root root 0 Dec 7 05:06 test.txt6
-rw-r--r-- 1 root root 0 Dec 7 05:06 test.txt7
Conclusion:
We have reached the end of this article. In this guide, we have walked you through the steps required to use for loop in Bash Script on Debian 12. Your feedback is much welcome.
Comments ( 0 )
No comments available