Chapter Six Introduction to Shell Script Programming.

Slides:



Advertisements
Similar presentations
Shell Script Assignment 1.
Advertisements

A Guide to Unix Using Linux Fourth Edition
A Guide to Unix Using Linux Fourth Edition
A Guide to Unix Using Linux Fourth Edition
NETW-240 Shells Last Update Copyright Kenneth M. Chipps Ph.D. 1.
CS Lecture 03 Outline Sed and awk from previous lecture Writing simple bash script Assignment 1 discussion 1CS 311 Operating SystemsLecture 03.
Linux+ Guide to Linux Certification, Second Edition
A Guide to Oracle9i1 Introduction To Forms Builder Chapter 5.
Guide To UNIX Using Linux Third Edition
Programming Logic and Design, Introductory, Fourth Edition1 Understanding Computer Components and Operations (continued) A program must be free of syntax.
Guide To UNIX Using Linux Third Edition
Guide To UNIX Using Linux Third Edition
Introduction to Unix (CA263) Introduction to Shell Script Programming By Tariq Ibn Aziz.
Chapter 3 Planning Your Solution
Fundamentals of Python: From First Programs Through Data Structures
Shell Programming, or Scripting Shirley Moore CPS 5401 Fall August 29,
Chapter Seven Advanced Shell Programming. 2 Lesson A Developing a Fully Featured Program.
Advanced File Processing
Advanced Shell Programming. 2 Objectives Use techniques to ensure a script is employing the correct shell Set the default shell Configure Bash login and.
Fundamentals of Python: First Programs
Lesson 7-Creating and Changing Directories. Overview Using directories to create order. Managing files in directories. Using pathnames to manage files.
Agenda Control Flow Statements Purpose test statement if / elif / else Statements for loops while vs. until statements case statement break vs. continue.
Introduction to Shell Script Programming
8 Shell Programming Mauro Jaskelioff. Introduction Environment variables –How to use and assign them –Your PATH variable Introduction to shell programming.
Week 7 Working with the BASH Shell. Objectives  Redirect the input and output of a command  Identify and manipulate common shell environment variables.
Agenda User Profile File (.profile) –Keyword Shell Variables Linux (Unix) filters –Purpose –Commands: grep, sort, awk cut, tr, wc, spell.
Chapter Four UNIX File Processing. 2 Lesson A Extracting Information from Files.
Guide To UNIX Using Linux Fourth Edition
Chapter 6: Shell Programming
Introduction to Unix – CS 21 Lecture 9. Lecture Overview Shell description Shell choices History Aliases Topic review.
Chapter Three The UNIX Editors. 2 Lesson A The vi Editor.
Input, Output, and Processing
Shell Script Programming. 2 Using UNIX Shell Scripts Unlike high-level language programs, shell scripts do not have to be converted into machine language.
Advanced File Processing. 2 Objectives Use the pipe operator to redirect the output of one command to another command Use the grep command to search for.
Linux+ Guide to Linux Certification, Third Edition
Linux Operations and Administration
UNIX Shell Script (1) Dr. Tran, Van Hoai Faculty of Computer Science and Engineering HCMC Uni. of Technology
Chapter Five Advanced File Processing Guide To UNIX Using Linux Fourth Edition Chapter 5 Unix (34 slides)1 CTEC 110.
Chapter Five Advanced File Processing. 2 Objectives Use the pipe operator to redirect the output of one command to another command Use the grep command.
Shell Programming. Introducing UNIX Shells  Shell is also a programming language and provides various features like variables, branching, looping and.
Linux+ Guide to Linux Certification Chapter Eight Working with the BASH Shell.
Shell Programming. Creating Shell Scripts: Some Basic Principles A script name is arbitrary. Choose names that make it easy to quickly identify file function.
Copyright © 2010 Certification Partners, LLC -- All Rights Reserved Perl Specialist.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
Review Chapters 5 thru 8. 2 Two Groups of Commands Select commands Manipulate and Format commands.
Chapter Five Advanced File Processing. 2 Lesson A Selecting, Manipulating, and Formatting Information.
Chapter Three The UNIX Editors.
Writing Scripts Hadi Otrok COEN 346.
©Colin Jamison 2004 Shell scripting in Linux Colin Jamison.
Copyright © 2003 ProsoftTraining. All rights reserved. Perl Fundamentals.
Shell Scripting – Putting it All Together. Agenda Escaping Characters Wildcards Redirecting Output Chaining and Conditional Chaining Unnamed and Named.
Lesson 3-Touring Utilities and System Features. Overview Employing fundamental utilities. Linux terminal sessions. Managing input and output. Using special.
Agenda Positional Parameters / Continued... Command Substitution Bourne Shell / Bash Shell / Korn Shell Mathematical Expressions Bourne Shell / Bash Shell.
Linux+ Guide to Linux Certification, Second Edition
Lesson 8-Specifying Instructions to the Shell. Overview An overview of shell. Execution of commands in a shell. Shell command-line expansion. Customizing.
1 Lecture 7 Introduction to Shell Scripts COP 3353 Introduction to UNIX.
Chapter 16 Advanced Bourne Shell Programming. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. Objectives To discuss numeric data processing.
Linux Administration Working with the BASH Shell.
Some of the utilities associated with the development of programs. These program development tools allow users to write and construct programs that the.
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
Lesson 5-Exploring Utilities
SUSE Linux Enterprise Desktop Administration
Shell Features CSCI N321 – System and Network Administration
Agenda Bash Shell Scripting – Part II Logic statements Loop statements
Shell Script Assignment 1.
Topics Introduction to File Input and Output
Guide To UNIX Using Linux Third Edition
Chapter Four UNIX File Processing.
Linux Shell Script Programming
Topics Introduction to File Input and Output
Presentation transcript:

