SlideShare a Scribd company logo
1 of 43
Using Shells and Shell Programming
Mrs.Harsha V Patil, MIT ACSC Alandi , Pune
Shell Programming : The shell provides you with an interface to the UNIX
system. It gathers input from you and executes programs based on that input.
When a program finishes executing, it displays that program's output.
A shell is an environment in which we can run our commands, programs, and
shell scripts. There are different flavors of shells, just as there are different
flavors of operating systems. Each flavor of shell has its own set of recognized
commands and functions.
Shell Prompt:
 The prompt, which is called command prompt, is issued by the shell. While the
prompt is displayed, you can type a command . The shell reads your input after
you press Enter. It determines the command you want executed by looking at
the first word of your input. A word is an unbroken set of characters. Spaces
and tabs separate words.
 Following is a simple example of date command which displays current date
and time:
$date
Thu Jun 2508:30:19 MST 2009
Mrs.Harsha V Patil, MIT ACSC Alandi , Pune
Shell Types:
In UNIX there are two major types of shells:
1. The Bourne shell. If you are using a Bourne-type shell, the default prompt is
the
$ character.
2. 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)
Mrs.Harsha V Patil, MIT ACSC Alandi , Pune
The different C-type shells follow:
• C shell ( csh)
• TENEX/TOPS C shell ( tcsh)
 The original UNIX shell was written in the mid-1970s by Stephen R. Bourne
while he was at AT&T Bell Labs in New Jersey.
 The Bourne shell was the first shell to appear on UNIX systems, thus it is
referred to as "the shell".
 The Bourne shell is usually installed as /bin/sh on most versions of UNIX. For
