SlideShare uma empresa Scribd logo
1 de 19
SHELL PROGRAMMING
SHELL PROGRAMMING
•A shell program is nothing but a series of commands
•We give the to-do list – a prg- that carries out an entire
procedure. Such programs are known as “shell scripts”
•We can write shell scripts for simple to full-fledged,
custom-made software applications
•We can exploit full power and versality of UNIX
•To execute ss we don’t need a separate compiler. Shell
itself interprets the commands and execute
•The commands like dfspace and shutdown are examples
for ss
•Atleast 280 ss come with UNIX OS
•The ss written for one shell may not work with
the other shell because different shell use
different mechanisms for executing the
commands and keywords may change also
•Ss written for bourne shell are likely to work with
korn shell reverse may not be true because k shell
supports arrays, command aliasing etc
•C shell programming language resemble the C-
language and it is quite different from Bourne
shell
•Basic ss will run under both
When to use shell scripts
Shell scripts can be used for a variety of tasks
•Customizing your work environment
   •Every time login to see current date, welcome message etc
•Automating your daily tasks
   •To take backup all your programs at the end of the day
•Automating repetitive tasks
   •Producing sales report of every month etc
•Executing important system procedures
   •Shutdown, formatting a disk, creating a file system on it,
   mounting and un mounting the disk etc
•Performing same operation on many files
   •Replacing printf with myprintf in all C programs present in a
   dir etc
When not to use shell scripts
When the task :
• is too complex such as writing entire billing
system
•Require a high degree of efficiency
•Requires a variety of software tools
Shell script structure
# comments about script
Write commands in shell script

Execution
$sh ss1

•When we execute commands at $ they are executed in the shell
that was invoked when we logged in unless otherwise specified
•When we execute shell script the login shell creates a new shell, a
new command interpreter and waits ideally in background while
the new shell executes shell script
•When all the commands in ss have been completed the new shell
terminates and our login shell once again takes over the control
Interactive Shell script
•To read input             -      read
•To display output         -      echo

#ss2
#usage sh ss2
echo what is your branch?
read branch
echo hello $branch students

Output:
what is your branch?
Computer science
hello Computer science students
Shell variables
•Provide the ability to store and manipulate
information with in the shell script
•We create and destroy any number of variables
as needed
•Rules for building shell variables
   •Any combination of alphabets, digits, underscore(_)
   •No commas, blanks are allowed
   •First character should be either alphabet or _
   •No limit on number of characters
   •Case sensitive
•Eg : avg    average etc
Shell keywords
•These are words whose meaning has already
been explained to shell
•We cannot used as var names
•Also called as reserved words
echo        if         until    trap
read        else       case     wait
set         fi         esac     eval
unset       while      break    exec
shift       do         continue ulimit
export      done       exit     umask
readonly for           return
Assigning a value to Shell variable
•Use assignment operator (=)
•Eg    name=lakshmi
       avg=10
•While assigning values there should not be spaces either
side of the =
•If you leave space the shell will try to interpret value
being assigned as a command to be executed
•While assignment if the variable is not there it will create
otherwise it will over write the contents
•2 types
    •System variables
    •User defined variables
System variables
•Standard variables which are always accessible
•The shell provides values for these variables
•Govern the environment
•We can change the values as per our preference
and customize the system environment
variable              meaning
PS1           system prompt1, default value”$”
PS2           system prompt2, default value”>”
PATH          defines the path in which the shell must
              search in order to execute the command or file
HOME          default working dir of the user
LOGNAME       login name of the user
MAIL          define the file with path where the mail of the
              user is stored
MAILCHECK     period of check for mails of user
SHELL         name of the default shell
TERM          name of the terminal on which you are working


We can see all the system variables and their values using
      $set
User defined variables
•All shell variables are string variables
•We can assign more than one word using “”
    •Eg : c=“one two”