Chapter Six Introduction to Shell Script Programming

2 Lesson A Using the UNIX Shell as a Scripting Language

3 Objectives Understand the program development cycle using a high-level computer language and UNIX shell scripts Compare the shells to determine the best choice for creating scripts Learn about shell variables, operators, and wildcard characters Write simple shell scripts to illustrate programming logic

4 The Program Development Cycle The program development cycle is the process of developing an application –The first step in the cycle is to create program specifications –The second step in the cycle is to create the program design –The third step is developing the code, which is written, tested, and debugged

5

6 Using High-Level Languages High-level languages are computer languages that use English-like expressions Example are; COBOL, C, C++ A program’s high-level language statements are stored in a file called the source file, which programmers creates using editors In order to execute, high-level source files must be converted into a low-level machine language file

7 Using High-Level Languages A compiler is a program that converts source files into executable machine-language files The complier reads the lines of code the programmer wrote in the source file and converts them to the appropriate machine language instructions If a source file contains syntax errors, it cannot be converted into an executable file –A programmer must correct these errors before the program can be run

8 Using UNIX Shell Scripts Unlike high-level language programs, shell scripts do not have to be converted into machine language by a compiler The UNIX shell acts as an interpreter when reading script files Interpreters read statements in script files and immediately translate them into executable instructions and cause them to run

9 Using UNIX Shell Scripts After creating shell script, the OS is instructed that the file is an executable shell script via the chmod command When the file is designated as executable, you may it run in one of many ways: –Type the script name at the command prompt after updating the path variable –If the script is in the current directory, proceed its name at the prompt with a dot slash (./) –If not in the current directory, specify the absolute path at the command prompt

10 The Programming Shell All Linux versions use the Bash shell as the default

11 Variables Variables are symbolic names that represent values stored in memory Three types of variables are: –Configuration variables store information about the setup of the OS –Environment variables hold information about your login session –Shell variables are created at the command prompt or in shell scripts and are used to temporarily store information

12 Variables Use the printenv command to see a list of environment variables

13

14

15

16 Variables To set: example=one To see: echo $example To make part of the environment: export example To remove: unsetenv example

17 Shell Operators Bash shell operators are in three groups: –Defining and Evaluating operators are used to set a variable to a value and to check variable values The equal sign (=) is an example –Arithmetic operators are used to perform mathematical equations The plus sign (+) is an example –Redirecting and piping operators are used to specify input and output data specifications The greater than sign (>) is an example

18 Shell Operators

19 More About Wildcard Characters Shell scripts often use wildcard characters Wildcard characters are intended to match filenames and words –Question mark (?) matches exactly one character –Asterisk (*) matches zero or more characters –[chars] defines a class of characters, the glob pattern matches any singles character in the class

