AMP AMP

How to Create and Run Bash Script on Oracle Linux 8.5

To Create And Run Bash Script On Oracle Linux 8.5

Introduction:

• A bash script is a collection of commands written in a file. • The bash program reads and executes these commands. • It executes these commands line by line. • Using the command line, you can navigate to a particular path, create a folder and spawn a process inside that folder.

Installation Procedure:

Step 1: Check the OS Version by using the below command

[root@linuxhelp linuxhelp]# cat /etc/os-release
NAME="Oracle Linux Server"
VERSION="8.5"

Step 2: Create a file named test.sh

[root@linuxhelp linuxhelp]# touch test.sh

Step 3: Grant Permission to test.sh file by using the below command

[root@linuxhelp linuxhelp]# chmod 755 test.sh

Step 4: Longlist the files by using the below command

 [root@linuxhelp linuxhelp]# ls -la
-rw-r--r--.  1 linuxhelp linuxhelp   18 Oct 10  2021 .bash_logout
-rw-r--r--.  1 linuxhelp linuxhelp  141 Oct 10  2021 .bash_profile
-rw-r--r--.  1 linuxhelp linuxhelp  376 Oct 10  2021 .bashrc
drwxr-xr-x.  2 linuxhelp linuxhelp    6 May 24 02:05 Desktop
drwxr-xr-x.  2 linuxhelp linuxhelp    6 May 24 02:05 Documents

drwxr-xr-x.  2 linuxhelp linuxhelp    6 May 24 02:05 Downloads
drwxr-xr-x.  2 linuxhelp linuxhelp    6 May 24 02:05 Music
drwxr-xr-x.  2 linuxhelp linuxhelp    6 May 24 02:05 Pictures
-rwxr-xr-x.  1 root      root         0 Jun 30 02:09      test.sh
drwxr-xr-x.  2 linuxhelp linuxhelp    6 May 24 02:05 Videos

Step 5: Edit the testscript.sh file by using the below command

[root@linuxhelp linuxhelp]# vim test.sh

Step 6: Run the test.sh by using the below command

[root@linuxhelp linuxhelp]# sh test.sh
Bash Script is Started
PING 192.168.6.101 (192.168.6.101) 56(84) bytes of data.
64 bytes from 192.168.6.101: icmp_seq=1 ttl=128 time=1.47 ms
64 bytes from 192.168.6.101: icmp_seq=2 ttl=128 time=0.773 ms
64 bytes from 192.168.6.101: icmp_seq=3 ttl=128 time=1.30 ms
64 bytes from 192.168.6.101: icmp_seq=4 ttl=128 time=3.53 ms
64 bytes from 192.168.6.101: icmp_seq=5 ttl=128 time=1.28 ms

--- 192.168.6.101 ping statistics ---
5 packets transmitted, 5 received, 0% packet loss, time 4052ms
rtt min/avg/max/mdev = 0.773/1.671/3.533/0.959 ms

Thu Jun 30 02:24:42 IST 2022

-rw-r--r--.  1 linuxhelp linuxhelp   18 Oct 10  2021 .bash_logout
-rw-r--r--.  1 linuxhelp linuxhelp  141 Oct 10  2021 .bash_profile
-rw-r--r--.  1 linuxhelp linuxhelp  376 Oct 10  2021 .bashrc
drwxr-xr-x.  2 linuxhelp linuxhelp    6 May 24 02:05 Desktop
drwxr-xr-x.  2 linuxhelp linuxhelp    6 May 24 02:05 Documents
drwxr-xr-x.  2 linuxhelp linuxhelp    6 May 24 02:05 Downloads
-rw-r--r--.  1 root      root         0 Jun 30 02:24 file1
drwxr-xr-x.  2 root      root         6 Jun 30 02:24 folder1
drwxr-xr-x.  2 linuxhelp linuxhelp    6 May 24 02:05 Music
drwxr-xr-x.  2 linuxhelp linuxhelp    6 May 24 02:05 Pictures
-rwxr-xr-x.  1 root      root       172 Jun 30 02:19 test.sh
drwxr-xr-x.  2 linuxhelp linuxhelp    6 May 24 02:05 Videos
Bash Script is Completed

Step 7: To Run Bash Script with variables and Create another file and grant permission by using the below command

[root@linuxhelp linuxhelp]# touch variables.sh

[root@linuxhelp linuxhelp]# chmod 775 variables.sh  

Step 8: Edit the variable.sh file by using the below command

#! /bin/bash
echo Enter your Name
read Name
echo Enter your Project name
read Project

echo Enter your Mail id
read Mail
echo "Details you Provided"
echo Your Name is $Name
echo Your Project is $Project
echo your Mail id is $Mail
echo Registration is Completed

Step 9: Run the variable.sh file by using the below command

[root@linuxhelp linuxhelp]# sh variables.sh 
Enter your Name
linuxhelp
Enter your Project name
Test
Enter your Mail id
linuxhelp@gmail.com
Details you Provided
Your Name is linuxhelp
Your Project is Test
your Mail id is linuxhelp@gmail.com
Registration is Completed

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 Oracle Linux 8.5. Your feedback is much welcome.

FAQ
Q
How does the Bash shell work?
A
Bash reads input from the terminal when interactive, and from the script file specified as an argument otherwise.
Q
What is shell script extension?
A
The shell script extension is .sh
Q
What is variable shell scripting?
A
A variable is a character string to which we assign a value. The value assigned could be a number, text, filename, device, or any other type of data
Q
Is Bourne Again a Linux shell?
A
Bash (Bourne Again Shell ) is the free version of the Bourne shell distributed with Linux and the GNU operating system.
Q
What is shell scripting?
A
A shell script is a text file that contains a sequence of commands for a UNIX-based operating system.