SlideShare una empresa de Scribd logo
1 de 30
Shell Programming & Scripting Languages
Dr.K.Sasidhar
UNIT – III Contents
 Unit III : Working with the Bash Shell
 Introduction, Shell responsibilities, pipes and input
redirection, output redirection, here documents, running a
shell script, shell as a programming language, shell meta
characters, filename substitution, shell variables,
command substitution, shell commands, the environment,
quoting, test command, control structures, arithmetic in
shell, Shell script examples, functions, debugging shell
scripts.
Unit III outcomes
 From the III unit Student can
 Understand and learn the shell basics, meta characters,
responsibilities, variables, control statements, functions and
debugging.
 Write and execute the shell scripts on his /her own.
Shell
 Shell is the command interpreter, interprets the commands
and conveys them to the kernel, which executes them.
 Types of shells:
 Bourne Shell
 C Shell
 Korn Shell
 Bash Shell (Bourne again Shell)
Common
core
Common
core
Bourne shell
Korn shell
C shell
Bourne Again Shell
Shell functions
 Built-in commands
 Scripts
 Redirection
 Wildcards
 pipes
 Variables
 Conditional statements
 Sub commands
 Background Processing ex: sleep 40&
 Command Substitution
Meta Characters
 Characters that are processed by shell for a special
purpose are called Meta Characters.
> output redirection, writes standard output to a file.
>> output redirection, append standard output to a file.
< input redirection, reads standard input from a file
* File substitution wild card, it matches to zero or more
characters.
? File substitution wild card, it matches to any single character.
[…] File substitution wild card, it matches to any single
character between the brackets.
`cmd` command substitution, replaced by the output from
command
Meta Characters
| Pipe symbol, sends the output of one process to the input of
another
; Used to sequence commands
|| Conditional execution; executes a command if the
previous one fails
&& Conditional execution; executes a command if the
previous one succeeds
(...) Group of commands
& Runs a command in the background
# All characters that follow up to a new line are
comment $ Access a variable
Examples with meta characters
 << label input redirection, reads standard input from
script up to label lbl
 To turn off the special meaning of a meta character,
precede it by a  character
 $ echo hi >file
 $ cat file
 hi
 $ echo hi >file hi >file ... not stored to file but displayed at
screen
 $
 OUTPUT REDIRECTION: >, >>
 $ cat >myfile
 Ali Ahmet bas
 ^D
 $ cat myfile
 Ali Ahmet bas
 $ cat >myfile
 Cem Nil
 ^D
Examples with meta characters
Redirection operator >>
 $ cat myfile
 Cem Nil
 $ cat >>myfile
 Canan
 ^D
 $ cat myfile
 Cem Nil Canan
 $
PIPES
 - Shells allow you to use the standard output of one process as the standard
input of another process by connecting the processes together using the
pipe(|) meta character.
 $ command1 | command2
 causes the standard output of command1 to “flow through” to the
standard input of command2.
 - Any number of commands may be connected by pipes.
 A sequence of commands changed together in this way is called a
pipeline.
Pipes examples
 With Pipes large problems can often be solved by a chain of smaller processes, each
performed by a relatively small, reusable utility.
 The standard error channel is not piped through a standard pipeline, although some
shells support this capability.
 in the following example we pipe the output of the ls utility to the input of the wc
utility in order to count the number of files in the current directory.
 $ ls ---> list the current directory.
 a.c b.c cc.c dir1 dir2
 $ ls | wc -w
 5
 $ _
Shell Responsibilities
 1. customizing work environment :
To see the date, welcome message and the list of users
who are logged in we can write a shell script.
 2. Automating daily tasks :
To take the back ups every day we can write a shell
script and run it
 3. Automating repetitive tasks :
The repetitive tasks like compiling a C program, linking it
with libraries and executing can be assigned to a shell
script. Producing sales reports every month.
 4. execute important system procedures
like shutdown, formatting the disk etc..
 5. performing same operation on many files.
 to take the printouts of files in a directory.