20 Shell Logic Structures Four basic logic structures needed for program development are: –Sequential logic –User input –Decision logic –Looping logic –Case logic

21 Sequential Logic commands are executed in the order in which they appear in the script break in sequence occurs when a branch instruction changes the flow of execution by redirecting to another location in the script

22 User input Script can read user data Command: read variable reads user input and assigns text to variable

23 User input Command: read var1 var2 var3 reads 3 words and assigns 3 variables Last variable contains rest of input line

24 User input Command: read –p “enter name: “ name Prompts user, then reads input and assigns to variable

25 User input example

26 Decision Logic Enables your script to execute statement(s) only if a certain condition is true Condition: –result of a command –Comparison of variables or values if statement

27 If statement Syntax: if [ condition ] thenstatementselsestatementsfi

28 Decision Logic example

29 Nested Decision Logic

30 Looping Logic A control structure repeats until some condition exists or some action occurs Two common looping mechanisms: –For loops cycle through a range of values until the last in a set of values is reached –The while loop cycles as long as a particular condition exists

31 For Loop Syntax for var in list dostatementsdone

32 For Loop example Program control structures can be entered from the command line

33 For loop in script

34 Loop with wildcard

35 While Loop Syntax while [ condition ] dostatementsdone

36 Looping Logic The while loop tests repeatedly for a matching condition

37 Looping Logic While loops can serve as data-entry forms

38 While loop to enter data

39 Case Logic The case logic structure simplifies the selection from a list of choices It allows the script to perform one of many actions, depending on the value of a variable Two semicolons (;;) terminate the actions taken after the case matches what is being tested

40 Case statement Syntax: case $variable in “pattern1”)statements;;“pattern2”)statements;;esac

41 Case example

42 Case Logic

43 Debugging a Shell Script Shell script will not execute if there is an error in one or more commands sh has options for debugging –sh -v displays lines of script as they are read by the interpreter –sh -x displays the command and its arguments line by line as they are run

44 Debugging a Shell Script View the script line by line as it is running to help locate errors

45 Lesson B Creating and Completing the Corporate Phone Application

46 Objectives Create screen-management scripts Use the trap command Enter and test shell scripts to print the phone records, view the contents of the corp_phone file, and add new phone records to the file

47 Using Shell Scripting to Create a Menu A menu is a good example of a shell script that employs the four basic logic structures A significant feature of the menu script is the screen presentation which should be as appealing and user-friendly as possible

48 tput command tput clear –clear the screen tput cup r c –position cursor to row and column –ex: tput cup 0 0 tput cup tput cup bold=`tput smso` offbold=`tput rmso`

49 Example tput clear; tput cup 10 15; echo “Hello”; tput cup 20 0

50 Creating a Menu tput can be used to help create data entry screens

51 Creating a Menu

52 Creating a Menu tput can be used to help create user menus

53 Creating a Menu

54 The trap Command used to guard against abnormal termination of script –user ^C –OS intervention normal: remove temporary file example: trap ’rm ~/tmp/*’ 2 15

55 Creating the corp_phones File The grep command is useful when building script applications by extracting data from files

56 Creating the corp_phones File Using awk speeds development in that it can select fields from many records and display them in a specified format on the screen

57 Creating the phoneadd Shell Script The phoneadd script allows you to add new records to the corp_phones file

58 Chapter Summary A high-level language uses English-like expressions and must be converted into a low- level language before being executed The shell interprets shell scripts UNIX shell script instructions do not need to be written from scratch, they are chosen from an inventory of executable commands Linux shells are derived from the UNIX Bourne, Korn and C shells, and bash is the default UNIX employs three types of variables: configuration, environment, and shell

59 Chapter Summary The shell supports numerous operators, including many for performing arithmetic operations Wildcard characters are used in shell scripts The logic structures supported by the shell are sequential, decision, looping and case The tput command can be used to manage cursor placement on the screen Programmers and system administrators often customize the.bashrc file to suit their needs

60 Chapter Summary Aliases, used to simplify commonly used commands, can be entered into the.bashrc Use the trap command to remove temporary files after the script exits The grep command serves a key role in the development of shell scripts by allowing searching and retrieving data from files The awk command serves as an effective and easy-to-use tool for generating reports

61