Your SlideShare is downloading. ×
shell scripting
Upcoming SlideShare
Loading in...5

Thanks for flagging this SlideShare!

Oops! An error has occurred.

Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

shell scripting


Published on

Published in: Technology

  • Be the first to comment

No Downloads
Total Views
On Slideshare
From Embeds
Number of Embeds
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

No notes for slide


  • 1. NATIONAL INSTITUTE OF TECHNOLOGYJAMSHEDPURComputer Science and Engineering Dept.
  • 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.  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
  • 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. HOW SHELL WORKS?OurcommandorShell scriptLinuxShellConverted toBinary languageby ShellNow LinuxKernalUnderstadOurRequest
  • 8. lsdatetime BASH001010001010101101010010LINUXKERNELExample:
  • 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.  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. 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.  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 script is also known as shell programWHAT IS SHELL SCRIPT
  • 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
  • 15. $ vi first#!/ My first shell script#clearecho “Time nd Tide wait 4 no one"
  • 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. HOW TO RUN SHELL SCRIPTRun our script asSyntax: ./your-shell-program-nameFor e.g.$ ./first
  • 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. 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. TEST OPERATORSTest Works in 3 Ways:o Numeric comparisono String comparisono File tests
  • 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. 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. 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. 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. 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. 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. 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. 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
  • 30. Reference:1. A Practical Guide to Ubuntu Linuxby MARK G SOBEL2. Linux Foundation Organisation
  • 31. Thank you!!