NATIONAL INSTITUTE OF TECHNOLOGYJAMSHEDPURComputer Science and Engineering Dept.
SHELL SCRIPTINGbyKUMAR SOURABH241/10
INDEX:• What is Shell• How Shell Works• How Shell Better• Type of Shell• What is Shell Script• Why We Write Shell Script• ...
 Shell is a agency that sits between the user and the UNIXsystem Shell is an command language interpreter that readcomma...
WHAT IS SHELLUSER2
HOW SHELL WORKS?Shell is a agency that sits between the user and the UNIX systemComputer understand the language of 0s an...
HOW SHELL WORKS?OurcommandorShell scriptLinuxShellConverted toBinary languageby ShellNow LinuxKernalUnderstadOurRequest
lsdatetime BASH001010001010101101010010LINUXKERNELExample:
Linux Shell is similar to Windows command promptbut are much more powerful.For example:On Linux it’s quite feasible to hav...
 Sh – simple shellBASH – Bourne Again ShellKSH – Korne ShellCSH – C ShellSSH – Secure Shell To use a particular shell ty...
TYPE OF SHELL-2Shell NameDevelopedbyWhere RemarkBASH ( Bourne-Again SHell )Brian Foxand ChetRameyFreeSoftwareFoundationMos...
 Shell Script is series of command written in plaintext file. Shell script is just like batch file in MS-DOS buthave mor...
Shell script can take input from user, file and outputthem on screen.Useful to create our own commands.Minimizes typing...
HOW TO WRITE SHELL SCRIPT
$ vi first#!/bin.sh## My first shell script#clearecho “Time nd Tide wait 4 no one"
Explanation:Script Command(s) Meaning$ vi firstvi editor## My first program## followed by any text is consideredcommentech...
HOW TO RUN SHELL SCRIPTRun our script asSyntax: ./your-shell-program-nameFor e.g.$ ./first
 In Linux (Shell), there are two types of variable:System variables-: Created and maintained by Linuxitself and defined i...
SYSTEM VARIABLES :System Variable Meaning1. BASH=/bin/bash Our shell name2. HOME=/home/kumar Our home directory3. PWD=/hom...
TEST OPERATORSTest Works in 3 Ways:o Numeric comparisono String comparisono File tests
EXAMPLE OF NUMERIC COMPARISON$ a=5; b=7; c=7.2$test $a –eq $b ; echo $?1$test $a -lt $b ; echo $?0$test $c –gt $b ; echo $...
EXAMPLE OF FILE TESTS$ ls –l emp.txt-rw-rw-rw- …$ [ -f emp.txt ] ; echo $?0$ [ -x emp.txt ] ; echo $?1$ [ -d emp.txt ] || ...
STRING TESTTest True ifS1=s2 String s1=s2S1!=s2 String s1 is not equalto s2-n stg String stg is not nullstring-z stg Strin...
Conditions (Case)case expr inOption1) stmt ;;Option2) stmt ;;*) stmt ;;esacEvery option should be terminated with a double...
ARRAYInitialising an arrayA[0] = 10A[1] = HiUsing an array${A[0]} : retrieves 10Here arrays can contain data belonging to ...
FUNCTIONLocal FunctionWritten at the command promptLasts for the current session aloneGlobal FunctionWritten in .bashrc fi...
ALIASAlias – Alternate name for an entityEntity here refers to commandWe can give another name or alias name for acommand ...
Alias and Unaliasalias newname=oldnameEg alias copy=cpThen we can use copy in the same way we use cpcommandEg copy file1 f...
export : EXPORTING SHELL VARIABLES
Reference:1. A Practical Guide to Ubuntu Linuxby MARK G SOBEL2. Linux Foundation Organisation
Thank you!!
shell scripting
shell scripting
Upcoming SlideShare
Loading in...5
×

shell scripting

526

Published on

Published in: Technology
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
526
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
0
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide

