Shell Scripting

  • 2,512 views
Uploaded on

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

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

More in: Technology
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
  • shell unix linux scripting
    Are you sure you want to
    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....
    Are you sure you want to
    Your message goes here
  • How to convert Shell script in Python? can you please suggest me some ways
    Are you sure you want to
    Your message goes here
  • This slide 'Why do we need scripts?' has too much text. The bottom line is cut off after ’unmounted file’
    Are you sure you want to
    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
    Are you sure you want to
    Your message goes here
No Downloads

Views

Total Views
2,512
On Slideshare
0
From Embeds
0
Number of Embeds
1

Actions

Shares
Downloads
123
Comments
5
Likes
2

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

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