1 / 65

Introduction to Linux Shell Script Programming

Introduction to Linux Shell Script Programming. Summer course, Institute of Bioinformatics National Yang-Ming University. Menu Today!. Part I: Shell Script in Practice Basics about Shell and Exercises of system Shell Scripts Perl Shell Script Part II: The applications of Shell Script

EllenMixel
Download Presentation

Introduction to Linux Shell Script Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Introduction to Linux Shell Script Programming Summer course, Institute of Bioinformatics National Yang-Ming University

  2. Menu Today! • Part I: Shell Script in Practice • Basics about Shell and Exercises of system Shell Scripts • Perl Shell Script • Part II: The applications of Shell Script • Massive routing jobs • Scheduling • Backup

  3. or this? This? This? Shell !?

  4. First glance — Linux Shell • Why Shell? • Computer only realize the command in binary form which is difficult for most of human • So OS provides a special program call ‘shell’ accepts human’s command in ‘readable’ form and translates them into 1 and 0 stream Your commands Converted binary commands Linux shell OS kernel $ ls $ man $ date 00010001010 10010110000 11100110100 BASH Linuxkernel

  5. Text Shells

  6. Kernel • Definition • It is heart of Linux OS • It manages all resources of OS • What it charges • I/O (Input and Output) • Process • Devices • File • Memory

  7. What is Shell? • Shell is an command language interpreter that executes commands read from the standard input device (your keyboard) or from a file • In Linux OS, it may use one of the following most popular shells (BASH, CSH and KSH) • In Microsoft DOS, the name of shell is COMMAND.COM, but it is NOT as powerful as Linux shell

  8. Operating System Shell • Shell of an operating system • it is a program that presents an interface to various operating system functions and services • Why named “shell”? • it is an outer layer of interface between the user and the innards of the OS (kernel)

  9. Shell, an interface • Main categories • CLI (Command Line Interface) • it makes things clear • text shell (what we are going to learn now) • GUI (Graphical Use interface) • it makes things look easy • graphic shell (what people always use nowadays)

  10. CLI • Unix shells • Bourne shell (sh) • Almquist shell (ash) • Bourne-Again shell (bash) • C shell (csh) • TENEX C shell (tcsh) • Korn shell (ksh) • Scheme shell (scsh) • Z shell (zsh) • Plan 9 and Unix • rc shell (rc) • DOS: command.com • OS/2 and windows NT: cmd.exe • DOS, OS/2 and NT • 4DOS, 4OS2, 4NT

  11. GUI DOSSHELL • MS windows • windows explorer • litestep • Geoshell • BB4Win • Emerge Desktop • Mac OS: Machitosh Finder • X-window system (Unix) • KDE, GNOME • Blackbox, CDE • DOSSHELL MS Mac GNOME KDE

  12. Popular Linux Shells

  13. Bell Labs: The beginning • The research and development center of Lucent Technologies, formerly AT&T. • Bell labs is one of the most renowned scientific laboratories in the world Alexander Graham Bell founds the company that becomes AT&T with 2 financial backers at 1876. He is also the inventor of the telephone. His famous sentence “Mr. Watson. Come here! I want you!” were the first words to travel over a wire, ringing in the birth of electronic communication Alexander Graham Bell (1847~1922)

  14. Bell Labs: Brief • Its official name is Bell Telephone Laboratories or Bell Labs which was originally the research and development arm of the United states Bell System, and it was also the premier corporate facility of its type, developing a range of revolutionary technologies from telephone switches to specialized coverings for telephone cables, to transistor. • The work done by Bell Labs are • Research • theoretical underpinnings for communications • it includes math, physics, material science, behavioral sciences, computer programming • System engineering • concerning itself with conceiving the highly complex systems that make up the telecommunication networks • Development • hardware and software for Bell System’s communication networks

  15. Bell Labs: big events • 1933, discovered radio waves emitted from center of galaxy • 1947, invention of transistor (Nobel Prize in Physics in 1956) • 1948, Claude Shannon published “A Mathematical Theory of Communication” • 1954, the development of photovoltaic cell • 1957, electronic music by Max Mathews • 1970s, Unix and C language • 1971, computerized switching system for telephone traffic • 1980, the realization of the world’s first single chip 32-bit microprocessor, the BELLMAC-32A • 1980, C++ language • late 1980s and early 1990s, developed Plan9 as a replacement for Unix • 1990s, inferno OS

  16. Variables in Linux • In the machine you are using, it has memory for storing your data. These memory are divided into smaller locations (address) and one can give them a name called memory variable or variable • There are 2 kinds of variables • System variables • created and maintained by O.S. itself, all their name are capital. • User-defined variable (UDV) • created and maintained by user, all their name are lower-case. • Check variables • set: check all variables • env: check system variables only

  17. The shell you use now • What is your current shell? • grep username /etc/passwd • echo $SHELL • chsh (you can change the default shell here) • How many shell you can use • cat /etc/shells • How to change current shell temporarily? • just type the name of new shell

  18. Script Components • designator line • Tell the O.S. where is the correct interpreter • it begins with #! • comments • it begins with # • shell commands • correct separator: semicolon (;) or new line (\n)

  19. Tea Time Take a break! Review what you have learned

  20. Start Your First Shell Script

  21. Your first Bash shell script: How are you doing? locate the shell you use, start with #! comments, start with # set variables (bash style) output the results

  22. Discussion:the difference between apostrophes and quotation marks

  23. Your 2nd Bash shell script: some arithmetic computations locate the shell you use, start with #! comments, start with # No variable declaration Declare as Integers

  24. Discussion: what is the difference?

  25. Declare • Syntax: declare [-afir] variable[=value] • Options: • -a: declared as array • -f: declared as function • -i: declared as integer • -r: declared as read-only variable

  26. Interactive shell script

  27. Multiple Arguments:How to access?

  28. Logical operators arithmetic only, not for text Both arithmetic and text

  29. Flow control • Branches • If then, else • Case • Loops (for, while, until) • it is a block of code that iterates a list of commands as long as the loop control condition is true

  30. Branches

  31. (1) If then, else • Syntax and example: • If [condition 1];then [statement 1] • elif [condition 2];then [statement 2] • else [statement 3] • fi Start cond 1 else then cond 2 then else stat 3 stat 2 stat 1

  32. Simple form User enter … (stored as yn) yn=y then else STOP! script is running

  33. Complex conditions User enter … (stored as yn) yn=y || yn=Y else then STOP! script is running

  34. else if

  35. Check your file

  36. logic tags for file identification

  37. Check and Create file check properties while it exists ask user whether wanna create it while it does not exist

  38. Remark: if then, else statements • You can link several conditions by || or && • Each condition must be between [ ] • Caution! the space between condition statement and [ or ] is necessary

  39. Advanced Example Analyze your host ~

  40. (2) Case • syntax and example • case variable in • value_1) [statements_1] ;; • value_2) [statements_2] ;; • value_3) [statements_3] ;; • *) [statements_for_exception] • esac

  41. Fortune Teller

  42. Loops

  43. for loop • syntax: • for(( initial value; stop criteria; increment/decrement )) • do [statements] • done • simple test • The sum from 1 to 100

  44. Do what gauss has doneby shell script

  45. syntax while [ condition ] do [statement] done syntax until [ condition ] do [statements] done while loop and until loop

  46. Rewritten in while loop

  47. Rewritten in until loop

  48. Compare while and until > <= while it reaches, stop while it holds, continue

  49. for loop, revisit • alternative syntax • for variable in variable_list • do [statement] • done • it differs significantly from its C counterpart

  50. Solar system

More Related