How to create and run Bash Script on Rocky Linux 8.6

To Create And Run Bash Script On Rocky Linux 8.6

Introduction:

It is a convention to give files that are Bash scripts an extension of .sh (myscript.sh for example). As you would be aware Linux is an extension-less system so a script doesn't necessarily have to have this characteristic in order to work.

Installation steps:

Step 1: Check the installed version of OS by using the below command

[root@linuxhelp ~]# cat /etc/os-release 
NAME="Rocky Linux"
VERSION="8.6 (Green Obsidian)"
ID="rocky"
ID_LIKE="rhel centos fedora"
VERSION_ID="8.6"
PLATFORM_ID="platform:el8"
PRETTY_NAME="Rocky Linux 8.6 (Green Obsidian)"
ANSI_COLOR="0;32"
CPE_NAME="cpe:/o:rocky:rocky:8:GA"
HOME_URL="https://rockylinux.org/"
BUG_REPORT_URL="https://bugs.rockylinux.org/"
ROCKY_SUPPORT_PRODUCT="Rocky Linux"
ROCKY_SUPPORT_PRODUCT_VERSION="8"
REDHAT_SUPPORT_PRODUCT="Rocky Linux"
REDHAT_SUPPORT_PRODUCT_VERSION="8"

Step 2: Check the ip address by using the below command

[root@linuxhelp ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever

    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 00:0c:29:b8:f7:db brd ff:ff:ff:ff:ff:ff
    inet 192.168.6.122/23 brd 192.168.7.255 scope global noprefixroute ens33
       valid_lft forever preferred_lft forever
    inet6 fe80::20c:29ff:feb8:f7db/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever
3: virbr0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default qlen 1000
    link/ether 52:54:00:f2:20:6b brd ff:ff:ff:ff:ff:ff
    inet 192.168.122.1/24 brd 192.168.122.255 scope global virbr0
       valid_lft forever preferred_lft forever

Step 3: Create the testscript.sh empty file by using touch command

[root@linuxhelp ~]# touch testscript.sh

Step 4: List current location to check the created file by using the below command

[root@linuxhelp ~]# ll
total 8
-rw-------. 1 root root 1204 Jun 27 08:48 anaconda-ks.cfg
drwxr-xr-x. 2 root root    6 Jun 27 08:50 Desktop
drwxr-xr-x. 2 root root    6 Jun 27 08:50 Documents

drwxr-xr-x. 2 root root    6 Jun 27 08:50 Downloads
-rw-r--r--. 1 root root 1431 Jun 27 08:49 initial-setup-ks.cfg
drwxr-xr-x. 2 root root    6 Jun 27 08:50 Music
drwxr-xr-x. 2 root root    6 Jun 27 08:50 Pictures
drwxr-xr-x. 2 root root    6 Jun 27 08:50 Public
drwxr-xr-x. 2 root root    6 Jun 27 08:50 Templates
-rw-r--r--. 1 root root    0 Jul  7 01:26 testscript.sh
drwxr-xr-x. 2 root root    6 Jun 27 08:50 Videos

Step 5: Now give the execute file permission by using the below command

[root@linuxhelp ~]# chmod +x testscript.sh

Step 6: Now list current location to check testscript.sh files execute permission by using the below command

[root@linuxhelp ~]# ll
total 8
-rw-------. 1 root root 1204 Jun 27 08:48 anaconda-ks.cfg
drwxr-xr-x. 2 root root    6 Jun 27 08:50 Desktop
drwxr-xr-x. 2 root root    6 Jun 27 08:50 Documents
drwxr-xr-x. 2 root root    6 Jun 27 08:50 Downloads
-rw-r--r--. 1 root root 1431 Jun 27 08:49 initial-setup-ks.cfg
drwxr-xr-x. 2 root root    6 Jun 27 08:50 Music
drwxr-xr-x. 2 root root    6 Jun 27 08:50 Pictures
drwxr-xr-x. 2 root root    6 Jun 27 08:50 Public
drwxr-xr-x. 2 root root    6 Jun 27 08:50 Templates

-rwxr-xr-x. 1 root root    0 Jul  7 01:26 testscript.sh
drwxr-xr-x. 2 root root    6 Jun 27 08:50 Videos

Step 7: open the testscript.sh file to create bash script by using the below command

[root@linuxhelp ~]# vim testscript.sh 	
#!/bin/bash
echo welcome to linuxhelp
ping -c 10 192.168.6.122
echo thank you for watching

Step 8: Run the script by using the below command

[root@linuxhelp ~]# ./testscript.sh 
welcome to linuxhelp
PING 192.168.6.122 (192.168.6.122) 56(84) bytes of data.
64 bytes from 192.168.6.122: icmp_seq=1 ttl=64 time=0.164 ms
64 bytes from 192.168.6.122: icmp_seq=2 ttl=64 time=0.128 ms
64 bytes from 192.168.6.122: icmp_seq=3 ttl=64 time=0.145 ms
64 bytes from 192.168.6.122: icmp_seq=4 ttl=64 time=0.363 ms
64 bytes from 192.168.6.122: icmp_seq=5 ttl=64 time=0.971 ms
64 bytes from 192.168.6.122: icmp_seq=6 ttl=64 time=0.499 ms
64 bytes from 192.168.6.122: icmp_seq=7 ttl=64 time=0.086 ms
64 bytes from 192.168.6.122: icmp_seq=8 ttl=64 time=0.271 ms
64 bytes from 192.168.6.122: icmp_seq=9 ttl=64 time=0.518 ms
64 bytes from 192.168.6.122: icmp_seq=10 ttl=64 time=0.105 ms

--- 192.168.6.122 ping statistics ---
10 packets transmitted, 10 received, 0% packet loss, time 9132ms
rtt min/avg/max/mdev = 0.086/0.325/0.971/0.263 ms
thank you for watching

Conclusion:

We have reached the end of this article. In this guide, we have walked you through the steps required to create and run Bash Script on Rocky Linux 8.6. Your feedback is much welcome.

FAQ
Q
Can Python replace bash?
A
The answer is yes as using python provides various benefits and it can be also installed by default on all the major Linux distributions.
Q
What language is the Linux terminal?
A
The language that is commonly used is a shell script that is sometimes referred to as “shebang”. Shell scripts are usually implemented by interpreters present in the Linux kernel.
Q
What is the difference between C shell and bash?
A
CSH is a C shell while BASH stands for Bourne Again shell. The two are both Unix and Linux shells. whereas the C-shell commands start with '#' and for bash it is ‘;’. The CSH also its own set of features and the BASH unites the features of other shells.
Q
Is bash a terminal or shell?
A
The GUI window refers to the terminal and it can also be seen on the screen as it requires commands and shows the output. The bash is particularly a shell and the primary example for it is sh(bourne shell), csh(c shell), and tcsh(turbo c shell).
Q
Is shell scripting and bash scripting the same?
A
Bash is the acronym for "Bourne Again Shell", which is also considered to be a subset of the shell scripting. Shell scripting is scripting in any shell and eases the interaction between the user and operating system, while bash scripting can be done only for the bash.