CST8177 Scripting 3: How?. But First... How do you approach a scripting problem? There are several steps to the process. In simplified form, they include:

Slides:



Advertisements
Similar presentations
CST8177 bash Scripting Chapters 13 and 14 in Quigley's "UNIX Shells by Example"
Advertisements

CST8177 awk. The awk program is not named after the sea-bird (that's auk), nor is it a cry from a parrot (awwwk!). It's the initials of the authors, Aho,
Lecture 4 Basic Scripting. Administrative  Files on the website will be posted in pdf for compatibility  Website is now mirrored at:
Chapter 3: Editing and Debugging SAS Programs. Some useful tips of using Program Editor Add line number: In the Command Box, type num, enter. Save SAS.
Exception Handling Introduction Exception handling is a mechanism to handle exceptions. Exceptions are error like situations. It is difficult to decide.
Python Programming Chapter 1: The way of the program Saad Bani Mohammad Department of Computer Science Al al-Bayt University 1 st 2011/2012.
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
Program Design and Development
Computer Science 1620 Programming & Problem Solving.
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.
Introductory CMS Training Welcome to the new Montana State University Content Management System!
Bash Shell Scripting 10 Second Guide Common environment variables PATH - Sets the search path for any executable command. Similar to the PATH variable.
11 Chapter 4 LOOPS AND FILES. 22 THE INCREMENT AND DECREMENT OPERATORS To increment a variable means to increase its value by one. To decrement a variable.
Fundamentals of Python: From First Programs Through Data Structures
Shell Programming, or Scripting Shirley Moore CPS 5401 Fall August 29,
Programming For Nuclear Engineers Lecture 12 MATLAB (3) 1.
Chapter Seven Advanced Shell Programming. 2 Lesson A Developing a Fully Featured Program.
Advanced Shell Programming. 2 Objectives Use techniques to ensure a script is employing the correct shell Set the default shell Configure Bash login and.
Introduction to Shell Script Programming
1 Operating Systems Lecture 3 Shell Scripts. 2 Brief review of unix1.txt n Glob Construct (metacharacters) and other special characters F ?, *, [] F Ex.
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.
– Introduction to the Shell 10/1/2015 Introduction to the Shell – Session Introduction to the Shell – Session 2 · Permissions · Users.
An Introduction to Unix Shell Scripting
Mail merge I: Use mail merge for mass mailings Perform a complete mail merge Now you’ll walk through the process of performing a mail merge by using the.
Shell Scripting Todd Kelley CST8207 – Todd Kelley1.
Chapter Three The UNIX Editors. 2 Lesson A The vi Editor.
1 Functions 1 Parameter, 1 Return-Value 1. The problem 2. Recall the layout 3. Create the definition 4. "Flow" of data 5. Testing 6. Projects 1 and 2.
Shell Script Programming. 2 Using UNIX Shell Scripts Unlike high-level language programs, shell scripts do not have to be converted into machine language.
Linux+ Guide to Linux Certification, Third Edition
Linux Operations and Administration
1 Week 2 The Crunchy Shell to the Soft and Chewy Kernel… Sarah Diesburg 8/3/2010 COP4610 / CGS5765.
Linux+ Guide to Linux Certification Chapter Eight Working with the BASH Shell.
Basic & Advanced Reporting in TIMSNT ** Part Two **
Forms and Server Side Includes. What are Forms? Forms are used to get user input We’ve all used them before. For example, ever had to sign up for courses.
C Hints and Tips The preprocessor and other fun toys.
30/10/ Iteration Loops Do While (condition is true) … Loop.
CSC 110 Using Python [Reading: chapter 1] CSC 110 B 1.
BMTRY 789 Lecture 11: Debugging Readings – Chapter 10 (3 rd Ed) from “The Little SAS Book” Lab Problems – None Homework Due – None Final Project Presentations.
Introduction to Loops For Loops. Motivation for Using Loops So far, everything we’ve done in MATLAB, you could probably do by hand: Mathematical operations.
Chapter Three The UNIX Editors.
Fall 2003Sylnovie Merchant, Ph.D. ACCESS Tutorial Note: The purpose of this tutorial is to provide an introduction to some of the functions of ACCESS in.
Chapter Six Introduction to Shell Script Programming.
Scripting Languages Diana Trandab ă ț Master in Computational Linguistics - 1 st year
1 Homework Done the reading? –K&R –Glass Chapters 1 and 2 Applied for cs240? (If not, keep at it!) Gotten a UNIX account? (If not, keep at it!)
Basic ActionScript and PHP Cis 126. Getting Started set up a basic folder structure so we can keep our files organized. Mirror this structure on your.
Introduction to Programming in Corvid EXSYS-Corvid is an intelligent systems programming environment General order of tasks: Enter and define Variables.
Controlling Program Flow with Decision Structures.
Linux+ Guide to Linux Certification, Second Edition
Group, group, group One after the other: cmd1 ; cmd2 One or both: cmd1 && cmd2 Only one of them: cmd1 || cmd2 Cuddling (there):( cmd1 ; cmd2 ) Cuddling.
Compunet Corporation Introduction to Unix (CA263) Round and Round By Tariq Ibn Aziz Dammam Community College.
Batch Files Flow of Control to Strengthen Copyright © by Curt Hill.
Linux Administration Working with the BASH Shell.
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
Part 1 Learning Objectives To understand that variables are a temporary named location to store data and that programmers work with different data types.
Functions. What is a Function?  We have already used a few functions. Can you give some examples?  Some functions take a comma-separated list of arguments.
Chapters 13 and 14 in Quigley's "UNIX Shells by Example"
Functions CIS 40 – Introduction to Programming in Python
Scripts & Functions Scripts and functions are contained in .m-files
Sentinel logic, flags, break Taken from notes by Dr. Neil Moore
Designing and Debugging Batch and Interactive COBOL Programs
I/O in C Lecture 6 Winter Quarter Engineering H192 Winter 2005
Conditions and Ifs BIS1523 – Lecture 8.
File I/O in C Lecture 7 Narrator: Lecture 7: File I/O in C.
Coding Concepts (Basics)
CST8177 Scripting 2: What?.
Presentation transcript:

CST8177 Scripting 3: How?

But First... How do you approach a scripting problem? There are several steps to the process. In simplified form, they include: 1. Analysis 2. Design 3. Building 4. Testing Let us first have a good look at these steps.

Analysis and Design Building Testing

1. The Analysis Process First of all, decide what the actual problem is that is to be solved. Sounds simple, right? It may be, if it's a program you need, and you have a good idea of what it needs to do. It's not so simple if you're writing it for someone else, like your boss (or your professor). You will have to carefully read whatever you've been given, perhaps checking back to clarify parts, until you have a full understanding. In either situation, lay down the full requirements in writing. Include in this document considerations you have carefully gone over, including: What inputs are needed? Be sure to consider both your own files, system or user file, the command-line arguments, and stdin. If you need a config file, decide what will go in it. What kind of normal output will there be? Consider stdout, log files, and system and user files. Define the format if you can.

1. The Analysis Process What kind of errors can be expected? What about unexpected errors, like missing files? Consider every possible (and some impossible) eventuality. Write down the conditions for the error and decide what debug information you will need to see. If it's also going to a system log file, make sure you follow the local style standard. Rough out, perhaps a list of bullet points, what steps need to be done, in the sequence you expect they will be needed. Consider decisions points and loops, but only in an informal manner. Write a how-to-use document for anyone who might be using your script. Show the command line, its options and arguments, and any prompts the script will make to the keyboard user.

2. The Design Process You may need to have your Analysis approved, or at least reviewed by a class-mate. This is the time to make sure you are proceeding in the correct direction. Now you can expand the roughed-out steps into full PDL (Problem Description Language). This describes WHAT the script is doing and WHY it's doing it. You do not need to describe HOW it's to be done -- that comes later. Define the all input data the script will need, if any. Choose a file name if it's your own file, otherwise write down the name of the system or user file. Specify both the content and the layout (the format of the content). Don't overlook command-line options and arguments. Do the same for all the output and the error messages. Actually write the text for each one. If you will be using the system logging facility, state the facility and priority to be used. If this is an addition to the logging rules, specify also the name and location of the (new?) log file.

2. The Design Process You will now have a well thought-out plan for your script. The next step is to walk through walk through the PDL, step by step, with reference to your input and output. You can update the PDL at this time as you uncover steps you have missed, or glossed over too simply. Finally, create and document a Test Plan. The goal of your Test Plan is to ensure that you can turn over the Plan, the final script, and the how-to document to a class-mate and expect them to be able to produce correctly-formatted test results. To make this possible (and to simplify your own execution of the Test Plan), design at this time any special files or tools, even other scripts, that will make testing simple and repeatable. Reduce the amount of typing on stdin that's required by redirecting a file as input, and also redirect stdout and stderr into separate files for automatically checking the results.

3. The Building Process It's time to write the script. By now you have developed a solid understanding of what problem is being solved and what the script is going to do to solve it. As a first step, construct any files that are needed, such as a configuration file. You're going to test each chunk of script as you write it, so have this in place at the start. Now convert the PDL to script. This is the HOW of the script, where the WHAT and WHY of the PDL become actual scripting statements. You do it a few lines at a time; many people turn the PDL into comments, and leave it throughout the script to describe what the next bit of script does. You may have to turn to one side, so to speak, to write a tiny test script. This will help you determine how you can correctly build some of the complex parts of the script. On the other hand, you can keep useful script fragments to cut-and-paste into a script you are building.

3. The Building Process Convert the PDL to script by repeating the following steps until it's all done: Write a few lines of script based on the PDL; Save your script file; you may wish to use a method that allows you to keep the last-good-version (save the current working copy under a test name; copy the 2 generations of backups only when this one has tested clean and error-free); Run the script in debug mode, carefully making sure each chunk of script behaves as expected before moving to the next; Make full and complete fixes of any script errors. If necessary, go back to your Design material and fix it as well. Once you have completed this translation from PDL, run through the whole script as you mean it to be used.

4. Test process Now it's the time to run the Test Plan, fixing errors until your script passes all the tests. This is often called the Alpha test, where the developer(s) of the script establish that what they have built operates as expected, even in error cases. If you can (unlikely, I expect), get a class-mate who unfamiliar with your script to run the Test Plan from the how-to-use document. This is a lost art, it appears, that of the Beta test. In times gone by, companies would have quality assurance people who did this for a living. The next step is to distribute the script, any required files, and the how-to document to a small set of live users. This may still happen in the Real World, since I still see references to Release Candidates. Assuming all of the above proceeds smoothly, you can now release your script to the world, if that was the goal.

An example This might be a casual or informal description from the Analysis of one of the script requirements: kick user out unless root Notice the brevity and simplicity. In the Design, turn this into good PDL - - describing WHAT and WHY, not HOW: IF user is not root DISPLAY error message EXIT ENDIF There are a few things to note here. The keywords that make this PDL are shown in UPPER CASE, in an effort to increase their visibility. For much the same reason, the PDL is indented for the contents of the IF-block. And each control structure, the IF-ENDIF here, is paired to mark both its beginning and the end.

An example Here, then, is the actual code -- the HOW: #! /bin/bash # kick user out unless root if [ $UID != 0 ]; then echo Must be root user exit 1 fi In some ways it's not too unlike the PDL. It looks almost like a one- to-one replacement, but that's not always the case.

An example You might wonder why it use UID and not USER? Surely comparing $USER to "root" can't be all that different from comparing $UID and 0. It's because UID has been set to be read- only, but the USER variable can be changed by any user. Prompt$ echo UID is $UID and USER is \"$USER\" UID is 500 and USER is "allisor" Prompt$ USER="root" Prompt$ echo UID is $UID and USER is \"$USER\" UID is 500 and USER is "root" Prompt$ UID=0 bash: UID: readonly variable Prompt$ echo UID is $UID and USER is \"$USER\" UID is 500 and USER is "root"

An example That was fairly painless, I hope. You have just learned 5 components of PDL: 1. IF -- ENDIF is a control structure, using the result of a comparison to make a choice. It comes in two more flavours that we'll look at soon. It, like all control structures, is paired to mark both the start and end of the block of statements. 2. Statements inside control structures (often called blocks) are indented for visibility. 3. There are 6 comparison operators for numbers. They are: == != > >= < <= 4. DISPLAY (or PRINT or SHOW) are used as output statements. 5. EXIT leaves the script.

An example You have even learned 8 components of scripts: 1. The hash-bang line: it must be the very first line in the script, since the #! serves to tell the shell that this is a script. The rest of that line is the absolute path to the script program, /bin/bash for us. 2. There can be blank lines anywhere you need them for readability. Don't be afraid to use a few. 3. A comment is marked by a #. You can use a full line comment as here, or put a comment on a statement line after the script part: exit 1 # leave now 1. This is a specific form of the if-statement. We'll look at the general form later. The fi (yes, that's if spelled backwards. Sigh) is the end of the if-block, just like the PDL.

An example 4. The indenting is also just like the PDL, and for the same reason: to improve the readability for us humans. Be sure to indent properly. 5. There are 6 comparison operators for numbers. They are: == != > >= < <= Actually there is a whole other set for scripts in addition to these, but we'll address that later. 6. You're already familiar with the echo(1) built-in, although it has features you haven't used. Have a look at the printf(1) command for formatted output. 7. The exit statement terminates the current shell. You may already use it to return after an su. It takes one number(or numeric variable) as an argument to return to the caller for status. By convention, 0 is OK and any other number (it should be >0) shows some sort of error condition.

A Test Script Let's take the code snippet we've been looking at and turn it into a test script. The only real change is the addition of a couple of echo statements: #! /bin/bash echo current UID is $UID if [ $UID != 0 ]; then echo Must be root user exit 1 fi echo UID is root exit 0 I have saved this in a test directory so I can run a few tests, both as a user and as root. Since I've already decided to use UID and not USER, since UID is protected, there's no reference to the global USER variable.

A Test Script Here a short test run: tmp]$./test-uid current UID is 500 Must be root user tmp]$ su Password: tmp]#./test-uid current UID is 0 UID is root tmp]# exit exit tmp]$ Did it work correctly? Without an Analysis of the requirements and a Test Plan, how can I tell?