Home > Linux / Unix > Unix shell scripting tutorial

Unix shell scripting tutorial

Unix shell scripting tutorial

Unix shell scripting tutorial


What is Shell Scripting:

Shell Scripting is the computer program. It is collection of commands which are combined as a program.

In Linux, shells like bash and korn support programming construct which are saved as scripts. 

To understand how their servers and applications are started, upgraded, maintained or removed and to understand how a user environment.

Prerequisite:



Before learning Shell Scripting, you must have the basic knowledge of Operating System and some basic commands which will use in this Unix shell scripting tutorial like cd ,mkdir, ls etc.To know more Linux basic commands with examples click on this a
rticle :Linux basic commands with examples.

 Unix shell scripting tutorial


  • By using below commands “echo $0” or “echo $SHELL” , we can find out which shell we are using .As we can see we are using bash shell and the complete path of shell is /bin/bash.

Unix shell scripting tutorial

  • Type of Variable in Linux  / Unix.We have two types of variable .
  1. User defined variable : Any name (var1 ,var2) but not match to command such as cd, mkdir etc .
  2. System defined variable : It is defined in capital letter such as SHELL , HOME , PATH etc.
  • Program No.1 : Hello World


Open vi editor , create new file “hello_world.sh” here “.sh” is extension of shell as .c , .java, .net etc.

Hello World in Shell programming

and press “i” for insert mode and type below program then press “esc” then “:qw” for save and exit from vi editor.

Hello World in Shell programming 1

Make executable file by using below command “chmod +x hello_world.sh”.

Hello World in Shell programming 2

Hello World in Shell Hello World in Shell programming 4

Now we can execute file by using below command ” ./hello_world.sh “. Now you output of your first shell program.

Hello World in Shell programming 5

Program No.2 : Related to Variables (User Defined and System defined )


Here , We will perform shell script program related to variable.

  • User Defined variable example:

OUTPUT:

[[email protected] Desktop]# cat test 
#!/bin/bash

echo "First name:";
read var1;
echo "Second name:";
read var2;

echo "Your Name:$var1 $var2"
[[email protected] Desktop]#
[[email protected] Desktop]# ./test 
First name:
t4tution
Second name:
.com
Your Name:t4tution .com
[[email protected] Desktop]# 

Special Variables:

#!/bin/bash

echo "First Arg1:$1";
echo "Second Arg2:$2";
echo "Third Arg3:$3";

echo "Quated value:[email protected]";
echo "Quated value:$*";

echo "Number of Argument:$#";

[[email protected] Desktop]# ./test www t4tution com
First Arg1:www
Second Arg2:t4tution
Third Arg3:com
Quated value:www t4tution com
Quated value:www t4tution com
Number of Argument:3
[[email protected] Desktop]#
  • System defined variable: 

OUTPUT

Program No.3 : if else example

Here , we are going to  see example related to if and else.

If we logged in by Root or Simple user and execute below script then output will change accordingly.

As we can see below.

Here, “test” is a command which return 0 for true and 1 for false .

“$USER” is a system variable which return information about the logged in user.

OUTPUT

Program No.4 : for loop in shell script

Now, we are going to see example of “for” loop with printing number from 1 to 10.

for loop in shell script program-2
for loop in shell script

for loop in shell script program-2

OUTPUT

for loop in shell script

Program No.5 : while loop in shell script

Below example about while loop in shell scripting.

while loop in shell script example-1

while loop in shell

while loop in shell script example-2

OUTPUT

Admin
We are group of application developer and admin.We are providing good content of information about IT technology and services .

Leave a Reply

Your email address will not be published. Required fields are marked *