Skip to main content

Basic Shell Programming in Unix #DOTC_Mdb

 

Basic Shell Programming in Unix.

Hello Everyone,

Today we will discuss about how to write simple shell program in Unix and in next blog we will discuss more about shell scripts. Before starting basic shell programming please go through my below blog related with Unix command so that you can able to write basic shell programs.

https://dheeraj60.blogspot.com/2020/08/basic-and-some-advance-command-in-unix.html

https://dheeraj60.blogspot.com/2020/08/basic-and-some-advance-command-in-unix.html

https://dheeraj60.blogspot.com/2020/08/advance-unix-command-part1-dotcmdb.html

https://dheeraj60.blogspot.com/2020/08/advance-unix-command-part-2-dotcmdb.html

 

I am writing one basic simple script which will list all the files in the current directory , also it will show the present working directory name as well as all the process running in the system. Suppose we create a test.sh script , the scripts would have the .sh extension. Before you add anything else to your script, you need to alert the system that a shell script is being started. This is done using the shebang construct that is #!/bin/sh  as it  tells the system that the commands follow are to be executed by the Bourne shell.  # symbol is called a hash, and the ! symbol is called a bang that’s why it known as shebang construct.

Steps to write first test.sh script to see the present working directory , list of all the files with timestamp and the process which is running in the system.

1. We need to open vi editor by writing vi test.sh in command prompt.

2. Once vi editor open to write anything we need to go into insert mode by using command esc i at same time to write the script in vi window.

3. Writing the script in vi editor as below for requirement.

 #!/bin/bash

# Author : Dheeraj Jha

# Script1 : To see all the files , process running and present working directory

pwd

ls-ltr

ps –ef

Note : Hope you all are aware about this command as we discussed in my previous blogs only #!/bin/sh   you are not aware which i already described above in this blog that is shebang . and # means comment.

4. Once you write the script to save you need to put esc command after that use :wq to save the file.

Note : if you use :q then it will not save your script as it will quit the file but :wq means save the file .

5. After this we can see the test.sh file in current directory but for execution of this script we need to give the permission of this file by using chmod command  as below

$ chmod +x test.sh

Note : We can use  chmod 777 test.sh command as well to execute this script.

6. Now we are able to run the test script by simply using the below command and it will give you the output.

$ ./test.sh

Output of above shell script in Putty terminal is as below:



 

Writing another shell program  script uses the read command which takes the input from the keyboard and assigns it as the value of the  any variable  like DOTC and finally prints it. We need to follow the same step which we have done for test.sh script . So I am writing test1.sh script in vi editor is as below.

#!/bin/sh

# Author : Dheeraj Jha

# Script2 : To Take input as variable

echo "Training Centre Name?"

read DOTC

echo "Hello, $DOTC"

 

After that we need to save this test1.sh script and to give permission to this script we need to use chmod +x test1.sh or chmod 777 test1.sh same we done for test.sh script then we run the command $./test1.sh .

Once we run the command output of the script is as below in Putty terminal.



 

Here we put input as Diksha Oracle Training Centre so output is as Hello , Diksha  Oracle Training Centre.

 

Now writing one more simple test3.sh script for array program to access the name by index of the value to be accessed . Here in test2.sh script we particularly access 2 items from the array in Program.

Writing test2.sh is similar to previous script we have written in vi editor.

Below is the script

#!/bin/sh

# Author: Dheeraj Jha

# Script 3 : Accessing particular elements from Array

NAME[0]="Diksha"

NAME[1]="Oracle"

NAME[2]="Training"

NAME[3]="Centre"

NAME[4]="Madhubani"

echo "First Index: ${NAME[0]}"

echo "Second Index: ${NAME[1]}"


After writing this in vi editor save this by using :wq and then use chmod 777 test2.sh

In test3.sh script we can access all the items in an array by using below script :

Program we need to write in vi editor is as below:

#!/bin/sh

# Author : Dheeraj Jha

# Script 4 : Accessing all the elements from Array

NAME[0]="Diksha"

NAME[1]="Oracle"

NAME[2]="Training"

NAME[3]="Centre"

NAME[4]="Madhubani"

echo "First Method: ${NAME[*]}"

echo "Second Method: ${NAME[@]}"

After this we need to save test3.sh and give the permission by using chmod 777 test3.sh command .

Note : We use ${array_name[*]}  and ${array_name[@]} in script to access all the elements .

Output of both test2.sh and test3.sh in Putty Terminal is as below :



This is the way you can write basic shell scripts in Unix and if you are familiar with C programming it’s easy for you to write several shell scripts also in Unix we can write C program as well as database script to run in the Unix operating system. In next blog I will come up with some advance shell script and also some scripts related with database. Hope in this blog you are familiar with writing small scripts by using vi editor.

Please read this blog carefully and let me know if you have any questions. Also I will suggest you to please go through my previous blogs related with Unix commands. In next blog I will come up with more shell scripting program so that you can easily interact with other languages in Unix Platform.

Thanks.

 

 

 

 

Comments

Post a Comment

Popular posts from this blog

SQL and Classification of SQL in Oracle Database #Diksha Oracle Training Centre

  SQL and Classification of SQL in Oracle Database.   SQL is Structured Query Language , which is used for storing, manipulating and retrieving data stored in a relational database .SQL is the standard language for RDBMS. All the Relational Database Management Systems (RDMS) like Oracle, MySQL, Sybase, Informix, IBM DB2 and Microsoft SQL Server use SQL as their standard database language. Oracle is one of the more secured database as compared to other databases. Importance of   SQL : SQL and PL/SQL is a backend process where all data is stored and retrieved in GUI which created either by any programming languages like Java, C++, PHP etc. so we need to have very secure database so that there will be no impact for users. SQL allows users to access data in the relational database management systems. SQL is used to communicate with a database.SQL and PL/SQL allows users to create and drop databases tables , views , stored procedures , functions , packages , trigger etc. SQL allows

Materialized View uses and Benefits in Database #DOTC_Mdb

Materialized View uses and Benefits in Database. Hello Everyone, Today we will discuss about Materialized view as it’s play important role in database. We already discussed about Simple Views and complex views in my previous blog. Before Materialized view go through my previous blog which related to simple view. https://dheeraj60.blogspot.com/2020/05/benefits-of-creating-and-using-view-in.html As we know View is not a database object and not like table which is stored in database, but view can be created from base table a view is an SQL statement that’s stored in the database. This statement, or view, has a name.A view looks and acts a lot like a table. It has columns and rows, and can be included in SELECT queries just like a table. In other word we can say View is a virtual/logical table which is basically used for security purpose. Let’s understand   about   Materialized view : A materialized view is a view that stores the results of the view’s query. Whenever you query the ma

Top 50 Interview Questions On SQL and PL/SQL #DOTC_Mdb

                    Top 50 Interview Questions On SQL and PL/SQL. Today we will Discuss Top 50 interview questions and answers of SQL and PL/SQL which is frequently asked in interview.     Question 1: What is SQL and Classification of SQL? Answer SQL is a Structure Query Language which is vastly used in RDBMS database like Oracle, Sybase, DB2 , Microsoft SQL server etc.   Classification of SQL is as below: DDL (Data Definition Language):  Commands are  create , alter , drop , truncate etc DML (Data Manipulation Language) : Commands are  insert,update and delete . TCL (Transaction Control Language ) : Commands are  Commit , Rollback and Save point. DCL (Data Control Language) : Commands are Grant , Revoke Question 2:    What is meant by Joins? What are the types of join? Answer Joins are basically used to extract/get data from multiple tables using some common columns or conditions and also by using alias to fulfill the condition.   There are various types of Joins as li