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 et...

Basic SQL Commands in Oracle Database #Diksha Oracle Training Centre

  Basic SQL Commands in Oracle Database.   Hello Everyone, In my previous blog I discussed about SQL and Classification of SQL . Today I will discuss about SQL basic commands which widely used in RDBMS. The Topics of SQL command which I am going to cover in this blog are mainly divided into four Categories: ·         DDL:   DDL consists of commands which are used to define and design the database. ·         DML:   DML consists of commands which are mainly used to manipulate the data in the database. ·        DCL: DCL Consists of commands which deal with the user permissions/access and controls in the database. ·       TCL:   TCL Consist of commands which deal with the transaction in the database. If you want to explore theory part please follow my below blog as in today blog I will discuss about how to use the commands. h...

Pseudo Column in Oracle #Diksha Oracle Training Centre

  Pseudo Column in Oracle. Hello Everyone, Today I will discuss about Pseudo column in Oracle. This topic is very important so before going to read this blog, I suggest you to please see my previous blogs which is in archive folder for your reference so that it’s easy for you to understand this topic. Let’s discuss about Pseudo Column in Oracle. Pseudo column: A pseudo-column is dynamic in Oracle as it behaves like a table column but is not stored in the table. Pseudo column behaves like a table column, but is not actually stored in the table. You can select from pseudo columns, but you cannot insert, update, or delete their values. A pseudo column is also similar to a function without arguments. In   Simple word we can say Pseudo column is a column that yields a value when selected, but which is not an actual column of the table. An example is RowID or SysDate. It can be use in combination with the DUAL table. Below are the Pseudo Column commonly used in Oracle Dat...