$name=“lakshmi”
$echo name is $name
•All the shell variables defined inside ss die at the
moment the execution of ss is over
•Defining null variables
    1.d=“”            2. d=‘’      3. d=
•On echoing null variables blank line appears on screen
•If we use null variables in commands shell ignores it
       eg: $wc –l $var1 file1             //display number
of lines in file1 only
Unchanging variables
•To declare constants we use readonly key word
       $a=20
       $readonly a
•Shell does not allow us to change the value of a
       $readonly          // display all readonly vars
Wiping out variables
•Unset is used to wipe of shell variables
      $unset a
•We cannot wipeout system variables
      $unset PS1          // will not work
Positional parameters
•We can convey the information to a program using
command line arguments
•Shell uses positional parameters concept to know about
command line arguments
•They are 9 in number named $1 to $9
       Eg: $sh ss this class is mtech(cs)
•Each word is automatically stored serially in the
positional parameters
       $0 assigned ss
       $1 assigned this
       $2 assigned class
       $3 assigned is
       $4 assigned mtech(cs)
•Write a shell script to copy one file contents to other file
pass filenames as command line arguments

#ss
#usage sh ss <sf> <tf>
cp $1 $2
cat $2
Setting values of Positional parameters
•We can’t assign values to positional parameters directly
1)$set hello cs students
2)$set `cat f1`      //f1 is a file
•If quoting meta characters are used the command given
with in the `` (reverse quotes) is replaced by the output

#ss
#usage sh ss <filename>
name=$1
set `who am i`
mv $name $name.$1
Using shift on Positional parameters
•   Set command can set pp upto 9
•   If we use more we can’t access after 1 directly
    eg: $set a b c d e f g h I j k l
         $echo $10 $11
       a0 a1
•   To access after 9 use shift
    Eg: $shift 2
•   First 2 words gone and lost forever
•   $echo $*         //will display all pps
•   $echo $#         //total number of pps

Mais conteúdo relacionado

Mais procurados

BASH Guide Summary
BASH Guide SummaryBASH Guide Summary
BASH Guide SummaryOhgyun Ahn
 
Linux shell env
Linux shell envLinux shell env
Linux shell envRahul Pola
 
Pipes and filters
Pipes and filtersPipes and filters
Pipes and filtersbhatvijetha
 
Bash Shell Scripting
Bash Shell ScriptingBash Shell Scripting
Bash Shell ScriptingRaghu nath
 
system management -shell programming by gaurav raikar
system management -shell programming by gaurav raikarsystem management -shell programming by gaurav raikar
system management -shell programming by gaurav raikarGauravRaikar3
 
Shell Scripting in Linux
Shell Scripting in LinuxShell Scripting in Linux
Shell Scripting in LinuxAnu Chaudhry
 
Bash Shell Scripting
Bash Shell ScriptingBash Shell Scripting
Bash Shell ScriptingRaghu nath
 
Bash shell scripting
Bash shell scriptingBash shell scripting
Bash shell scriptingVIKAS TIWARI
 
Quick start bash script
Quick start   bash scriptQuick start   bash script
Quick start bash scriptSimon Su
 
Shellvariables in Linux
Shellvariables in Linux Shellvariables in Linux
Shellvariables in Linux bhatvijetha
 
Shell & Shell Script
Shell & Shell Script Shell & Shell Script
Shell & Shell Script Amit Ghosh
 

Mais procurados (20)

BASH Guide Summary
BASH Guide SummaryBASH Guide Summary
BASH Guide Summary
 
SHELL PROGRAMMING
SHELL PROGRAMMINGSHELL PROGRAMMING
SHELL PROGRAMMING
 
Linux shell env
Linux shell envLinux shell env
Linux shell env
 
Unix shell scripting tutorial
Unix shell scripting tutorialUnix shell scripting tutorial
Unix shell scripting tutorial
 
Pipes and filters
Pipes and filtersPipes and filters
Pipes and filters
 
