Slide 03 3 Shell Script
Slide 03 3 Shell Script
Source(Ubuntu)
Shell
✓ A shell is a command-line interpreter that provides a user interface for interacting with
the operating system.
✓ It allows users to execute commands, manage files and directories, and automate tasks
using shell scripts.
✓ A shell script is simply an ordinary text file containing a series of commands in a shell
command language (just like a "batch file" under MS-DOS).
Shell and Shell Scripting
Shell Types:
✓ In UNIX there are two major types of shells:
I. The Bourne shell. If you are using a Bourne-type shell, the default prompt is the
$ character.
II. The C shell. If you are using a C-type shell, the default prompt is the %
character.
✓ There are again various subcategories for Bourne Shell which are listed as follows:
➢ Bourne shell ( sh)
➢ Korn shell ( ksh)
➢ Bourne Again shell ( bash)
➢ POSIX shell ( sh)
✓ The different C-type shells follow:
➢ C shell ( csh)
➢ TENEX/TOPS C shell ( tcsh)
Shell and Shell Scripting
Shell Scripts:
✓ The basic concept of a shell script is a list of commands, which are listed in the order of
execution. A good shell script will have comments, preceded by a pound sign, #, describing the
steps. Shell scripts and functions are both interpreted. This means they are not compiled.
Example Script:
✓ Assume we create a ss1.sh script. Note all the scripts would have .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.
✓ For example: #!/bin/sh This tells the system that the commands that follow are to be executed
by the Bourne shell. It's called a shebang because the # symbol is called a hash, and the !
symbol is called a bang.
Shell and Shell Scripting
Example - 01:
✓ First create a text file ss1.sh
#!/bin/bash
# This is my first shell script
echo “Hello, World.”
✓ And then execute the shell script (sh)
$ sh ss1.sh
Hello, World.
Example - 02:
✓ To create a script containing pwd,ls commands, you put the shebang line first and then
add the commands: #!/bin/bash
pwd
ls
Shell and Shell Scripting
Shell Comments:
✓ Shell scripts have several required constructs that tell the shell environment what to do
and when to do it.
✓ The shell is, after all, a real programming language, complete with variables, control
structures, and so forth.
✓ No matter how complicated a script gets, however, it is still just a list of commands
executed sequentially.
✓ Following script use the read command which takes the input from the keyboard and
assigns it as the value of the variable PERSON and finally prints it on STDOUT.
Shell and Shell Scripting
Variables
✓ The value assigned could be a number, text, filename, device, or any other type of
data.
✓ A variable is nothing more than a pointer to the actual data. The shell enables you to
create, assign, and delete variables.
Variable Names:
Defining Variables:
variable_name=variable_value
Accessing Values:
✓ To access the value stored in a variable, prefix its name with the dollar sign ( $):
✓ For example, following script would access the value of defined variable NAME and
would print it on STDOUT:
Shell and Shell Scripting
Read-only Variables:
✓ The shell provides a way to mark variables as read-only by using the readonly
command.
Unsetting Variables:
✓ Unsetting or deleting a variable tells the shell to remove the variable from the list of
variables that it tracks.
✓ Once you unset a variable, you would not be able to access stored value in the
variable.
✓ Following is the syntax to unset a defined variable using the unset command:
unset variable_name
Shell and Shell Scripting
Variable Types:
➢ 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 command prompt.
➢ 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 variable. HISTFILE, COLUMNS,
PS1, BASH
Shell and Shell Scripting
Special Variables
Shell and Shell Scripting
Special Variables
✓ We can write the shell script shown below to process an unknown number of
command-line arguments with either the $* or $@ special parameters:
#!/bin/sh
for TOKEN in $*
do
echo $TOKEN
done
Exit Status:
✓ The $? variable represents the exit status of the previous command. Exit status is a
numerical value returned by every command upon its completion. As a rule, most
commands return an exit status of 0 if they were successful, and 1 if they were
unsuccessful.
Shell and Shell Scripting
Basic Operators
➢ Arithmetic Operators.
➢ Relational Operators.
➢ Boolean Operators.
➢ String Operators.
✓ The Bourne shell didn't originally have any mechanism to perform simple arithmetic
but it uses external programs, either awk or the must simpler program expr.
Shell and Shell Scripting
Basic Operators
-s file : Check if file has size greater than 0 if yes then condition becomes true.
-e file : Check if file exists. Is true even if file is a directory but exists.
Shell and Shell Scripting
Decision Making
✓ While writing a shell script, there may be a situation when you need to adopt one path
out of the given two paths.
✓ So you need to make use of conditional statements that allow your program to make
correct decisions and perform right actions.
✓ Unix Shell supports conditional statements which are used to perform different actions
based on different conditions.
✓
Shell and Shell Scripting
✓ If else statements are useful decision making statements which can be used to select an
option from a given set of options.
if...fi statement
✓ The if...fi statement is the fundamental control statement that allows Shell to make
decisions and execute statements conditionally.
Syntax:
if [ condition ]; then
# Code to execute if the condition is true
else
# Code to execute if the condition is false
fi
Shell and Shell Scripting
if...elif...else...fi statement
✓ The if...elif...fi statement is the one level advance form of control statement that allows
Shell to make correct decision out of several conditions.
Syntax:
Shell and Shell Scripting
✓ You can use multiple if...elif statements to perform a multiway branch. However, this
is not always the best solution, especially when all of the branches depend on the value
of a single variable.
✓ Unix Shell supports case...esac statement which handles exactly this situation, and it
does so more efficiently than repeated if...elif statements.
Syntax:
✓ The basic syntax of the case...esac statement is to give an expression to evaluate and
several different statements to execute based on the value of the expression.
✓ The interpreter checks each case against the value of the expression until a match is
found. If nothing matches, a default condition will be used.
Shell and Shell Scripting
Syntax:
✓ When statement(s) part executes, the command ;; indicates that program flow should
jump to the end of the entire case statement.
Shell and Shell Scripting
Shell Loops
✓ Loops are a powerful programming tool that enable you to execute a set of commands
repeatedly.
✓ The while loop enables you to execute a set of commands repeatedly until some
condition occurs. It is usually used when you need to manipulate the value of a
variable repeatedly.
Syntax:
Shell and Shell Scripting
Example:
Here is a simple example that uses the while loop to display the numbers zero to nine:
Shell and Shell Scripting
✓ The for loop operate on lists of items. It repeats a set of commands for every item in a
list.
Syntax:
✓ Here var is the name of a variable and word1 to wordN are sequences of characters
separated by spaces (words). Each time the for loop executes, the value of the variable
var is set to the next word in the list of words, word1 to wordN.
Shell and Shell Scripting
Example:
✓ Here is a simple example that uses for loop to span through the given list of numbers:
✓ Following is the example to display all the files starting with .bash and available in
your home. I'm executing this script from my root:
Shell and Shell Scripting
Example:
✓ Here is a simple example that uses for loop to span through the given list of numbers:
✓ Following is the example to display all the files starting with .bash and available in
your home. I'm executing this script from my root:
Shell and Shell Scripting
Loop Control
✓ So far you have looked at creating loops and working with loops to accomplish
different tasks.
✓ Sometimes you need to stop a loop or skip iterations of the loop. In this section you
will learn following two statements used to control shell loops:
Loop Control
✓ is used to terminate the execution of the entire loop, after completing the execution
of all of the lines of code up to the break statement.
✓ It then steps down to the code following the end of the loop.
Shell and Shell Scripting
✓ The continue statement is similar to the break command, except that it causes the
current iteration of the loop to exit, rather than the entire loop.
✓ This statement is useful when an error has occurred but you want to try to execute the
next iteration of the loop.
Shell and Shell Scripting
Example:
✓ The following loop makes use of continue statement which returns from the continue
statement and start processing next statement:
Thanx !!!