1 4 sp


Published on

  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

1 4 sp

  2. 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 entireprocedure. 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. Shellitself interprets the commands and execute•The commands like dfspace and shutdown are examplesfor ss•Atleast 280 ss come with UNIX OS
  3. 3. •The ss written for one shell may not work withthe other shell because different shell usedifferent mechanisms for executing thecommands and keywords may change also•Ss written for bourne shell are likely to work withkorn shell reverse may not be true because k shellsupports arrays, command aliasing etc•C shell programming language resemble the C-language and it is quite different from Bourneshell•Basic ss will run under both
  4. 4. When to use shell scriptsShell 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. 5. When not to use shell scriptsWhen the task :• is too complex such as writing entire billingsystem•Require a high degree of efficiency•Requires a variety of software tools
  6. 6. Shell script structure# comments about scriptWrite commands in shell scriptExecution$sh ss1•When we execute commands at $ they are executed in the shellthat was invoked when we logged in unless otherwise specified•When we execute shell script the login shell creates a new shell, anew command interpreter and waits ideally in background whilethe new shell executes shell script•When all the commands in ss have been completed the new shellterminates and our login shell once again takes over the control
  7. 7. Interactive Shell script•To read input - read•To display output - echo#ss2#usage sh ss2echo what is your branch?read branchecho hello $branch studentsOutput:what is your branch?Computer sciencehello Computer science students
  8. 8. Shell variables•Provide the ability to store and manipulateinformation with in the shell script•We create and destroy any number of variablesas 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. 9. Shell keywords•These are words whose meaning has alreadybeen explained to shell•We cannot used as var names•Also called as reserved wordsecho if until trapread else case waitset fi esac evalunset while break execshift do continue ulimitexport done exit umaskreadonly for return
  10. 10. Assigning a value to Shell variable•Use assignment operator (=)•Eg name=lakshmi avg=10•While assigning values there should not be spaces eitherside of the =•If you leave space the shell will try to interpret valuebeing assigned as a command to be executed•While assignment if the variable is not there it will createotherwise it will over write the contents•2 types •System variables •User defined variables
  11. 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 preferenceand customize the system environment
  12. 12. variable meaningPS1 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 fileHOME default working dir of the userLOGNAME login name of the userMAIL define the file with path where the mail of the user is storedMAILCHECK period of check for mails of userSHELL name of the default shellTERM name of the terminal on which you are workingWe can see all the system variables and their values using $set
  13. 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 themoment 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 numberof lines in file1 only
  14. 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. 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. 16. Positional parameters•We can convey the information to a program usingcommand line arguments•Shell uses positional parameters concept to know aboutcommand 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 thepositional parameters $0 assigned ss $1 assigned this $2 assigned class $3 assigned is $4 assigned mtech(cs)
  17. 17. •Write a shell script to copy one file contents to other filepass filenames as command line arguments#ss#usage sh ss <sf> <tf>cp $1 $2cat $2
  18. 18. Setting values of Positional parameters•We can’t assign values to positional parameters directly1)$set hello cs students2)$set `cat f1` //f1 is a file•If quoting meta characters are used the command givenwith in the `` (reverse quotes) is replaced by the output#ss#usage sh ss <filename>name=$1set `who am i`mv $name $name.$1
  19. 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