Bash Shell Scripting
Bash Shell ScriptingBash Shell Scripting
Bash Shell Scripting
 
Shell script-sec
Shell script-secShell script-sec
Shell script-sec
 
Linux shell scripting
Linux shell scriptingLinux shell scripting
Linux shell scripting
 
system management -shell programming by gaurav raikar
system management -shell programming by gaurav raikarsystem management -shell programming by gaurav raikar
system management -shell programming by gaurav raikar
 
Shell Scripting in Linux
Shell Scripting in LinuxShell Scripting in Linux
Shell Scripting in Linux
 
Bash Shell Scripting
Bash Shell ScriptingBash Shell Scripting
Bash Shell Scripting
 
Unix - Filters/Editors
Unix - Filters/EditorsUnix - Filters/Editors
Unix - Filters/Editors
 
Basics of shell programming
Basics of shell programmingBasics of shell programming
Basics of shell programming
 
Bash shell scripting
Bash shell scriptingBash shell scripting
Bash shell scripting
 
Quick start bash script
Quick start   bash scriptQuick start   bash script
Quick start bash script
 
Shellvariables in Linux
Shellvariables in Linux Shellvariables in Linux
Shellvariables in Linux
 
Shell Scripting
Shell ScriptingShell Scripting
Shell Scripting
 
Chap06
Chap06Chap06
Chap06
 
Shell scripting
Shell scriptingShell scripting
Shell scripting
 
Shell & Shell Script
Shell & Shell Script Shell & Shell Script
Shell & Shell Script
 

Semelhante a SHELL PROGRAMMING GUIDE

Variables and User Input
Variables and User InputVariables and User Input
Variables and User Inputprimeteacher32
 
04 using and_configuring_bash
04 using and_configuring_bash04 using and_configuring_bash
04 using and_configuring_bashShay Cohen
 
390aLecture05_12sp.ppt
390aLecture05_12sp.ppt390aLecture05_12sp.ppt
390aLecture05_12sp.pptmugeshmsd5
 
2. UNIX OS System Architecture easy.pptx
2. UNIX OS System Architecture easy.pptx2. UNIX OS System Architecture easy.pptx
2. UNIX OS System Architecture easy.pptxPriyadarshini648418
 
Shell Programming_Module2_Part2.pptx.pdf
Shell Programming_Module2_Part2.pptx.pdfShell Programming_Module2_Part2.pptx.pdf
Shell Programming_Module2_Part2.pptx.pdfHIMANKMISHRA2
 
Course 102: Lecture 10: Learning About the Shell
Course 102: Lecture 10: Learning About the Shell Course 102: Lecture 10: Learning About the Shell
Course 102: Lecture 10: Learning About the Shell Ahmed El-Arabawy
 
shellScriptAlt.pptx
shellScriptAlt.pptxshellScriptAlt.pptx
shellScriptAlt.pptxNiladriDey18
 
Shell Scripting crash course.pdf
Shell Scripting crash course.pdfShell Scripting crash course.pdf
Shell Scripting crash course.pdfharikrishnapolaki
 
Using Unix
Using UnixUsing Unix
Using UnixDr.Ravi
 
Practical approach to perl day1
Practical approach to perl day1Practical approach to perl day1
Practical approach to perl day1Rakesh Mukundan
 
Linux Shell Scripting Craftsmanship
Linux Shell Scripting CraftsmanshipLinux Shell Scripting Craftsmanship
Linux Shell Scripting Craftsmanshipbokonen
 
Shell Scripting and Programming.pptx
Shell Scripting and Programming.pptxShell Scripting and Programming.pptx
Shell Scripting and Programming.pptxHarsha Patel
 

Semelhante a SHELL PROGRAMMING GUIDE (20)

Variables and User Input
Variables and User InputVariables and User Input
Variables and User Input
 
04 using and_configuring_bash
04 using and_configuring_bash04 using and_configuring_bash
04 using and_configuring_bash
 
