Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Shell Scripting

3,139 views

Published on

A brief introduction to UNIX shell scripts presented at Linux Users of Victoria, Australia

Published in: Technology
  • shell unix linux scripting
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • @rporbitz Do you mean convert sh to Python or Python to sh? Either way I figure you need to re-write it by hand unless Google can help you find something....
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • How to convert Shell script in Python? can you please suggest me some ways
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • This slide 'Why do we need scripts?' has too much text. The bottom line is cut off after ’unmounted file’
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • Alec...just a suggestion. Since this is for relative newbies to scripting, you shouldn't assume 'regexp' is known. Spell it out to 'regular expressions'. You may need to define what they are, or at least say you will later. Gary
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here

Shell Scripting

  1. 1. Unix Shell Scripting Alec Clews
  2. 2. Agenda <ul><li>We will look at how to write Bourne shell scripts
  3. 3. We need to understand </li><ul><li>Why you need to it
  4. 4. How you do it </li></ul><li>I assume you have some basic programming experience </li></ul>
  5. 5. Plan <ul><li>We will take about 45 minutes
  6. 6. What is a script
  7. 7. Why we need to write shell scripts
  8. 8. Features of the Bourne shell
  9. 9. Quoting rules
  10. 10. Further references </li></ul>
  11. 11. What is a script? <ul><li>High level language
  12. 12. Usually handles high level identities: Files; processes; users, records. Not bits and bytes
  13. 13. Built in support for some form of wildcard or regexp
  14. 14. A few lines do a lot
  15. 15. Interpreted line by line. No compiling or linking required
  16. 16. May require a large number of external programs to do useful work: e.g. rm, awk, sqlplus (for Oracle) </li></ul>
  17. 17. Why do we need scripts? <ul><li>Why use scripts when we have C/C++ and Java?
  18. 18. A few lines of script do a lot. With practice it's quick and easy
  19. 19. We may not have C or Java runtime support available, even if it's installed it could be on an unmounted file system – this applied to the modern scripting languages as well (the shell is always on the root filesystem)
  20. 20. The shell command language is the natural way to control process, and manipulate files and file systems </li></ul>
  21. 21. The Bourne Shell <ul><li>Named after Steve Bourne, the original developer
  22. 22. Variants of Bourne shell ship on all *NIX
  23. 23. Rudimentary programming language </li><ul><li>if then else, case, while, for statements
  24. 24. Simple variables (no arrays, maps etc.)
  25. 25. Simple I/O
  26. 26. Beware the overused set command
  27. 27. Powerful but hard to use quoting rules </li></ul></ul>
  28. 28. Danger Will Robinson <ul><li>Under no circumstances should the csh or it's variants be used for shell scripts
  29. 29. Poor File IO and support for file redirection
  30. 30. Cannot handle signals </li><ul><li>And so on... </li></ul><li>See http://www.faqs.org/faqs/unix-faq/shell/csh-whynot/ for more information </li></ul>
  31. 31. Using Environment Variables <ul><li>Variables can copy information from parent to child processes only
  32. 32. Variables must be exported before can be copied to the child's environment </li></ul>FRED=bill ; export FRED
  33. 34. Quoting <ul><li>One of scariest parts of the shell is the quoting syntax. However a few simple rules can help us </li><ul><li>&quot;..&quot; : Anything in rabbit ears is treated as a single token. However the shell looks inside and does most (?) magic required (interpolate variables etc.) </li></ul></ul>
  34. 35. Quoting contd: <ul><li>'..' : Single quotes treat whatever inside as literal text. All magic is turned off
  35. 36. The backslash '' escapes the previous character
  36. 37. So 'fred's car cost $100' really is fred's cheap car and not a syntax error </li></ul>
  37. 38. Quoting contd: <ul><li>`...` : Backticks cause the output from the quoted command to appear as replaced text. e.g. </li></ul>FRED=/etc/passwd echo `cat $FRED` <ul>displays the contents of the password file <li>In modern shells use $(...) instead if you know it's avaliable. e.g. </li></ul>echo $(cat $FRED)
  38. 39. Summary <ul><li>Learn basic Bourne shell programming to maintain your system
  39. 40. Learn the quoting rules
  40. 41. Don't use csh or tcsh etc. for shell scripts
  41. 42. Prefer modern languages if possible (e.g. Perl, Python etc.) </li></ul>
  42. 43. Further reading <ul><li>The UNIX Shell FAQs </li><ul><li>http://www.faqs.org/faqs/unix-faq/shell/ </li></ul><li>Ch 22 of New Riders Autoconf book (ch 21 in dead tree version) </li><ul><li>http://sources.redhat.com/autobook/autobook/autobook_208.html#SEC208 </li></ul></ul>

×