Transcript of "shell scripting"

  1. 1. NATIONAL INSTITUTE OF TECHNOLOGYJAMSHEDPURComputer Science and Engineering Dept.
  2. 2. SHELL SCRIPTINGbyKUMAR SOURABH241/10
  3. 3. INDEX:• What is Shell• How Shell Works• How Shell Better• Type of Shell• What is Shell Script• Why We Write Shell Script• How We Run Shell Script• Variable in Shell• Problem Statement
  4. 4.  Shell is a agency that sits between the user and the UNIXsystem Shell is an command language interpreter that readcommands from the standard input device (keyboard) orfrom a file and execute them Shell is not part of system kernel, but uses the system kernel toexecute programs, create files etc.WHAT IS SHELL
  5. 5. WHAT IS SHELLUSER2
  6. 6. HOW SHELL WORKS?Shell is a agency that sits between the user and the UNIX systemComputer understand the language of 0s and 1s called binary language. which isdifficult for all of us, to read and write.So In OS there is special program called Shell.Shell issues the prompt and waits for us to enter a commandAfter a command is entered , the shell scans the command line for meta-character and expands abbreviation (like * in rm *)to recreate a simplifiedcommand lineIt then passes command line to kernel for executionNow the shell waits for the command to complete and normally it can’t do anywork while the command is runningAfter command execution is complete , shell returns to its waiting role to startthe next cycle . Now we are free to enter another commandHowever we can change this behavior and instruct not to wait
  7. 7. HOW SHELL WORKS?OurcommandorShell scriptLinuxShellConverted toBinary languageby ShellNow LinuxKernalUnderstadOurRequest
  8. 8. lsdatetime BASH001010001010101101010010LINUXKERNELExample:
  9. 9. Linux Shell is similar to Windows command promptbut are much more powerful.For example:On Linux it’s quite feasible to have multiple shellsinstalled, with different users able to pick the one theyprefer.:input and output can be redirected using <and >$ ls -l > lsoutput.txtHOW SHELL IS BETTER
  10. 10.  Sh – simple shellBASH – Bourne Again ShellKSH – Korne ShellCSH – C ShellSSH – Secure Shell To use a particular shell type the shell name at thecommand prompt.Eg $csh – will switch the current shell to c shell To view the available shells in the system, type cat/etc/shells at the command prompt To view the current shell that is being used, type echo$SHELL at the command promptTYPE OF SHELL-1
  11. 11. TYPE OF SHELL-2Shell NameDevelopedbyWhere RemarkBASH ( Bourne-Again SHell )Brian Foxand ChetRameyFreeSoftwareFoundationMost common shell inLinux.CSH (C SHell) Bill JoyUniversity ofCaliforniaThe C shells syntax andusage are very similar tothe C programminglanguage.KSH (Korn SHell) David KornAT & T BellLabsUsed in CommercialApplication
  12. 12.  Shell Script is series of command written in plaintext file. Shell script is just like batch file in MS-DOS buthave more power than the MS-DOS batch file.shell script is also known as shell programWHAT IS SHELL SCRIPT
  13. 13. Shell script can take input from user, file and outputthem on screen.Useful to create our own commands.Minimizes typing of repetitive commandSave lots of time.Can schedule jobs to run in the systemTo automate some task of day today life.System Administration part can be also automated.WHY TO WRITE SHELL SCRIPT
  14. 14. HOW TO WRITE SHELL SCRIPT
  15. 15. $ vi first#!/bin.sh## My first shell script#clearecho “Time nd Tide wait 4 no one"
  16. 16. Explanation:Script Command(s) Meaning$ vi firstvi editor## My first program## followed by any text is consideredcommentecho “Time nd Tide wait 4 no one” To print message or value of variableon screen, we use echo commandSyntax:echo "Message"
  17. 17. HOW TO RUN SHELL SCRIPTRun our script asSyntax: ./your-shell-program-nameFor e.g.$ ./first
  18. 18.  In Linux (Shell), there are two types of variable:System variables-: Created and maintained by Linuxitself and defined in CAPITAL LETTERS.User defined variables (UDV)-: Created andmaintained by user. This type of variable defined inlower letters.VARIABLE IN SHELLS
  19. 19. SYSTEM VARIABLES :System Variable Meaning1. BASH=/bin/bash Our shell name2. HOME=/home/kumar Our home directory3. PWD=/home/students/Common Our current working directory4. USERNAME=kumar User name who is currently login to PC5. SHELL=/bin/bash Our shell name
  20. 20. TEST OPERATORSTest Works in 3 Ways:o Numeric comparisono String comparisono File tests
  21. 21. EXAMPLE OF NUMERIC COMPARISON$ a=5; b=7; c=7.2$test $a –eq $b ; echo $?1$test $a -lt $b ; echo $?0$test $c –gt $b ; echo $?1$test $c –eq $b ; echo $?0
  22. 22. EXAMPLE OF FILE TESTS$ ls –l emp.txt-rw-rw-rw- …$ [ -f emp.txt ] ; echo $?0$ [ -x emp.txt ] ; echo $?1$ [ -d emp.txt ] || echo “file is not a directory”file is not a directory
  23. 23. STRING TESTTest True ifS1=s2 String s1=s2S1!=s2 String s1 is not equalto s2-n stg String stg is not nullstring-z stg String stg is nullstring
  24. 24. Conditions (Case)case expr inOption1) stmt ;;Option2) stmt ;;*) stmt ;;esacEvery option should be terminated with a doublesemicolon.Denotes default caseCase should be termniated with esac
  25. 25. ARRAYInitialising an arrayA[0] = 10A[1] = HiUsing an array${A[0]} : retrieves 10Here arrays can contain data belonging to differentdata typesUninitialised index in arrays will have null value bydefault
  26. 26. FUNCTIONLocal FunctionWritten at the command promptLasts for the current session aloneGlobal FunctionWritten in .bashrc fileAvailable as long as the definition is there in.bashrc fileFunction in shell scriptAvailable with in the file alone
  27. 27. ALIASAlias – Alternate name for an entityEntity here refers to commandWe can give another name or alias name for acommand either at the command prompt or in the.bashrc file.The former will be temporary and will vanish if thesession ends and the latter will be permanent aslong as the definition exists in the .bashrc file
  28. 28. Alias and Unaliasalias newname=oldnameEg alias copy=cpThen we can use copy in the same way we use cpcommandEg copy file1 file2 //copies content of file1 to file2To remove alias use unalias commandunalias copyAfter this we cannot use copy to perform copyingfunction
  29. 29. export : EXPORTING SHELL VARIABLES
  30. 30. Reference:1. A Practical Guide to Ubuntu Linuxby MARK G SOBEL2. Linux Foundation Organisation
  31. 31. Thank you!!

×