Basics of shell programming
Basics of shell programmingBasics of shell programming
Basics of shell programming
 
Spsl by sasidhar 3 unit
Spsl by sasidhar  3 unitSpsl by sasidhar  3 unit
Spsl by sasidhar 3 unit
 
Shell scripting
Shell scriptingShell scripting
Shell scripting
 
390aLecture05_12sp.ppt
390aLecture05_12sp.ppt390aLecture05_12sp.ppt
390aLecture05_12sp.ppt
 
2. UNIX OS System Architecture easy.pptx
2. UNIX OS System Architecture easy.pptx2. UNIX OS System Architecture easy.pptx
2. UNIX OS System Architecture easy.pptx
 
Shell Programming_Module2_Part2.pptx.pdf
Shell Programming_Module2_Part2.pptx.pdfShell Programming_Module2_Part2.pptx.pdf
Shell Programming_Module2_Part2.pptx.pdf
 
Course 102: Lecture 10: Learning About the Shell
Course 102: Lecture 10: Learning About the Shell Course 102: Lecture 10: Learning About the Shell
Course 102: Lecture 10: Learning About the Shell
 
shellScriptAlt.pptx
shellScriptAlt.pptxshellScriptAlt.pptx
shellScriptAlt.pptx
 
Shell Scripting crash course.pdf
Shell Scripting crash course.pdfShell Scripting crash course.pdf
Shell Scripting crash course.pdf
 
PPT 19.pptx
PPT 19.pptxPPT 19.pptx
PPT 19.pptx
 
Using Unix
Using UnixUsing Unix
Using Unix
 
Review of C.pptx
Review of C.pptxReview of C.pptx
Review of C.pptx
 
Practical approach to perl day1
Practical approach to perl day1Practical approach to perl day1
Practical approach to perl day1
 
Linux Shell Scripting Craftsmanship
Linux Shell Scripting CraftsmanshipLinux Shell Scripting Craftsmanship
Linux Shell Scripting Craftsmanship
 
Unix
UnixUnix
Unix
 
Scripting and the shell in LINUX
Scripting and the shell in LINUXScripting and the shell in LINUX
Scripting and the shell in LINUX
 
Unixscripting
UnixscriptingUnixscripting
Unixscripting
 
Shell Scripting and Programming.pptx
Shell Scripting and Programming.pptxShell Scripting and Programming.pptx
Shell Scripting and Programming.pptx
 

