Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
100% found this document useful (1 vote)
214 views

Basic Shell Programming Part 1

Shell is the outermost layer of an operating system and provides an interface between the user and the kernel. There are two main types of shells - Bourne shell and C shell. Shell scripting allows combining commands into scripts to simplify repetitive tasks. A shell script is created by writing commands in a text file and making it executable. Variables are used to store and reference data in shell scripts. Special variables have predefined meanings and functions used by the shell.

Uploaded by

Pragat Gupta
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
214 views

Basic Shell Programming Part 1

Shell is the outermost layer of an operating system and provides an interface between the user and the kernel. There are two main types of shells - Bourne shell and C shell. Shell scripting allows combining commands into scripts to simplify repetitive tasks. A shell script is created by writing commands in a text file and making it executable. Variables are used to store and reference data in shell scripts. Special variables have predefined meanings and functions used by the shell.

Uploaded by

Pragat Gupta
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

SHELL Programming Part 1

An Operating is made of many components, but its two prime components are -

 Kernel
 Shell

 A Kernel is at the nucleus of a computer. It makes the communication between the hardware and software
possible. While the Kernel is the innermost part of an operating system, a shell is the outermost one.
 A shell in a Linux operating system takes input from you in the form of commands, processes it, and then gives
an output. It is the interface through which a user works on the programs, commands, and scripts. A shell is
accessed by a terminal which runs it.
 The Shell wraps around the delicate interior of an Operating system protecting it from accidental damage.
Hence the name Shell.

Types of Shell
There are two main shells in Linux:

1. The Bourne Shell: The prompt for this shell is $ and its derivatives are listed below:

 POSIX shell also is known as sh


 Korn Shell also knew as sh
 Bourne Again SHell also knew as bash (most popular)
2. The C shell: The prompt for this shell is %, and its subcategories are:

 C shell also is known as csh


 Tops C shell also is known as tcsh

Shell scripting is writing a series of command for the shell to execute. It can combine lengthy and repetitive
sequences of commands into a single and simple script, which can be stored and executed anytime. This reduces
the effort required by the end user.

steps in creating a Shell Script

1. Create a file using a vi editor(or any other editor). Name script file with extension .sh
2. Start the script with #! /bin/sh
3. Write some code.
4. Save the script file as filename.sh
5. For executing the script type bash filename.sh
6. "#!" is an operator called shebang which directs the script to the interpreter location. So, if we use"#! /bin/sh"
the script gets directed to the bourne-shell.
7. Let's create a small script -
8. #!/bin/sh
9. ls
10. Let's see the steps to create it -
11.
12. Command 'ls' is executed when we execute the scrip sample.sh file.

Adding shell comments


13. Commenting is important in any program. In Shell programming, the syntax to add a comment is
14. #comment
15. Let understand this with an example.
16.

Variables store data in the form of characters and numbers. Similarly, Shell variables are used to store information
and they can by the shell only.

For example, the following creates a shell variable and then prints it:

variable ="Hello"
echo $variable

Below is a small script which will use a variable.

#!/bin/sh
echo "what is your name?"
read name
echo "How do you do, $name?"
read remark
echo "I am $remark too!"

Variable Names
The name of a variable can contain only letters (a to z or A to Z), numbers ( 0 to 9) or the underscore character (
_).

By convention, Unix shell variables will have their names in UPPERCASE.

The following examples are valid variable names −


_ALI
TOKEN_A
VAR_1
VAR_2

Following are the examples of invalid variable names −


2_VAR
-VARIABLE
VAR1-VAR2
VAR_A!

The reason you cannot use other characters such as !, *, or - is that these characters have a special meaning for
the shell.

Defining Variables
Variables are defined as follows −
variable_name=variable_value

For example −
NAME="Zara Ali"

The above example defines the variable NAME and assigns the value "Zara Ali" to it. Variables of this type are
called scalar variables. A scalar variable can hold only one value at a time.

Shell enables you to store any value you want in a variable. For example −

VAR1="Zara Ali"

VAR2=100

Accessing Values
To access the value stored in a variable, prefix its name with the dollar sign ($) −

For example, the following script will access the value of defined variable NAME and print it on STDOUT −
Live Demo

#!/bin/sh

NAME="Zara Ali"

echo $NAME

The above script will produce the following value −


Zara Ali

Read-only Variables
Shell provides a way to mark variables as read-only by using the read-only command. After a variable is marked
read-only, its value cannot be changed.

For example, the following script generates an error while trying to change the value of NAME −
Live Demo

#!/bin/sh

NAME="Zara Ali"

readonly NAME

NAME="Qadiri"

The above script will generate the following result −


/bin/sh: NAME: This variable is read only.

Unsetting Variables
Unsetting or deleting a variable directs the shell to remove the variable from the list of variables that it tracks. Once
you unset a variable, you cannot access the stored value in the variable.

Following is the syntax to unset a defined variable using the unset command −
unset variable_name

The above command unsets the value of a defined variable. Here is a simple example that demonstrates how the
command works −

#!/bin/sh
NAME="Zara Ali"

unset NAME

echo $NAME

The above example does not print anything. You cannot use the unset command to unset variables that are
marked readonly.

Variable Types
When a shell is running, three main types of variables are present −

 Local Variables − A local variable is a variable that is present within the current instance of the shell. It is not available to
programs that are started by the shell. They are set at the command prompt.

 Environment Variables − An environment variable is available to any child process of the shell. Some programs need
environment variables in order to function correctly. Usually, a shell script defines only those environment variables that are
needed by the programs that it runs.

 Shell Variables − A shell variable is a special variable that is set by the shell and is required by the shell in order to function
correctly. Some of these variables are environment variables whereas others are local variables.

SPECIAL VARIABLE IN UNIX


variables are reserved for specific functions.

For example, the $ character represents the process ID number, or PID, of the current shell −
$echo $$

The above command writes the PID of the current shell −


29949

The following table shows a number of special variables that you can use in your shell scripts −

Sr.No. Variable & Description

1
$0

The filename of the current script.

2 $n

These variables correspond to the arguments with which a script was


invoked. Here n is a positive decimal number corresponding to the
position of an argument (the first argument is $1, the second argument
is $2, and so on).

3 $#

The number of arguments supplied to a script.

4 $*
All the arguments are double quoted. If a script receives two arguments,
$* is equivalent to $1 $2.

5 $@

All the arguments are individually double quoted. If a script receives two
arguments, $@ is equivalent to $1 $2.

6 $?

The exit status of the last command executed.

7 $$

The process number of the current shell. For shell scripts, this is the
process ID under which they are executing.

8 $!

The process number of the last background command.

You might also like