SHELL PROGRAMS: SCRIPTS
• Any series of shell commands stored inside a regular text file for later
execution.
• A file that contains shell commands is called a script.
• Before you can run a script, you must give it execute permission by using the
chmod utility.
• To run it, sh type its name with .sh extention.
• Scripts are useful for storing commonly used sequences of commands, and they
range in complexity from simple one-liners to fully blown programs.
• When a script is run, the system determines which shell the script was written
for and then executes the shell using the script as its standard input.
- Every shell contains two data areas:
an environment space and a local-variable space.
A child shell inherits a copy of its parent’s environment space
and a clean local-variable space:
Parent shell
Child shell
Environment
Environment Copied from parent
Local
Local Clean, initialized
VARIABLES
• A shell supports two kinds of variables:
local and environment variables.
• Both kinds of variables hold data in a string format.
• The child shell gets a copy of its parent shell’s environment
variables, but not its local variables.
• Environment variables are therefore used for transmitting useful
information between parent shells and their children.
• Every shell has a set of predefined environment variables that
are
usually initialized by the startup files.
Prof. Andrzej (AJ) Bieszczad Email: andrzej@csun.edu Phone: 818-677-4954 18
• VARIABLES
- In the following example,
we display the values of some common shell environment
variables:
$ echo HOME = $HOME, PATH=$PATH ---> list two variables.
HOME =/home/glass, PATH=/bin:/usr/bin:/usr/sbin
$ echo MAIL = $MAIL
MAIL=/var/mail/glass
$ echo USER = $USER, SHELL = $SHELL, TERM=$TERM
USER = glass, SHELL = /bin/sh, TERM=vt100
$ _
• VARIABLES
- Here is a list of the predefined environment variables that are
common to all shells:
Name Meaning
$HOME the full pathname of your home directory
$PATH a list of directories to search for commands
$MAIL the full pathname of your mailbox
$USER your username
$SHELL the full pathname of your login shell
$TERM the type of your terminal
• VARIABLES
- The syntax for assigning variables differs between shells,
but the way that you access the variables is the same:
If you precede the name of a variable with a $,
this token sequence is replaced by the shell with the value of
the named variable.
To create a variable, simply assign it a value;
variable does not have to be declared.
- the syntax for assigning a variable in the Bourne, Bash and Korn
shells is as follows:
variableName=value ---> place no spaces around the value
or
variableName=“ value ” ---> here, spacing doesn’t matter.
Prof. Andrzej (AJ) Bieszczad Email: andrzej@csun.edu Phone: 818-677-4954 21
- several common built-in variables that have special meanings:
Name Meaning
$$ The process ID of the shell.
$0 The name of the shell script( if applicable ).
$1..$9 $n refers to the nth command line argument
( if applicable ).
$* A list of all the command-line arguments.
here document or here doc.
 A here document, or here doc, is one way to get text
input into a script without feeding it from a separate file.
  a here document (here-document, heredoc, here-
string or here-script) is a file literal or input stream literal.
It is a section of a source code file that is treated as if it
were a separate file. The term is also used for a form of
multiline string literals that use similar syntax, preserving
line breaks and other white space (including indentation) in
the text.
Shell keywords
 The words already explained to the shell. Also called as
reserved words.
echo if until trap read else case esac wait
set unset fi eval while break exec readonly
do continue ulimit shift done exit umask
export for return
Working with Variables
 To make a variable as a constant
 $ a=20
 $ readonly a
 when the variables are made readonly, the shell
does not allow us to change their values. All such
variables can be listed by entering readonly at the $
prompt
 To erase the variable from the shell memory we have to
use:
 $unset b