SHELL PROGRAMMING GUIDE

  • 2. SHELL PROGRAMMING •A shell program is nothing but a series of commands •We give the to-do list – a prg- that carries out an entire procedure. Such programs are known as “shell scripts” •We can write shell scripts for simple to full-fledged, custom-made software applications •We can exploit full power and versality of UNIX •To execute ss we don’t need a separate compiler. Shell itself interprets the commands and execute •The commands like dfspace and shutdown are examples for ss •Atleast 280 ss come with UNIX OS
  • 3. •The ss written for one shell may not work with the other shell because different shell use different mechanisms for executing the commands and keywords may change also •Ss written for bourne shell are likely to work with korn shell reverse may not be true because k shell supports arrays, command aliasing etc •C shell programming language resemble the C- language and it is quite different from Bourne shell •Basic ss will run under both
  • 4. When to use shell scripts Shell scripts can be used for a variety of tasks •Customizing your work environment •Every time login to see current date, welcome message etc •Automating your daily tasks •To take backup all your programs at the end of the day •Automating repetitive tasks •Producing sales report of every month etc •Executing important system procedures •Shutdown, formatting a disk, creating a file system on it, mounting and un mounting the disk etc •Performing same operation on many files •Replacing printf with myprintf in all C programs present in a dir etc
  • 5. When not to use shell scripts When the task : • is too complex such as writing entire billing system •Require a high degree of efficiency •Requires a variety of software tools
  • 6. Shell script structure # comments about script Write commands in shell script Execution $sh ss1 •When we execute commands at $ they are executed in the shell that was invoked when we logged in unless otherwise specified •When we execute shell script the login shell creates a new shell, a new command interpreter and waits ideally in background while the new shell executes shell script •When all the commands in ss have been completed the new shell terminates and our login shell once again takes over the control
  • 7. Interactive Shell script •To read input - read •To display output - echo #ss2 #usage sh ss2 echo what is your branch? read branch echo hello $branch students Output: what is your branch? Computer science hello Computer science students
  • 8. Shell variables •Provide the ability to store and manipulate information with in the shell script •We create and destroy any number of variables as needed •Rules for building shell variables •Any combination of alphabets, digits, underscore(_) •No commas, blanks are allowed •First character should be either alphabet or _ •No limit on number of characters •Case sensitive •Eg : avg average etc
  • 9. Shell keywords •These are words whose meaning has already been explained to shell •We cannot used as var names •Also called as reserved words echo if until trap read else case wait set fi esac eval unset while break exec shift do continue ulimit export done exit umask readonly for return
  • 10. Assigning a value to Shell variable •Use assignment operator (=) •Eg name=lakshmi avg=10 •While assigning values there should not be spaces either side of the = •If you leave space the shell will try to interpret value being assigned as a command to be executed •While assignment if the variable is not there it will create otherwise it will over write the contents •2 types •System variables •User defined variables
  • 11. System variables •Standard variables which are always accessible •The shell provides values for these variables •Govern the environment •We can change the values as per our preference and customize the system environment
  • 12. variable meaning PS1 system prompt1, default value”$” PS2 system prompt2, default value”>” PATH defines the path in which the shell must search in order to execute the command or file HOME default working dir of the user LOGNAME login name of the user MAIL define the file with path where the mail of the user is stored MAILCHECK period of check for mails of user SHELL name of the default shell TERM name of the terminal on which you are working We can see all the system variables and their values using $set
  • 13. User defined variables •All shell variables are string variables •We can assign more than one word using “” •Eg : c=“one two” $name=“lakshmi” $echo name is $name •All the shell variables defined inside ss die at the moment the execution of ss is over •Defining null variables 1.d=“” 2. d=‘’ 3. d= •On echoing null variables blank line appears on screen •If we use null variables in commands shell ignores it eg: $wc –l $var1 file1 //display number of lines in file1 only
  • 14. Unchanging variables •To declare constants we use readonly key word $a=20 $readonly a •Shell does not allow us to change the value of a $readonly // display all readonly vars
  • 15. Wiping out variables •Unset is used to wipe of shell variables $unset a •We cannot wipeout system variables $unset PS1 // will not work
  • 16. Positional parameters •We can convey the information to a program using command line arguments •Shell uses positional parameters concept to know about command line arguments •They are 9 in number named $1 to $9 Eg: $sh ss this class is mtech(cs) •Each word is automatically stored serially in the positional parameters $0 assigned ss $1 assigned this $2 assigned class $3 assigned is $4 assigned mtech(cs)
  • 17. •Write a shell script to copy one file contents to other file pass filenames as command line arguments #ss #usage sh ss <sf> <tf> cp $1 $2 cat $2
  • 18. Setting values of Positional parameters •We can’t assign values to positional parameters directly 1)$set hello cs students 2)$set `cat f1` //f1 is a file •If quoting meta characters are used the command given with in the `` (reverse quotes) is replaced by the output #ss #usage sh ss <filename> name=$1 set `who am i` mv $name $name.$1
  • 19. Using shift on Positional parameters • Set command can set pp upto 9 • If we use more we can’t access after 1 directly eg: $set a b c d e f g h I j k l $echo $10 $11 a0 a1 • To access after 9 use shift Eg: $shift 2 • First 2 words gone and lost forever • $echo $* //will display all pps • $echo $# //total number of pps