this reason, it is the shell of choice for writing scripts to use on several different
versions of UNIX.
Mrs.Harsha V Patil, MIT ACSC Alandi , Pune
Shell Interpretive Cycle
1. Shell issues the prompt and waits for you (user) to enter a command
2. After a command is entered, the shell scans command line for meta-characters
and expands abbreviations to recreate simplified commands.
3. It then passes on the command line to kernel for execution
4. The shell waits for the command to complete and normally can't do anything
while the command is running.
5. After the command gets executed the prompt reappear and the whole cycle is
repeated again.
Mrs.Harsha V Patil, MIT ACSC Alandi , Pune
Wild cards and file name generation.
When you have a number of files named in series (for
example, chap1 to chap12) or filenames with common characters
(like aegis, aeon, and aerie), you can use wildcards (also called metacharacters)
to specify many files at once. These special characters are *
(asterisk), ? (question mark), and [ ] (square brackets). When used in a
filename given as an argument to a command:
1. * An asterisk is replaced by any number of characters in a filename. For
example, ae* would match aegis, aerie, aeon, etc. if those files were in the same
directory. You can use this to save typing for a single filename (for
example, al* for alp
2.? A question mark is replaced by any single character
(so h?p matches hop and hip, but not help).
3. [] Square brackets can surround a choice of characters you’d like to match.
Any one of the characters between the brackets will be matched. For example,
[Cc]hapter would match either Chapter or chapter, but [ch]apter would match
either capter or hapter. Use a hyphen (-) to separate a range of consecutive
characters. For example, chap[13] would match chap1, chap2, or chap3.
Mrs.Harsha V Patil, MIT ACSC Alandi , Pune
Three Standard Files and Redirection:
Once a command begins running, it has access to three files:
1.Standard Input-It reads from its standard input file.
By default, standard input is the keyboard.
2.Standard Output-It writes to its standard output file.
If you invoke a shell command from the shell, a C program, or a REXX program
invoked from TSO READY, standard output is directed to your terminal screen
by default.If you invoke a shell command, REXX program, or C program from
the ISPF shell, standard output cannot be directed to your terminal screen. You
can specify a z/OS UNIX file or use the default, a temporary file.
3.Standard Error-It writes error messages to its standard error file.
If you invoke a shell command from the shell or from a C program or from a
REXX program invoked from TSO READY, standard error is directed to your
terminal screen by default.
If you invoke a shell command, REXX program, or C program from the ISPF
shell, standard error cannot be directed to your terminal screen. You can
specify a z/OS UNIX file or use the default, a temporary file.If the standard
output or standard error file contains any data when the command completes,
the file is displayed for you to browse.
Mrs.Harsha V Patil, MIT ACSC Alandi , Pune
Types of Redirection:
1.Overwrite Redirection:
Overwrite redirection is useful when you want to store/save the output of a
command to a file and replace all the existing content of that file. for
example, if you run a command that gives a report, and you want to save the
report to the existing file of the previous report you can use overwrite
redirection to do this.
“>” standard output
“<” standard input
2. Append Redirection:
With the help of this Redirection, you can append the output to the file
without compromising the existing data of the file.
“>>” standard output
“<<” standard input
Mrs.Harsha V Patil, MIT ACSC Alandi , Pune
3. Merge Redirection:
This allows you to redirect the output of a command or a program to a specific
file descriptor instead of standard output. the syntax for using this is “>&”
operator followed by the file descriptor number.
 “p >& q” Merges output from stream p with stream q
 “p <& q” Merges input from stream p with stream q
Connecting commands: Pipe.
A pipe is a form of redirection (transfer of standard output to some other
destination) that is used in Linux and other Unix-like operating systems to
send the output of one command/program/process to another
command/program/process for further processing.
The Unix/Linux systems allow stdout of a command to be connected to stdin of
another command. You can make it do so by using the pipe character ‘|’. Pipe is
used to combine two or more commands, and in this, the output of one
command acts as input to another command, and this command’s output may
act as input to the next command and so on.
Mrs.Harsha V Patil, MIT ACSC Alandi , Pune
 It can also be visualized as a temporary connection between two or more
commands/ programs/ processes. The command line programs that do the
further processing are referred to as filters.
 This direct connection between commands/ programs/ processes allows them
to operate simultaneously and permits data to be transferred between them
continuously rather than having to pass it through temporary text files or
through the display screen.
 Pipes are unidirectional i.e data flows from left to right through the
pipeline.
 Syntax : command_1 | command_2 | command_3 | .... | command_N
 Example :
1. Listing all files and directories and give it as input to more command.
 $ ls -l | more
Mrs.Harsha V Patil, MIT ACSC Alandi , Pune
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.
 There are conditional tests, such as value A is greater than value B, loops
allowing us to go through massive amounts of data, files to read and store data,
and variables to read and store data, and the script may include functions.
 Shell scripts and functions are both interpreted. This means they are not
compiled.
 It supports less features. It supports input and output redirection operators.
Example Script:
 Assume we create a test.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
Mrs.Harsha V Patil, MIT ACSC Alandi , Pune
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.
 To create a script containing these commands, you put the shebang line first
and then add the commands:
#!/bin/bash
pwd
ls
Shell Comments:
You can put your comments in your script as follows:
#!/bin/bash
# this is sscasc
# Copyright (c)
sscasc.com #
Script follows
here: pwd
ls
Mrs.Harsha V Patil, MIT ACSC Alandi , Pune
Now you save the above content and make this script executable as follows:
$chmod +x test.sh
Now you have your shell script ready to be executed as
follows: $./test.sh
Extended Shell Scripts:
 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.
echo "What is your name?"
read PERSON
echo "Hello,$PERSON"
Mrs.Harsha V Patil, MIT ACSC Alandi , Pune
Job ID Versus Process ID
 Background and suspended processes are usually manipulated via job number
(job ID). This number is different from the process ID and is used because it is
shorter.
 In addition, a job can consist of multiple processes running in a series or at the
same time, in parallel. Using the job ID is easier than tracking individual
processes.
Nice command:-
 It is used to change or set the priority of a process
 syntax: $nice-value cat filename
 The default priority of a process in unix is 20
 The value range from 0 to 39, in linux-9 to 20.where 0 is high and 39 is lower
value.
 the default value of reduction is 10.
 The priority of a process can be increased only by administrator using double
minus(--).
 eg:-$nice--15catlast.txt
 The priority of a process can be made lower using the nice command.
Mrs.Harsha V Patil, MIT ACSC Alandi , Pune
Here is sample run of the script:
 $./test.sh
 What is your
 name? vvfgc
 Hello, vvfgc
Variables:
 Variable is value that always changes during execution of a program. It is an
integral part of shell programming. They provide the ability to store and
manipulate information.
 There are 2 types of variables. They are
• Environment variables
• User defined variables
Mrs.Harsha V Patil, MIT ACSC Alandi , Pune
Environment variables:
These variables are the part of the system and these are created and maintained
by the
system itself. These variables always in capital letters only.
Variable meaning
 PS1- this is first prompt setting in Unix ($)
 PS2- this is second prompt setting in Unix (>)
 PATH- whether we are used absolute or relative path.
 HOME- it stores the current root directory.
 LOGNAME-it stores the login name of the user.
User defined variable:
Variables are defined as follows::
variable_name = variable_value
Mrs.Harsha V Patil, MIT ACSC Alandi , Pune
For example:
NAME = "sscasc"
Above example defines the variable NAME and assigns it the value "sscasc".
Variables of this type are called scalar variables. A scalar variable can hold only
one value at a time.
The shell enables you to store any value you want in a variable. For example:
VAR1="ssczsc "
VAR2=100
Accessing Values to variables:
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:
NAME="vvfgc "
echo $NAME
This would produce following value:
Output: vvfgc
Mrs.Harsha V Patil, MIT ACSC Alandi , Pune
Read-only Variables:
 The 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, following script would give error while trying to change the value
of NAME:
NAME="vvfgc “
readonly NAME
NAME="Qadiri“
This would produce following result:
/bin/sh: NAME: This variable is read only.
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.
Mrs.Harsha V Patil, MIT ACSC Alandi , Pune
Following is the syntax to unset a defined variable using the unset
command: unset variable_name
Above command would unset the value of a defined variable. Here is a simple
example
NAME="vvfgc"
unset
NAME
echo $NAME
Above example would not print anything. You cannot use the unset command to
unset variables that are marked readonly.
Mrs.Harsha V Patil, MIT ACSC Alandi , Pune
Read command :
This command is used to take the input from the user.
Syntax: $read var1 var2 var3 ….. var n
Syntax: $ read var1
The variable used along with the read command need not be preceded by the
$ symbol.
Ex: clear echo “enter ur name”
read name
echo “hello $name”
Output: enter ur name :vvfgc
Hello vvfgc
Mrs.Harsha V Patil, MIT ACSC Alandi , Pune
Expr command:
This command is used to perform mathematical caluculations.
Syntax: ` expr operand1 operator operand2 `
Where ( ` ) this symbol is known as grep symbol. There is always must a space
between symbol and the expr command.
Ex: clear echo “enter 2 numbers”
read a b
echo “ sum of 2 numbers is ` expr $a + $b ` “
echo “ sub of 2 numbers is ` expr $a - $b ` “
echo “ product of 2 numbers is ` expr $a * $b ` “
echo “ quotient of 2 numbers is ` expr $a /$b ` “
Note: In unix multiplication purpose we use the symbol of “*” because only * is
wild card character.
Mrs.Harsha V Patil, MIT ACSC Alandi , Pune
Test operator [numerical test]
 Arithmetic Operators:
 There are following arithmetic operators supported by
Bourne Shell.
 Assume variable “a” holds 10 and variable “b” holds 20
then:
 Show Examples
Mrs.Harsha V Patil, MIT ACSC Alandi , Pune
Operator Description Example
+ Addition - Adds values on either side of the operator `expr $a + $b` will give 30
- Subtraction - Subtracts right hand operand from left hand operand `expr $a -
$b` will give 10
* Multiplication - Multiplies values on either side of the operator `expr $a * $b`
will give 200/
/ Division - Divides left hand operand by right operand `expr $b / $a` will give 2
% Modulus - Divides left hand operand by right hand operand and returns
remainder `expr $b % $a` will give 0
=Assignment - Assign right operand in left a=$b would assign value of b into a
operand
= = Equality - Compares two numbers, if both are [ $a == $b ] would return same
then returns true. false.
!=Not Equality - Compares two numbers, if both [ $a != $b ] would return are
different then returns true. true.
Mrs.Harsha V Patil, MIT ACSC Alandi , Pune
 It is very important to note here that all the conditional expressions would be
put inside
 square braces with one spaces around them, for example [ $a == $b ] is correct
where as
 [$a==$b] is incorrect.
All the arithmetical calculations are done using long integers.
Relational Operators:
 Bourne Shell supports following relational operators which are specific to
numeric values.
 These operators would not work for string values unless their value is numeric.
 For example, following operators would work to check a relation between 10
and 20 as well as in between "10" and "20" but not in between "ten" and
"twenty".
 Assume variable “a” holds 10 and variable “b” holds 20 then:
Mrs.Harsha V Patil, MIT ACSC Alandi , Pune
Operator Description Example:
-eq Checks if the value of two operands is equal or not, if yes then condition
becomes true. [ $a -eq $b ] is not true.
-ne Checks if the value of two operands is equal or not, if values are not equal
then condition becomes true [ $a -ne $b ] is true.
-gt Checks if the value of left operand is greater than the value of right operand, if
yes then condition becomes true. [ $a -gt $b ] is not true.
-lt Checks if the value of left operand is less than the value of right operand, if yes
then condition becomes true.[ $a -lt $b ] is true.
-ge Checks if the value of left operand is greater than or equal to the value of right
operand, if yes then condition becomes true.[ $a -ge $b ] is not true.
-le Checks if the value of left operand is less than or equal to the value of right
operand, if yes then condition becomes true [ $a -le $b ] is true.
 Note here that all the conditional expressions would be put inside square
braces with one spaces around them, for example [ $a <= $b ] is correct where as
[$a <= $b] is incorrect.
Mrs.Harsha V Patil, MIT ACSC Alandi , Pune
 Boolean Operators (or) logical operators:
 There are following Boolean operators supported by
Bourne Shell.
 Assume variable “a” holds 10 and variable “b” holds 20
then:
 Show Examples
Mrs.Harsha V Patil, MIT ACSC Alandi , Pune
Operator Description Example
! This is logical negation. This inverts a true condition into false and vice versa.[ !
false ] is true.
-o This is logical OR. If one of the operands is true then condition would be true.[
$a -lt 20 - $b -gt 100 ] is true.
-a This is logical AND. If both the operands are true then condition would be true
otherwise it would be false.[ $a -lt 20 -a $b -gt 100 ] is false.
String test Operators
There are following string operators supported by Bourne Shell.
Assume variable a holds "abc" and variable b holds "efg" then:
Show Examples
Mrs.Harsha V Patil, MIT ACSC Alandi , Pune
Operator Description Example
= Checks if the value of two operands is equal or not, if yes then condition
becomes true. [ $a = $b ] is not true.
!= Checks if the value of two operands is equal or not, if values are not equal then
condition becomes true.[ $a != $b ] is true.
-z Checks if the given string operand size is zero. [ -z $a ] is not true. If it is zero
length then it returns true.
-n Checks if the given string operand size is non- zero. If it is non-zero length
then it returns true. [ -n $a ] is true.
Check if str is not the empty string. If it is str empty then it returns false.[ $a ] is
not false.
Mrs.Harsha V Patil, MIT ACSC Alandi , Pune
File Test Operators
There are following operators to test various properties associated with a Unix
file.
Assume a variable file holds an existing file name "test" whose size is 100
bytes and has read, write and execute permission on:
Show Examples
Operator Description Example
b file Checks if file is a block special file if yes then condition becomes true. [ -b
$file ] is false.
-c file Checks if file is a character special file if yes then condition becomes true.[
-b $file ] is false.
-d file Check if file is a directory if yes then condition becomes true.[ -d $file ] is
not true.
-f file Check if file is an ordinary file as opposed to a directory or special file if yes
then condition becomes true. [ -f $file ] is true.
Mrs.Harsha V Patil, MIT ACSC Alandi , Pune
-g file Checks if file has its set group ID (SGID) bit set if yes then condition
becomes true. [ -g $file ] is false.
-k file Checks if file has its sticky bit set if yes then condition becomes true. [ -k
$file ] is false.
-p file Checks if file is a named pipe if yes then condition becomes true.[ -p $file ]
is false.
-t file Checks if file descriptor is open and associated
with a terminal if yes then condition becomes true.[ -t $file ]is false.
-u file Checks if file has its set user id (SUID) bit set if yes then condition
becomes true.[ -u $file ] is false.
-r file Checks if file is readable if yes then condition becomes true.[ -r $file ] is
true.
-w file Check if file is writable if yes then condition becomes true.[ -w $file ] is
true.
-x file Check if file is execute if yes then condition becomes true.[ -x $file ] is true.
-s file Check if file has size greater than 0 if yes then condition becomes true.[ -s
$file ] is true.
-e file Check if file exists. Is true even if file is a directory but exists.[ -e $file ] is
true.
Mrs.Harsha V Patil, MIT ACSC Alandi , Pune
Control statements
The ability to control the flow of execution of program is known as control
statements . The different types of control structures are
 Sequence control structure
 Selection control structure
 Looping control structure
Selection control structure
This type of instruction allows the shell script to be executed depending on the
condition. There are mainly 4 types of decision making instructions. They are
 if-then-fi statement
 if-then-else-fi statement
 if-then-elif-then-else-fi statement case-esac statement
if-then-fi statement:
in this first check the condition. That condition is true then only the if block
statements will be executed otherwise the cursor transfer outside the if
condition.
Mrs.Harsha V Patil, MIT ACSC Alandi , Pune
Syntax: ex:
if [ condition ] if [ $a –gt 18 ]
then then
statements echo “eligible for vote”
fi fi
if-then-else-fi statement:
in this first check the condition. That condition is true then only the if block
statements will be executed otherwise the else block statements will be executed.
Syntax: ex:
if [ condition ] if [ $a –gt $b ]
then then
statements echo “a is larger than b”
else else
statements echo “b is larger than a”
fi fi
Mrs.Harsha V Patil, MIT ACSC Alandi , Pune
if-then-elif-then-else-fi statement:
in this first check the condition. That condition is true then only the if block
statements will be executed otherwise the cursor checks the next condition
then the second condition will be true then inside that statements will be
executed and so on. If any conditions were not true then the else block
statements will be executed.
Syntax: ex:
if [ condition 1 ] if [ $a –gt $b –a $a –gt $c]
then then
statements echo ”a is larger”
elif [ condition 2] elif [ $b –gt $c ]
then then
statements echo “ b is larger”
else else
statements echo “c is larger”
fi fi
Mrs.Harsha V Patil, MIT ACSC Alandi , Pune
Case-esac statement:
Syntax: case $variable in
[match1] )statements ; ;
[match2] statements ; ;
[match3] statements ; ;
: :
: :
*) statements ; ;
esac
here match1,match2 etc are the case labels.
• When a case statement is evaluated the value of variable is matched in any one
of the choices.
• When a match is found then shell executes that corresponding match
statements.
• The pair of semicolon at the end of every choices. It identifies break.
• *) indicates default class. Ex: clear echo “enter a character”
Mrs.Harsha V Patil, MIT ACSC Alandi , Pune
read ch
case $ch in
[a-z]) echo “entered character is lowercase letters” ; ;
[A-Z] echo “entered character is uppercase letters” ; ;
[0-9] echo “entered character isdigit” ; ;
*) echo “invalid choice” ;;
esac
Looping Control statements
In this all statements are executed repeatedly again and again as long as
condition is true.
This is also known as repetition or iteration.
Shell allows various types of looping. They are
• While loop
• Until loop
• For loop
Mrs.Harsha V Patil, MIT ACSC Alandi , Pune
While loop:
 This is the pretested loop or entry controlled loop. In this first check the
condition, if that condition was true then control enter inside the loop
otherwise control transferred outside the loop.
Syntax: ex:
while [ condition ] while [ i –le 10 ]
do do
Statements echo “$i”
done i =` expr $i + 1 `
done
Mrs.Harsha V Patil, MIT ACSC Alandi , Pune
until loop:
 This is also pretested loop or entry controlled loop. In this first check the
condition, if that condition was false then control enter inside the loop
otherwise control transferred outside the loop.
Syntax: ex:
until [ condition ] until [ i -ge 10 ]
do do
Statements echo “$i”
done i =`expr $i + 1 `
done
Mrs.Harsha V Patil, MIT ACSC Alandi , Pune
for loop:
This is a fixed execution loop. This loop is allow to execute list of statements
certain period of time.
Syntax:
for variable in value1 value2 value3….. value n do
statements
done
ex:
for i in 1 2 3 4 5
do
echo $i
i=` expr $i + 1 `
done
Mrs.Harsha V Patil, MIT ACSC Alandi , Pune
Command line arguments.
 The Unix shell is used to run commands, and it allows users to pass run time
arguments to these commands.
 These arguments, also known as command line parameters, that allows the
users to either control the flow of the command or to specify the input data for
the command.
 While running a command, the user can pass a variable number of parameters
in the command line.
 Within the command script, the passed parameters are accessible using
‘positional parameters’. These range from $0 to $9, where $0 refers to the name
of the command itself, and $1 to $9 are the first through to the ninth parameter,
depending on how many parameters were actually passed.
Example:
$ sh hello how to do you do
Here $0 would be assigned sh
$1 would be assigned hello
$2 would be assigned how
And so on …
Mrs.Harsha V Patil, MIT ACSC Alandi , Pune
Some additional commands to process these parameters.
1)Set
This command can be used to set the values of the positional parameters on
the command line.
Example:
$ set how do you do
$ echo $1 $2
how do
Here, “how” was assigned to $1 and “do” was assigned to $2 and so on.
2) shift
This command is used to shift the position of the positional parameters. i.e. $2
will be shifted to $1 all the way to the tenth parameter being shifted to $9. Note
that if in case there are more than 9 parameters, this mechanism can be used to
read beyond the 9th.
Example:
$ set hello good morning how do you do welcome to Unix tutorial.
Here, ‘hello’ is assigned to $1, ‘good’ to $2 and so on to ‘to’ being assigned to
$9. Now the shift command can be used to shift the parameters ‘N’ places.
Mrs.Harsha V Patil, MIT ACSC Alandi , Pune
Example:
$ shift 2
$ echo $1
Now $1 will be ‘morning’ and so on to $8 being ‘unix’ and $9 being ‘tutorial’.
Special Parameters $* and $@
 There are special parameters that allow accessing all the command-line
arguments at once. $* and $@ both will act the same unless they are enclosed
in double quotes, "".
 Both the parameters specify the command-line arguments. However, the "$*"
special parameter takes the entire list as one argument with spaces between
and the "$@" special parameter takes the entire list and separates it into
separate arguments.
 We can write the shell script to process an unknown number of command line
arguments with either the $* or $@ special parameters −
Mrs.Harsha V Patil, MIT ACSC Alandi , Pune
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.
 Some commands return additional exit statuses for particular reasons. For
example, some commands differentiate between kinds of errors and will return
various exit values depending on the specific type of failure.
Mrs.Harsha V Patil, MIT ACSC Alandi , Pune
Thanks !
Mrs.Harsha V Patil, MIT ACSC Alandi , Pune

More Related Content

Similar to Shell Scripting and Programming.pptx

intro unix/linux 03
intro unix/linux 03intro unix/linux 03
intro unix/linux 03duquoi
 
intro unix/linux 08
intro unix/linux 08intro unix/linux 08
intro unix/linux 08duquoi
 
BACKGROUND A shell provides a command-line interface for users. I.docx
BACKGROUND A shell provides a command-line interface for users. I.docxBACKGROUND A shell provides a command-line interface for users. I.docx
BACKGROUND A shell provides a command-line interface for users. I.docxwilcockiris
 
The Korn Shell is the UNIX shell (command execution program, often c.docx
The Korn Shell is the UNIX shell (command execution program, often c.docxThe Korn Shell is the UNIX shell (command execution program, often c.docx
The Korn Shell is the UNIX shell (command execution program, often c.docxSUBHI7
 
basic shell scripting syntex
basic shell scripting syntexbasic shell scripting syntex
basic shell scripting syntexKsd Che
 
Unix Shell Script
Unix Shell ScriptUnix Shell Script
Unix Shell Scriptstudent
 
1 of 9 CSCE 3600 Systems Programming Major Assignm.docx
  1 of 9 CSCE 3600 Systems Programming  Major Assignm.docx  1 of 9 CSCE 3600 Systems Programming  Major Assignm.docx
1 of 9 CSCE 3600 Systems Programming Major Assignm.docxShiraPrater50
 
The Bash Love
The Bash LoveThe Bash Love
The Bash Loveishwon
 
2023comp90024_linux.pdf
2023comp90024_linux.pdf2023comp90024_linux.pdf
2023comp90024_linux.pdfLevLafayette1
 
Unix operating system
Unix operating systemUnix operating system
Unix operating systemmidhunjose4u
 
Introduction to-linux
Introduction to-linuxIntroduction to-linux
Introduction to-linuxkishore1986
 
intro unix/linux 02
intro unix/linux 02intro unix/linux 02
intro unix/linux 02duquoi
 

Similar to Shell Scripting and Programming.pptx (20)

Unix shell scripting tutorial
Unix shell scripting tutorialUnix shell scripting tutorial
Unix shell scripting tutorial
 
intro unix/linux 03
intro unix/linux 03intro unix/linux 03
intro unix/linux 03
 
intro unix/linux 08
intro unix/linux 08intro unix/linux 08
intro unix/linux 08
 
Shell scripting
Shell scriptingShell scripting
Shell scripting
 
Unixscripting
UnixscriptingUnixscripting
Unixscripting
 
BACKGROUND A shell provides a command-line interface for users. I.docx
BACKGROUND A shell provides a command-line interface for users. I.docxBACKGROUND A shell provides a command-line interface for users. I.docx
BACKGROUND A shell provides a command-line interface for users. I.docx
 
The Korn Shell is the UNIX shell (command execution program, often c.docx
The Korn Shell is the UNIX shell (command execution program, often c.docxThe Korn Shell is the UNIX shell (command execution program, often c.docx
The Korn Shell is the UNIX shell (command execution program, often c.docx
 
basic shell scripting syntex
basic shell scripting syntexbasic shell scripting syntex
basic shell scripting syntex
 
Unix Shell Script
Unix Shell ScriptUnix Shell Script
Unix Shell Script
 
1 of 9 CSCE 3600 Systems Programming Major Assignm.docx
  1 of 9 CSCE 3600 Systems Programming  Major Assignm.docx  1 of 9 CSCE 3600 Systems Programming  Major Assignm.docx
1 of 9 CSCE 3600 Systems Programming Major Assignm.docx
 
The Bash Love
The Bash LoveThe Bash Love
The Bash Love
 
2023comp90024_linux.pdf
2023comp90024_linux.pdf2023comp90024_linux.pdf
2023comp90024_linux.pdf
 
Linux
LinuxLinux
Linux
 
60761 linux
60761 linux60761 linux
60761 linux
 
Ch03
Ch03Ch03
Ch03
 
Unix operating system
Unix operating systemUnix operating system
Unix operating system
 
Unix
UnixUnix
Unix
 
Chap06
Chap06Chap06
Chap06
 
Introduction to-linux
Introduction to-linuxIntroduction to-linux
Introduction to-linux
 
intro unix/linux 02
intro unix/linux 02intro unix/linux 02
intro unix/linux 02
 

More from Harsha Patel

Introduction to Reinforcement Learning.pptx
Introduction to Reinforcement Learning.pptxIntroduction to Reinforcement Learning.pptx
Introduction to Reinforcement Learning.pptxHarsha Patel
 
Introduction to Association Rules.pptx
Introduction  to  Association  Rules.pptxIntroduction  to  Association  Rules.pptx
Introduction to Association Rules.pptxHarsha Patel
 
Introduction to Clustering . pptx
Introduction    to     Clustering . pptxIntroduction    to     Clustering . pptx
Introduction to Clustering . pptxHarsha Patel
 
Introduction to Classification . pptx
Introduction  to   Classification . pptxIntroduction  to   Classification . pptx
Introduction to Classification . pptxHarsha Patel
 
Introduction to Regression . pptx
Introduction     to    Regression . pptxIntroduction     to    Regression . pptx
Introduction to Regression . pptxHarsha Patel
 
Intro of Machine Learning Models .pptx
Intro of Machine  Learning  Models .pptxIntro of Machine  Learning  Models .pptx
Intro of Machine Learning Models .pptxHarsha Patel
 
Introduction to Machine Learning.pptx
Introduction  to  Machine  Learning.pptxIntroduction  to  Machine  Learning.pptx
Introduction to Machine Learning.pptxHarsha Patel
 
Unit-V-Introduction to Data Mining.pptx
Unit-V-Introduction to  Data Mining.pptxUnit-V-Introduction to  Data Mining.pptx
Unit-V-Introduction to Data Mining.pptxHarsha Patel
 
Unit-IV-Introduction to Data Warehousing .pptx
Unit-IV-Introduction to Data Warehousing .pptxUnit-IV-Introduction to Data Warehousing .pptx
Unit-IV-Introduction to Data Warehousing .pptxHarsha Patel
 
Unit-III-AI Search Techniques and solution's
Unit-III-AI Search Techniques and solution'sUnit-III-AI Search Techniques and solution's
Unit-III-AI Search Techniques and solution'sHarsha Patel
 
Unit-II-Introduction of Artifiial Intelligence.pptx
Unit-II-Introduction of Artifiial Intelligence.pptxUnit-II-Introduction of Artifiial Intelligence.pptx
Unit-II-Introduction of Artifiial Intelligence.pptxHarsha Patel
 
Unit-I-Introduction to Recent Trends.pptx
Unit-I-Introduction to Recent Trends.pptxUnit-I-Introduction to Recent Trends.pptx
Unit-I-Introduction to Recent Trends.pptxHarsha Patel
 
Using Unix Commands.pptx
Using Unix Commands.pptxUsing Unix Commands.pptx
Using Unix Commands.pptxHarsha Patel
 
Using Vi Editor.pptx
Using Vi Editor.pptxUsing Vi Editor.pptx
Using Vi Editor.pptxHarsha Patel
 
Shell Scripting and Programming.pptx
Shell Scripting and Programming.pptxShell Scripting and Programming.pptx
Shell Scripting and Programming.pptxHarsha Patel
 
Managing Processes in Unix.pptx
Managing Processes in Unix.pptxManaging Processes in Unix.pptx
Managing Processes in Unix.pptxHarsha Patel
 
Introduction to Unix Concets.pptx
Introduction to Unix Concets.pptxIntroduction to Unix Concets.pptx
Introduction to Unix Concets.pptxHarsha Patel
 
Handling Files Under Unix.pptx
Handling Files Under Unix.pptxHandling Files Under Unix.pptx
Handling Files Under Unix.pptxHarsha Patel
 
Introduction to OS.pptx
Introduction to OS.pptxIntroduction to OS.pptx
Introduction to OS.pptxHarsha Patel
 
Using Unix Commands.pptx
Using Unix Commands.pptxUsing Unix Commands.pptx
Using Unix Commands.pptxHarsha Patel
 

More from Harsha Patel (20)

Introduction to Reinforcement Learning.pptx
Introduction to Reinforcement Learning.pptxIntroduction to Reinforcement Learning.pptx
Introduction to Reinforcement Learning.pptx
 
Introduction to Association Rules.pptx
Introduction  to  Association  Rules.pptxIntroduction  to  Association  Rules.pptx
Introduction to Association Rules.pptx
 
Introduction to Clustering . pptx
Introduction    to     Clustering . pptxIntroduction    to     Clustering . pptx
Introduction to Clustering . pptx
 
Introduction to Classification . pptx
Introduction  to   Classification . pptxIntroduction  to   Classification . pptx
Introduction to Classification . pptx
 
Introduction to Regression . pptx
Introduction     to    Regression . pptxIntroduction     to    Regression . pptx
Introduction to Regression . pptx
 
Intro of Machine Learning Models .pptx
Intro of Machine  Learning  Models .pptxIntro of Machine  Learning  Models .pptx
Intro of Machine Learning Models .pptx
 
Introduction to Machine Learning.pptx
Introduction  to  Machine  Learning.pptxIntroduction  to  Machine  Learning.pptx
Introduction to Machine Learning.pptx
 
Unit-V-Introduction to Data Mining.pptx
Unit-V-Introduction to  Data Mining.pptxUnit-V-Introduction to  Data Mining.pptx
Unit-V-Introduction to Data Mining.pptx
 
Unit-IV-Introduction to Data Warehousing .pptx
Unit-IV-Introduction to Data Warehousing .pptxUnit-IV-Introduction to Data Warehousing .pptx
Unit-IV-Introduction to Data Warehousing .pptx
 
Unit-III-AI Search Techniques and solution's
Unit-III-AI Search Techniques and solution'sUnit-III-AI Search Techniques and solution's
Unit-III-AI Search Techniques and solution's
 
Unit-II-Introduction of Artifiial Intelligence.pptx
Unit-II-Introduction of Artifiial Intelligence.pptxUnit-II-Introduction of Artifiial Intelligence.pptx
Unit-II-Introduction of Artifiial Intelligence.pptx
 
Unit-I-Introduction to Recent Trends.pptx
Unit-I-Introduction to Recent Trends.pptxUnit-I-Introduction to Recent Trends.pptx
Unit-I-Introduction to Recent Trends.pptx
 
Using Unix Commands.pptx
Using Unix Commands.pptxUsing Unix Commands.pptx
Using Unix Commands.pptx
 
Using Vi Editor.pptx
Using Vi Editor.pptxUsing Vi Editor.pptx
Using Vi Editor.pptx
 
Shell Scripting and Programming.pptx
Shell Scripting and Programming.pptxShell Scripting and Programming.pptx
Shell Scripting and Programming.pptx
 
Managing Processes in Unix.pptx
Managing Processes in Unix.pptxManaging Processes in Unix.pptx
Managing Processes in Unix.pptx
 
Introduction to Unix Concets.pptx
Introduction to Unix Concets.pptxIntroduction to Unix Concets.pptx
Introduction to Unix Concets.pptx
 
Handling Files Under Unix.pptx
Handling Files Under Unix.pptxHandling Files Under Unix.pptx
Handling Files Under Unix.pptx
 
Introduction to OS.pptx
Introduction to OS.pptxIntroduction to OS.pptx
Introduction to OS.pptx
 
Using Unix Commands.pptx
Using Unix Commands.pptxUsing Unix Commands.pptx
Using Unix Commands.pptx
 

Recently uploaded

Concept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfConcept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfUmakantAnnand
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17Celine George
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting DataJhengPantaleon
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxRoyAbrique
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesFatimaKhan178732
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentInMediaRes1
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxOH TEIK BIN
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsanshu789521
 
Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsKarinaGenton
 

Recently uploaded (20)

Concept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfConcept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.Compdf
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17
 
Staff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSDStaff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSD
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and Actinides
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media Component
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptx
 
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha elections
 
Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its Characteristics
 

Shell Scripting and Programming.pptx

  • 1. Using Shells and Shell Programming Mrs.Harsha V Patil, MIT ACSC Alandi , Pune
  • 2. Shell Programming : The shell provides you with an interface to the UNIX system. It gathers input from you and executes programs based on that input. When a program finishes executing, it displays that program's output. A shell is an environment in which we can run our commands, programs, and shell scripts. There are different flavors of shells, just as there are different flavors of operating systems. Each flavor of shell has its own set of recognized commands and functions. Shell Prompt:  The prompt, which is called command prompt, is issued by the shell. While the prompt is displayed, you can type a command . The shell reads your input after you press Enter. It determines the command you want executed by looking at the first word of your input. A word is an unbroken set of characters. Spaces and tabs separate words.  Following is a simple example of date command which displays current date and time: $date Thu Jun 2508:30:19 MST 2009 Mrs.Harsha V Patil, MIT ACSC Alandi , Pune
  • 3. Shell Types: In UNIX there are two major types of shells: 1. The Bourne shell. If you are using a Bourne-type shell, the default prompt is the $ character. 2. 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) Mrs.Harsha V Patil, MIT ACSC Alandi , Pune
  • 4. The different C-type shells follow: • C shell ( csh) • TENEX/TOPS C shell ( tcsh)  The original UNIX shell was written in the mid-1970s by Stephen R. Bourne while he was at AT&T Bell Labs in New Jersey.  The Bourne shell was the first shell to appear on UNIX systems, thus it is referred to as "the shell".  The Bourne shell is usually installed as /bin/sh on most versions of UNIX. For this reason, it is the shell of choice for writing scripts to use on several different versions of UNIX. Mrs.Harsha V Patil, MIT ACSC Alandi , Pune
  • 5. Shell Interpretive Cycle 1. Shell issues the prompt and waits for you (user) to enter a command 2. After a command is entered, the shell scans command line for meta-characters and expands abbreviations to recreate simplified commands. 3. It then passes on the command line to kernel for execution 4. The shell waits for the command to complete and normally can't do anything while the command is running. 5. After the command gets executed the prompt reappear and the whole cycle is repeated again. Mrs.Harsha V Patil, MIT ACSC Alandi , Pune
  • 6. Wild cards and file name generation. When you have a number of files named in series (for example, chap1 to chap12) or filenames with common characters (like aegis, aeon, and aerie), you can use wildcards (also called metacharacters) to specify many files at once. These special characters are * (asterisk), ? (question mark), and [ ] (square brackets). When used in a filename given as an argument to a command: 1. * An asterisk is replaced by any number of characters in a filename. For example, ae* would match aegis, aerie, aeon, etc. if those files were in the same directory. You can use this to save typing for a single filename (for example, al* for alp 2.? A question mark is replaced by any single character (so h?p matches hop and hip, but not help). 3. [] Square brackets can surround a choice of characters you’d like to match. Any one of the characters between the brackets will be matched. For example, [Cc]hapter would match either Chapter or chapter, but [ch]apter would match either capter or hapter. Use a hyphen (-) to separate a range of consecutive characters. For example, chap[13] would match chap1, chap2, or chap3. Mrs.Harsha V Patil, MIT ACSC Alandi , Pune
  • 7. Three Standard Files and Redirection: Once a command begins running, it has access to three files: 1.Standard Input-It reads from its standard input file. By default, standard input is the keyboard. 2.Standard Output-It writes to its standard output file. If you invoke a shell command from the shell, a C program, or a REXX program invoked from TSO READY, standard output is directed to your terminal screen by default.If you invoke a shell command, REXX program, or C program from the ISPF shell, standard output cannot be directed to your terminal screen. You can specify a z/OS UNIX file or use the default, a temporary file. 3.Standard Error-It writes error messages to its standard error file. If you invoke a shell command from the shell or from a C program or from a REXX program invoked from TSO READY, standard error is directed to your terminal screen by default. If you invoke a shell command, REXX program, or C program from the ISPF shell, standard error cannot be directed to your terminal screen. You can specify a z/OS UNIX file or use the default, a temporary file.If the standard output or standard error file contains any data when the command completes, the file is displayed for you to browse. Mrs.Harsha V Patil, MIT ACSC Alandi , Pune
  • 8. Types of Redirection: 1.Overwrite Redirection: Overwrite redirection is useful when you want to store/save the output of a command to a file and replace all the existing content of that file. for example, if you run a command that gives a report, and you want to save the report to the existing file of the previous report you can use overwrite redirection to do this. “>” standard output “<” standard input 2. Append Redirection: With the help of this Redirection, you can append the output to the file without compromising the existing data of the file. “>>” standard output “<<” standard input Mrs.Harsha V Patil, MIT ACSC Alandi , Pune
  • 9. 3. Merge Redirection: This allows you to redirect the output of a command or a program to a specific file descriptor instead of standard output. the syntax for using this is “>&” operator followed by the file descriptor number.  “p >& q” Merges output from stream p with stream q  “p <& q” Merges input from stream p with stream q Connecting commands: Pipe. A pipe is a form of redirection (transfer of standard output to some other destination) that is used in Linux and other Unix-like operating systems to send the output of one command/program/process to another command/program/process for further processing. The Unix/Linux systems allow stdout of a command to be connected to stdin of another command. You can make it do so by using the pipe character ‘|’. Pipe is used to combine two or more commands, and in this, the output of one command acts as input to another command, and this command’s output may act as input to the next command and so on. Mrs.Harsha V Patil, MIT ACSC Alandi , Pune
  • 10.  It can also be visualized as a temporary connection between two or more commands/ programs/ processes. The command line programs that do the further processing are referred to as filters.  This direct connection between commands/ programs/ processes allows them to operate simultaneously and permits data to be transferred between them continuously rather than having to pass it through temporary text files or through the display screen.  Pipes are unidirectional i.e data flows from left to right through the pipeline.  Syntax : command_1 | command_2 | command_3 | .... | command_N  Example : 1. Listing all files and directories and give it as input to more command.  $ ls -l | more Mrs.Harsha V Patil, MIT ACSC Alandi , Pune
  • 11. 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.  There are conditional tests, such as value A is greater than value B, loops allowing us to go through massive amounts of data, files to read and store data, and variables to read and store data, and the script may include functions.  Shell scripts and functions are both interpreted. This means they are not compiled.  It supports less features. It supports input and output redirection operators. Example Script:  Assume we create a test.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 Mrs.Harsha V Patil, MIT ACSC Alandi , Pune
  • 12. 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.  To create a script containing these commands, you put the shebang line first and then add the commands: #!/bin/bash pwd ls Shell Comments: You can put your comments in your script as follows: #!/bin/bash # this is sscasc # Copyright (c) sscasc.com # Script follows here: pwd ls Mrs.Harsha V Patil, MIT ACSC Alandi , Pune
  • 13. Now you save the above content and make this script executable as follows: $chmod +x test.sh Now you have your shell script ready to be executed as follows: $./test.sh Extended Shell Scripts:  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. echo "What is your name?" read PERSON echo "Hello,$PERSON" Mrs.Harsha V Patil, MIT ACSC Alandi , Pune
  • 14. Job ID Versus Process ID  Background and suspended processes are usually manipulated via job number (job ID). This number is different from the process ID and is used because it is shorter.  In addition, a job can consist of multiple processes running in a series or at the same time, in parallel. Using the job ID is easier than tracking individual processes. Nice command:-  It is used to change or set the priority of a process  syntax: $nice-value cat filename  The default priority of a process in unix is 20  The value range from 0 to 39, in linux-9 to 20.where 0 is high and 39 is lower value.  the default value of reduction is 10.  The priority of a process can be increased only by administrator using double minus(--).  eg:-$nice--15catlast.txt  The priority of a process can be made lower using the nice command. Mrs.Harsha V Patil, MIT ACSC Alandi , Pune
  • 15. Here is sample run of the script:  $./test.sh  What is your  name? vvfgc  Hello, vvfgc Variables:  Variable is value that always changes during execution of a program. It is an integral part of shell programming. They provide the ability to store and manipulate information.  There are 2 types of variables. They are • Environment variables • User defined variables Mrs.Harsha V Patil, MIT ACSC Alandi , Pune
  • 16. Environment variables: These variables are the part of the system and these are created and maintained by the system itself. These variables always in capital letters only. Variable meaning  PS1- this is first prompt setting in Unix ($)  PS2- this is second prompt setting in Unix (>)  PATH- whether we are used absolute or relative path.  HOME- it stores the current root directory.  LOGNAME-it stores the login name of the user. User defined variable: Variables are defined as follows:: variable_name = variable_value Mrs.Harsha V Patil, MIT ACSC Alandi , Pune
  • 17. For example: NAME = "sscasc" Above example defines the variable NAME and assigns it the value "sscasc". Variables of this type are called scalar variables. A scalar variable can hold only one value at a time. The shell enables you to store any value you want in a variable. For example: VAR1="ssczsc " VAR2=100 Accessing Values to variables: 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: NAME="vvfgc " echo $NAME This would produce following value: Output: vvfgc Mrs.Harsha V Patil, MIT ACSC Alandi , Pune
  • 18. Read-only Variables:  The 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, following script would give error while trying to change the value of NAME: NAME="vvfgc “ readonly NAME NAME="Qadiri“ This would produce following result: /bin/sh: NAME: This variable is read only. 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. Mrs.Harsha V Patil, MIT ACSC Alandi , Pune
  • 19. Following is the syntax to unset a defined variable using the unset command: unset variable_name Above command would unset the value of a defined variable. Here is a simple example NAME="vvfgc" unset NAME echo $NAME Above example would not print anything. You cannot use the unset command to unset variables that are marked readonly. Mrs.Harsha V Patil, MIT ACSC Alandi , Pune
  • 20. Read command : This command is used to take the input from the user. Syntax: $read var1 var2 var3 ….. var n Syntax: $ read var1 The variable used along with the read command need not be preceded by the $ symbol. Ex: clear echo “enter ur name” read name echo “hello $name” Output: enter ur name :vvfgc Hello vvfgc Mrs.Harsha V Patil, MIT ACSC Alandi , Pune
  • 21. Expr command: This command is used to perform mathematical caluculations. Syntax: ` expr operand1 operator operand2 ` Where ( ` ) this symbol is known as grep symbol. There is always must a space between symbol and the expr command. Ex: clear echo “enter 2 numbers” read a b echo “ sum of 2 numbers is ` expr $a + $b ` “ echo “ sub of 2 numbers is ` expr $a - $b ` “ echo “ product of 2 numbers is ` expr $a * $b ` “ echo “ quotient of 2 numbers is ` expr $a /$b ` “ Note: In unix multiplication purpose we use the symbol of “*” because only * is wild card character. Mrs.Harsha V Patil, MIT ACSC Alandi , Pune
  • 22. Test operator [numerical test]  Arithmetic Operators:  There are following arithmetic operators supported by Bourne Shell.  Assume variable “a” holds 10 and variable “b” holds 20 then:  Show Examples Mrs.Harsha V Patil, MIT ACSC Alandi , Pune
  • 23. Operator Description Example + Addition - Adds values on either side of the operator `expr $a + $b` will give 30 - Subtraction - Subtracts right hand operand from left hand operand `expr $a - $b` will give 10 * Multiplication - Multiplies values on either side of the operator `expr $a * $b` will give 200/ / Division - Divides left hand operand by right operand `expr $b / $a` will give 2 % Modulus - Divides left hand operand by right hand operand and returns remainder `expr $b % $a` will give 0 =Assignment - Assign right operand in left a=$b would assign value of b into a operand = = Equality - Compares two numbers, if both are [ $a == $b ] would return same then returns true. false. !=Not Equality - Compares two numbers, if both [ $a != $b ] would return are different then returns true. true. Mrs.Harsha V Patil, MIT ACSC Alandi , Pune
  • 24.  It is very important to note here that all the conditional expressions would be put inside  square braces with one spaces around them, for example [ $a == $b ] is correct where as  [$a==$b] is incorrect. All the arithmetical calculations are done using long integers. Relational Operators:  Bourne Shell supports following relational operators which are specific to numeric values.  These operators would not work for string values unless their value is numeric.  For example, following operators would work to check a relation between 10 and 20 as well as in between "10" and "20" but not in between "ten" and "twenty".  Assume variable “a” holds 10 and variable “b” holds 20 then: Mrs.Harsha V Patil, MIT ACSC Alandi , Pune
  • 25. Operator Description Example: -eq Checks if the value of two operands is equal or not, if yes then condition becomes true. [ $a -eq $b ] is not true. -ne Checks if the value of two operands is equal or not, if values are not equal then condition becomes true [ $a -ne $b ] is true. -gt Checks if the value of left operand is greater than the value of right operand, if yes then condition becomes true. [ $a -gt $b ] is not true. -lt Checks if the value of left operand is less than the value of right operand, if yes then condition becomes true.[ $a -lt $b ] is true. -ge Checks if the value of left operand is greater than or equal to the value of right operand, if yes then condition becomes true.[ $a -ge $b ] is not true. -le Checks if the value of left operand is less than or equal to the value of right operand, if yes then condition becomes true [ $a -le $b ] is true.  Note here that all the conditional expressions would be put inside square braces with one spaces around them, for example [ $a <= $b ] is correct where as [$a <= $b] is incorrect. Mrs.Harsha V Patil, MIT ACSC Alandi , Pune
  • 26.  Boolean Operators (or) logical operators:  There are following Boolean operators supported by Bourne Shell.  Assume variable “a” holds 10 and variable “b” holds 20 then:  Show Examples Mrs.Harsha V Patil, MIT ACSC Alandi , Pune
  • 27. Operator Description Example ! This is logical negation. This inverts a true condition into false and vice versa.[ ! false ] is true. -o This is logical OR. If one of the operands is true then condition would be true.[ $a -lt 20 - $b -gt 100 ] is true. -a This is logical AND. If both the operands are true then condition would be true otherwise it would be false.[ $a -lt 20 -a $b -gt 100 ] is false. String test Operators There are following string operators supported by Bourne Shell. Assume variable a holds "abc" and variable b holds "efg" then: Show Examples Mrs.Harsha V Patil, MIT ACSC Alandi , Pune
  • 28. Operator Description Example = Checks if the value of two operands is equal or not, if yes then condition becomes true. [ $a = $b ] is not true. != Checks if the value of two operands is equal or not, if values are not equal then condition becomes true.[ $a != $b ] is true. -z Checks if the given string operand size is zero. [ -z $a ] is not true. If it is zero length then it returns true. -n Checks if the given string operand size is non- zero. If it is non-zero length then it returns true. [ -n $a ] is true. Check if str is not the empty string. If it is str empty then it returns false.[ $a ] is not false. Mrs.Harsha V Patil, MIT ACSC Alandi , Pune
  • 29. File Test Operators There are following operators to test various properties associated with a Unix file. Assume a variable file holds an existing file name "test" whose size is 100 bytes and has read, write and execute permission on: Show Examples Operator Description Example b file Checks if file is a block special file if yes then condition becomes true. [ -b $file ] is false. -c file Checks if file is a character special file if yes then condition becomes true.[ -b $file ] is false. -d file Check if file is a directory if yes then condition becomes true.[ -d $file ] is not true. -f file Check if file is an ordinary file as opposed to a directory or special file if yes then condition becomes true. [ -f $file ] is true. Mrs.Harsha V Patil, MIT ACSC Alandi , Pune
  • 30. -g file Checks if file has its set group ID (SGID) bit set if yes then condition becomes true. [ -g $file ] is false. -k file Checks if file has its sticky bit set if yes then condition becomes true. [ -k $file ] is false. -p file Checks if file is a named pipe if yes then condition becomes true.[ -p $file ] is false. -t file Checks if file descriptor is open and associated with a terminal if yes then condition becomes true.[ -t $file ]is false. -u file Checks if file has its set user id (SUID) bit set if yes then condition becomes true.[ -u $file ] is false. -r file Checks if file is readable if yes then condition becomes true.[ -r $file ] is true. -w file Check if file is writable if yes then condition becomes true.[ -w $file ] is true. -x file Check if file is execute if yes then condition becomes true.[ -x $file ] is true. -s file Check if file has size greater than 0 if yes then condition becomes true.[ -s $file ] is true. -e file Check if file exists. Is true even if file is a directory but exists.[ -e $file ] is true. Mrs.Harsha V Patil, MIT ACSC Alandi , Pune
  • 31. Control statements The ability to control the flow of execution of program is known as control statements . The different types of control structures are  Sequence control structure  Selection control structure  Looping control structure Selection control structure This type of instruction allows the shell script to be executed depending on the condition. There are mainly 4 types of decision making instructions. They are  if-then-fi statement  if-then-else-fi statement  if-then-elif-then-else-fi statement case-esac statement if-then-fi statement: in this first check the condition. That condition is true then only the if block statements will be executed otherwise the cursor transfer outside the if condition. Mrs.Harsha V Patil, MIT ACSC Alandi , Pune
  • 32. Syntax: ex: if [ condition ] if [ $a –gt 18 ] then then statements echo “eligible for vote” fi fi if-then-else-fi statement: in this first check the condition. That condition is true then only the if block statements will be executed otherwise the else block statements will be executed. Syntax: ex: if [ condition ] if [ $a –gt $b ] then then statements echo “a is larger than b” else else statements echo “b is larger than a” fi fi Mrs.Harsha V Patil, MIT ACSC Alandi , Pune
  • 33. if-then-elif-then-else-fi statement: in this first check the condition. That condition is true then only the if block statements will be executed otherwise the cursor checks the next condition then the second condition will be true then inside that statements will be executed and so on. If any conditions were not true then the else block statements will be executed. Syntax: ex: if [ condition 1 ] if [ $a –gt $b –a $a –gt $c] then then statements echo ”a is larger” elif [ condition 2] elif [ $b –gt $c ] then then statements echo “ b is larger” else else statements echo “c is larger” fi fi Mrs.Harsha V Patil, MIT ACSC Alandi , Pune
  • 34. Case-esac statement: Syntax: case $variable in [match1] )statements ; ; [match2] statements ; ; [match3] statements ; ; : : : : *) statements ; ; esac here match1,match2 etc are the case labels. • When a case statement is evaluated the value of variable is matched in any one of the choices. • When a match is found then shell executes that corresponding match statements. • The pair of semicolon at the end of every choices. It identifies break. • *) indicates default class. Ex: clear echo “enter a character” Mrs.Harsha V Patil, MIT ACSC Alandi , Pune
  • 35. read ch case $ch in [a-z]) echo “entered character is lowercase letters” ; ; [A-Z] echo “entered character is uppercase letters” ; ; [0-9] echo “entered character isdigit” ; ; *) echo “invalid choice” ;; esac Looping Control statements In this all statements are executed repeatedly again and again as long as condition is true. This is also known as repetition or iteration. Shell allows various types of looping. They are • While loop • Until loop • For loop Mrs.Harsha V Patil, MIT ACSC Alandi , Pune
  • 36. While loop:  This is the pretested loop or entry controlled loop. In this first check the condition, if that condition was true then control enter inside the loop otherwise control transferred outside the loop. Syntax: ex: while [ condition ] while [ i –le 10 ] do do Statements echo “$i” done i =` expr $i + 1 ` done Mrs.Harsha V Patil, MIT ACSC Alandi , Pune
  • 37. until loop:  This is also pretested loop or entry controlled loop. In this first check the condition, if that condition was false then control enter inside the loop otherwise control transferred outside the loop. Syntax: ex: until [ condition ] until [ i -ge 10 ] do do Statements echo “$i” done i =`expr $i + 1 ` done Mrs.Harsha V Patil, MIT ACSC Alandi , Pune
  • 38. for loop: This is a fixed execution loop. This loop is allow to execute list of statements certain period of time. Syntax: for variable in value1 value2 value3….. value n do statements done ex: for i in 1 2 3 4 5 do echo $i i=` expr $i + 1 ` done Mrs.Harsha V Patil, MIT ACSC Alandi , Pune
  • 39. Command line arguments.  The Unix shell is used to run commands, and it allows users to pass run time arguments to these commands.  These arguments, also known as command line parameters, that allows the users to either control the flow of the command or to specify the input data for the command.  While running a command, the user can pass a variable number of parameters in the command line.  Within the command script, the passed parameters are accessible using ‘positional parameters’. These range from $0 to $9, where $0 refers to the name of the command itself, and $1 to $9 are the first through to the ninth parameter, depending on how many parameters were actually passed. Example: $ sh hello how to do you do Here $0 would be assigned sh $1 would be assigned hello $2 would be assigned how And so on … Mrs.Harsha V Patil, MIT ACSC Alandi , Pune
  • 40. Some additional commands to process these parameters. 1)Set This command can be used to set the values of the positional parameters on the command line. Example: $ set how do you do $ echo $1 $2 how do Here, “how” was assigned to $1 and “do” was assigned to $2 and so on. 2) shift This command is used to shift the position of the positional parameters. i.e. $2 will be shifted to $1 all the way to the tenth parameter being shifted to $9. Note that if in case there are more than 9 parameters, this mechanism can be used to read beyond the 9th. Example: $ set hello good morning how do you do welcome to Unix tutorial. Here, ‘hello’ is assigned to $1, ‘good’ to $2 and so on to ‘to’ being assigned to $9. Now the shift command can be used to shift the parameters ‘N’ places. Mrs.Harsha V Patil, MIT ACSC Alandi , Pune
  • 41. Example: $ shift 2 $ echo $1 Now $1 will be ‘morning’ and so on to $8 being ‘unix’ and $9 being ‘tutorial’. Special Parameters $* and $@  There are special parameters that allow accessing all the command-line arguments at once. $* and $@ both will act the same unless they are enclosed in double quotes, "".  Both the parameters specify the command-line arguments. However, the "$*" special parameter takes the entire list as one argument with spaces between and the "$@" special parameter takes the entire list and separates it into separate arguments.  We can write the shell script to process an unknown number of command line arguments with either the $* or $@ special parameters − Mrs.Harsha V Patil, MIT ACSC Alandi , Pune
  • 42. 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.  Some commands return additional exit statuses for particular reasons. For example, some commands differentiate between kinds of errors and will return various exit values depending on the specific type of failure. Mrs.Harsha V Patil, MIT ACSC Alandi , Pune
  • 43. Thanks ! Mrs.Harsha V Patil, MIT ACSC Alandi , Pune