echo with backslash characters
 echo “07”  bell
 echo “033[1m This is Bold”
 echo “033[7m This is Bold and Reverse”
Control Statements
 if – then – fi statement
 if - then – else – fi statement
 if - then –elif – else – fi statement
Operators and meaning
Operator Meaning
-gt Greater than
-lt Less than
-ge Greater than or equal to
-le Less than or equal to
-ne Not equal to
-eq Equal to
File Tests
Option Meaning
-s file True if the file exists and has a size > 0
-f file True if the file exists
-d file True if the file exists and it is a directory file
-c file True if the file exists and is a character special file
-b file True if the file exists and is a block special file
-r file True if the file exists and you have read permission
-w file True if the file exists and have write permission
-x file True if the file exists and have execute permission
String tests
Condition Meaning
string1 = string2 True if the strings are equal
string1 != string2 True if the strings are different
-n string True if the length of the string is > 0
-z string True if the length of the string is zero
string Ture if the string is not null
Logical Operators
 -a AND
 -o OR
 ! NOT

Más contenido relacionado

La actualidad más candente

Linux Command Suumary
Linux Command SuumaryLinux Command Suumary
Linux Command Suumarymentorsnet
 
Course 102: Lecture 5: File Handling Internals
Course 102: Lecture 5: File Handling Internals Course 102: Lecture 5: File Handling Internals
Course 102: Lecture 5: File Handling Internals Ahmed El-Arabawy
 
Unix_commands_theory
Unix_commands_theoryUnix_commands_theory
Unix_commands_theoryNiti Patel
 
Unix OS & Commands
Unix OS & CommandsUnix OS & Commands
Unix OS & CommandsMohit Belwal
 
Karkha unix shell scritping
Karkha unix shell scritpingKarkha unix shell scritping
Karkha unix shell scritpingchockit88
 
intro unix/linux 06
intro unix/linux 06intro unix/linux 06
intro unix/linux 06duquoi
 
Unix command line concepts
Unix command line conceptsUnix command line concepts
Unix command line conceptsArtem Nagornyi
 
Unix system programming
Unix system programmingUnix system programming
Unix system programmingSyed Mustafa
 
Unix Linux Commands Presentation 2013
Unix Linux Commands Presentation 2013Unix Linux Commands Presentation 2013
Unix Linux Commands Presentation 2013Wave Digitech
 
Red hat linux essentials
Red hat linux essentialsRed hat linux essentials
Red hat linux essentialsHaitham Raik
 
Unix fundamentals
Unix fundamentalsUnix fundamentals
Unix fundamentalsDima Gomaa
 
Unit 7
Unit 7Unit 7
Unit 7siddr
 
Some basic unix commands
Some basic unix commandsSome basic unix commands
Some basic unix commandsaaj_sarkar06
 
Course 102: Lecture 11: Environment Variables
Course 102: Lecture 11: Environment VariablesCourse 102: Lecture 11: Environment Variables
Course 102: Lecture 11: Environment VariablesAhmed El-Arabawy
 
LINUX
LINUXLINUX
LINUXARJUN
 
Course 102: Lecture 12: Basic Text Handling
Course 102: Lecture 12: Basic Text Handling Course 102: Lecture 12: Basic Text Handling
Course 102: Lecture 12: Basic Text Handling Ahmed El-Arabawy
 

La actualidad más candente (20)

Linux Command Suumary
Linux Command SuumaryLinux Command Suumary
Linux Command Suumary
 
Linux
LinuxLinux
Linux
 
Course 102: Lecture 5: File Handling Internals
Course 102: Lecture 5: File Handling Internals Course 102: Lecture 5: File Handling Internals
Course 102: Lecture 5: File Handling Internals
 
Unix_commands_theory
Unix_commands_theoryUnix_commands_theory
Unix_commands_theory
 
Unix OS & Commands
Unix OS & CommandsUnix OS & Commands
Unix OS & Commands
 
Unix practical file
Unix practical fileUnix practical file
Unix practical file
 
Karkha unix shell scritping
Karkha unix shell scritpingKarkha unix shell scritping
Karkha unix shell scritping
 
intro unix/linux 06
intro unix/linux 06intro unix/linux 06
intro unix/linux 06
 
Unix command line concepts
Unix command line conceptsUnix command line concepts
Unix command line concepts
 
Unix system programming
Unix system programmingUnix system programming
Unix system programming
 
Unix Linux Commands Presentation 2013
Unix Linux Commands Presentation 2013Unix Linux Commands Presentation 2013
Unix Linux Commands Presentation 2013
 
Red hat linux essentials
Red hat linux essentialsRed hat linux essentials
Red hat linux essentials
 
Unix fundamentals
Unix fundamentalsUnix fundamentals
Unix fundamentals
 
Linuxnishustud
LinuxnishustudLinuxnishustud
Linuxnishustud
 
Unit 7
Unit 7Unit 7
Unit 7
 
Some basic unix commands
Some basic unix commandsSome basic unix commands
Some basic unix commands
 
Linux administration classes in mumbai
Linux administration classes in mumbaiLinux administration classes in mumbai
Linux administration classes in mumbai
 
Course 102: Lecture 11: Environment Variables
Course 102: Lecture 11: Environment VariablesCourse 102: Lecture 11: Environment Variables
Course 102: Lecture 11: Environment Variables
 
LINUX
LINUXLINUX
LINUX
 
Course 102: Lecture 12: Basic Text Handling
Course 102: Lecture 12: Basic Text Handling Course 102: Lecture 12: Basic Text Handling
Course 102: Lecture 12: Basic Text Handling
 

Similar a Spsl by sasidhar 3 unit

Bash Shell Scripting
Bash Shell ScriptingBash Shell Scripting
Bash Shell ScriptingRaghu nath
 
Unix And Shell Scripting
Unix And Shell ScriptingUnix And Shell Scripting
Unix And Shell ScriptingJaibeer Malik
 
Basic shell programs assignment 1_solution_manual
Basic shell programs assignment 1_solution_manualBasic shell programs assignment 1_solution_manual
Basic shell programs assignment 1_solution_manualKuntal Bhowmick
 
Using Unix
Using UnixUsing Unix
Using UnixDr.Ravi
 
intro unix/linux 08
intro unix/linux 08intro unix/linux 08
intro unix/linux 08duquoi
 
Bash shell
Bash shellBash shell
Bash shellxylas121
 
Advanced linux chapter ix-shell script
Advanced linux chapter ix-shell scriptAdvanced linux chapter ix-shell script
Advanced linux chapter ix-shell scriptEliezer Moraes
 
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
 
Shell Scripting crash course.pdf
Shell Scripting crash course.pdfShell Scripting crash course.pdf
Shell Scripting crash course.pdfharikrishnapolaki
 
Shell Scripting Tutorial | Edureka
Shell Scripting Tutorial | EdurekaShell Scripting Tutorial | Edureka
Shell Scripting Tutorial | EdurekaEdureka!
 
Linux shell env
Linux shell envLinux shell env
Linux shell envRahul Pola
 

Similar a Spsl by sasidhar 3 unit (20)

SHELL PROGRAMMING
SHELL PROGRAMMINGSHELL PROGRAMMING
SHELL PROGRAMMING
 
Shell Scripting
Shell ScriptingShell Scripting
Shell Scripting
 
Shellscripting
ShellscriptingShellscripting
Shellscripting
 
Shell programming
Shell programmingShell programming
Shell programming
 
Basics of shell programming
Basics of shell programmingBasics of shell programming
Basics of shell programming
 
Basics of shell programming
Basics of shell programmingBasics of shell programming
Basics of shell programming
 
Shell scripting
Shell scriptingShell scripting
Shell scripting
 
Bash Shell Scripting
Bash Shell ScriptingBash Shell Scripting
Bash Shell Scripting
 
Unix And Shell Scripting
Unix And Shell ScriptingUnix And Shell Scripting
Unix And Shell Scripting
 
Basic shell programs assignment 1_solution_manual
Basic shell programs assignment 1_solution_manualBasic shell programs assignment 1_solution_manual
Basic shell programs assignment 1_solution_manual
 
Shell scripting
Shell scriptingShell scripting
Shell scripting
 
Using Unix
Using UnixUsing Unix
Using Unix
 
intro unix/linux 08
intro unix/linux 08intro unix/linux 08
intro unix/linux 08
 
Unix
UnixUnix
Unix
 
Bash shell
Bash shellBash shell
Bash shell
 
Advanced linux chapter ix-shell script
Advanced linux chapter ix-shell scriptAdvanced linux chapter ix-shell script
Advanced linux chapter ix-shell script
 
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
 
Shell Scripting crash course.pdf
Shell Scripting crash course.pdfShell Scripting crash course.pdf
Shell Scripting crash course.pdf
 
Shell Scripting Tutorial | Edureka
Shell Scripting Tutorial | EdurekaShell Scripting Tutorial | Edureka
Shell Scripting Tutorial | Edureka
 
Linux shell env
Linux shell envLinux shell env
Linux shell env
 

Más de Sasidhar Kothuru (20)

Spsl vi unit final
Spsl vi unit finalSpsl vi unit final
Spsl vi unit final
 
Spsl iv unit final
Spsl iv unit  finalSpsl iv unit  final
Spsl iv unit final
 
Spsl v unit - final
Spsl v unit - finalSpsl v unit - final
Spsl v unit - final
 
Spsl iv unit final
Spsl iv unit  finalSpsl iv unit  final
Spsl iv unit final
 
Spsl v unit - final
Spsl v unit - finalSpsl v unit - final
Spsl v unit - final
 
Spsl II unit
Spsl   II unitSpsl   II unit
Spsl II unit
 
Jdbc sasidhar
Jdbc  sasidharJdbc  sasidhar
Jdbc sasidhar
 
Servlets
ServletsServlets
Servlets
 
Servers names
Servers namesServers names
Servers names
 
Servers names
Servers namesServers names
Servers names
 
Web Technologies -- Servlets 4 unit slides
Web Technologies -- Servlets   4 unit slidesWeb Technologies -- Servlets   4 unit slides
Web Technologies -- Servlets 4 unit slides
 
Xml quiz 3 unit
Xml quiz   3 unitXml quiz   3 unit
Xml quiz 3 unit
 
Web technologies quiz questions - unit1,2
Web technologies quiz questions  - unit1,2Web technologies quiz questions  - unit1,2
Web technologies quiz questions - unit1,2
 
Web technologies 4th unit quiz
Web technologies 4th unit quizWeb technologies 4th unit quiz
Web technologies 4th unit quiz
 
Xml quiz 3 unit
Xml quiz   3 unitXml quiz   3 unit
Xml quiz 3 unit
 
Web technologies quiz questions - unit1,2
Web technologies quiz questions  - unit1,2Web technologies quiz questions  - unit1,2
Web technologies quiz questions - unit1,2
 
Jsp sasidhar
Jsp sasidharJsp sasidhar
Jsp sasidhar
 
Xml sasidhar
Xml  sasidharXml  sasidhar
Xml sasidhar
 
Java script -23jan2015
Java script -23jan2015Java script -23jan2015
Java script -23jan2015
 
HTML By K.Sasidhar
HTML By K.SasidharHTML By K.Sasidhar
HTML By K.Sasidhar
 

Último

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
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactPECB
 
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
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphThiyagu K
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpinRaunakKeshri1
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104misteraugie
 
“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
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionSafetyChain Software
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docxPoojaSen20
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
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
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 

Último (20)

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
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
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
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
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
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpin
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
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
 
“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...
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory Inspection
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docx
 
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
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...
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 

Spsl by sasidhar 3 unit

  • 1. Shell Programming & Scripting Languages Dr.K.Sasidhar
  • 2. UNIT – III Contents  Unit III : Working with the Bash Shell  Introduction, Shell responsibilities, pipes and input redirection, output redirection, here documents, running a shell script, shell as a programming language, shell meta characters, filename substitution, shell variables, command substitution, shell commands, the environment, quoting, test command, control structures, arithmetic in shell, Shell script examples, functions, debugging shell scripts.
  • 3. Unit III outcomes  From the III unit Student can  Understand and learn the shell basics, meta characters, responsibilities, variables, control statements, functions and debugging.  Write and execute the shell scripts on his /her own.
  • 4. Shell  Shell is the command interpreter, interprets the commands and conveys them to the kernel, which executes them.  Types of shells:  Bourne Shell  C Shell  Korn Shell  Bash Shell (Bourne again Shell) Common core Common core Bourne shell Korn shell C shell Bourne Again Shell
  • 5. Shell functions  Built-in commands  Scripts  Redirection  Wildcards  pipes  Variables  Conditional statements  Sub commands  Background Processing ex: sleep 40&  Command Substitution
  • 6. Meta Characters  Characters that are processed by shell for a special purpose are called Meta Characters. > output redirection, writes standard output to a file. >> output redirection, append standard output to a file. < input redirection, reads standard input from a file * File substitution wild card, it matches to zero or more characters. ? File substitution wild card, it matches to any single character. […] File substitution wild card, it matches to any single character between the brackets. `cmd` command substitution, replaced by the output from command
  • 7. Meta Characters | Pipe symbol, sends the output of one process to the input of another ; Used to sequence commands || Conditional execution; executes a command if the previous one fails && Conditional execution; executes a command if the previous one succeeds (...) Group of commands & Runs a command in the background # All characters that follow up to a new line are comment $ Access a variable
  • 8. Examples with meta characters  << label input redirection, reads standard input from script up to label lbl  To turn off the special meaning of a meta character, precede it by a character  $ echo hi >file  $ cat file  hi  $ echo hi >file hi >file ... not stored to file but displayed at screen  $
  • 9.  OUTPUT REDIRECTION: >, >>  $ cat >myfile  Ali Ahmet bas  ^D  $ cat myfile  Ali Ahmet bas  $ cat >myfile  Cem Nil  ^D Examples with meta characters
  • 10. Redirection operator >>  $ cat myfile  Cem Nil  $ cat >>myfile  Canan  ^D  $ cat myfile  Cem Nil Canan  $
  • 11. PIPES  - Shells allow you to use the standard output of one process as the standard input of another process by connecting the processes together using the pipe(|) meta character.  $ command1 | command2  causes the standard output of command1 to “flow through” to the standard input of command2.  - Any number of commands may be connected by pipes.  A sequence of commands changed together in this way is called a pipeline.
  • 12. Pipes examples  With Pipes large problems can often be solved by a chain of smaller processes, each performed by a relatively small, reusable utility.  The standard error channel is not piped through a standard pipeline, although some shells support this capability.  in the following example we pipe the output of the ls utility to the input of the wc utility in order to count the number of files in the current directory.  $ ls ---> list the current directory.  a.c b.c cc.c dir1 dir2  $ ls | wc -w  5  $ _
  • 13. Shell Responsibilities  1. customizing work environment : To see the date, welcome message and the list of users who are logged in we can write a shell script.  2. Automating daily tasks : To take the back ups every day we can write a shell script and run it  3. Automating repetitive tasks : The repetitive tasks like compiling a C program, linking it with libraries and executing can be assigned to a shell script. Producing sales reports every month.
  • 14.  4. execute important system procedures like shutdown, formatting the disk etc..  5. performing same operation on many files.  to take the printouts of files in a directory.
  • 15. SHELL PROGRAMS: SCRIPTS • Any series of shell commands stored inside a regular text file for later execution. • A file that contains shell commands is called a script. • Before you can run a script, you must give it execute permission by using the chmod utility. • To run it, sh type its name with .sh extention. • Scripts are useful for storing commonly used sequences of commands, and they range in complexity from simple one-liners to fully blown programs. • When a script is run, the system determines which shell the script was written for and then executes the shell using the script as its standard input.
  • 16. - Every shell contains two data areas: an environment space and a local-variable space. A child shell inherits a copy of its parent’s environment space and a clean local-variable space: Parent shell Child shell Environment Environment Copied from parent Local Local Clean, initialized
  • 17. VARIABLES • A shell supports two kinds of variables: local and environment variables. • Both kinds of variables hold data in a string format. • The child shell gets a copy of its parent shell’s environment variables, but not its local variables. • Environment variables are therefore used for transmitting useful information between parent shells and their children. • Every shell has a set of predefined environment variables that are usually initialized by the startup files.
  • 18. Prof. Andrzej (AJ) Bieszczad Email: andrzej@csun.edu Phone: 818-677-4954 18 • VARIABLES - In the following example, we display the values of some common shell environment variables: $ echo HOME = $HOME, PATH=$PATH ---> list two variables. HOME =/home/glass, PATH=/bin:/usr/bin:/usr/sbin $ echo MAIL = $MAIL MAIL=/var/mail/glass $ echo USER = $USER, SHELL = $SHELL, TERM=$TERM USER = glass, SHELL = /bin/sh, TERM=vt100 $ _
  • 19. • VARIABLES - Here is a list of the predefined environment variables that are common to all shells: Name Meaning $HOME the full pathname of your home directory $PATH a list of directories to search for commands $MAIL the full pathname of your mailbox $USER your username $SHELL the full pathname of your login shell $TERM the type of your terminal
  • 20. • VARIABLES - The syntax for assigning variables differs between shells, but the way that you access the variables is the same: If you precede the name of a variable with a $, this token sequence is replaced by the shell with the value of the named variable. To create a variable, simply assign it a value; variable does not have to be declared. - the syntax for assigning a variable in the Bourne, Bash and Korn shells is as follows: variableName=value ---> place no spaces around the value or variableName=“ value ” ---> here, spacing doesn’t matter.
  • 21. Prof. Andrzej (AJ) Bieszczad Email: andrzej@csun.edu Phone: 818-677-4954 21 - several common built-in variables that have special meanings: Name Meaning $$ The process ID of the shell. $0 The name of the shell script( if applicable ). $1..$9 $n refers to the nth command line argument ( if applicable ). $* A list of all the command-line arguments.
  • 22. here document or here doc.  A here document, or here doc, is one way to get text input into a script without feeding it from a separate file.   a here document (here-document, heredoc, here- string or here-script) is a file literal or input stream literal. It is a section of a source code file that is treated as if it were a separate file. The term is also used for a form of multiline string literals that use similar syntax, preserving line breaks and other white space (including indentation) in the text.
  • 23. Shell keywords  The words already explained to the shell. Also called as reserved words. echo if until trap read else case esac wait set unset fi eval while break exec readonly do continue ulimit shift done exit umask export for return
  • 24. Working with Variables  To make a variable as a constant  $ a=20  $ readonly a  when the variables are made readonly, the shell does not allow us to change their values. All such variables can be listed by entering readonly at the $ prompt  To erase the variable from the shell memory we have to use:  $unset b
  • 25. echo with backslash characters  echo “07”  bell  echo “033[1m This is Bold”  echo “033[7m This is Bold and Reverse”
  • 26. Control Statements  if – then – fi statement  if - then – else – fi statement  if - then –elif – else – fi statement
  • 27. Operators and meaning Operator Meaning -gt Greater than -lt Less than -ge Greater than or equal to -le Less than or equal to -ne Not equal to -eq Equal to
  • 28. File Tests Option Meaning -s file True if the file exists and has a size > 0 -f file True if the file exists -d file True if the file exists and it is a directory file -c file True if the file exists and is a character special file -b file True if the file exists and is a block special file -r file True if the file exists and you have read permission -w file True if the file exists and have write permission -x file True if the file exists and have execute permission
  • 29. String tests Condition Meaning string1 = string2 True if the strings are equal string1 != string2 True if the strings are different -n string True if the length of the string is > 0 -z string True if the length of the string is zero string Ture if the string is not null
  • 30. Logical Operators  -a AND  -o OR  ! NOT