CST8177 Scripting 2: What?.

Slides:



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

CIS 118 – Intro to UNIX Shells 1. 2 What is a shell? Bourne shell – Developed by Steve Bourne at AT&T Korn shell – Developed by David Korn at AT&T C-shell.
1 © 2001 John Urrutia. All rights reserved. Chapter 5 The Shell Overview.
CS 497C – Introduction to UNIX Lecture 22: - The Shell Chin-Chih Chang
Linux+ Guide to Linux Certification, Second Edition
Guide To UNIX Using Linux Third Edition
Introduction to Unix (CA263) Introduction to Shell Script Programming By Tariq Ibn Aziz.
Console and File I/O - Basics Rudra Dutta CSC Spring 2007, Section 001.
Shell Script Examples.
Second edition Your UNIX: The Ultimate Guide Das © 2006 The McGraw-Hill Companies, Inc. All rights reserved. UNIX – Shell Programming The activities of.
Shell Programming, or Scripting Shirley Moore CPS 5401 Fall August 29,
Agenda Control Flow Statements Purpose test statement if / elif / else Statements for loops while vs. until statements case statement break vs. continue.
1 Operating Systems Lecture 3 Shell Scripts. 2 Brief review of unix1.txt n Glob Construct (metacharacters) and other special characters F ?, *, [] F Ex.
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 UNIX / Linux - 11
File Processing. Introduction More UNIX commands for handling files Regular Expressions and Searching files Redirection and pipes Bash facilities.
Chapter 6: Shell Programming
An Introduction to Unix Shell Scripting
The UNIX Shell. The Shell Program that constantly runs at terminal after a user has logged in. Prompts the user and waits for user input. Interprets command.
Linux Shell Programming Tutorial 3 ENGR 3950U / CSCI 3020U Operating Systems Instructor: Dr. Kamran Sartipi.
Shell Scripting Todd Kelley CST8207 – Todd Kelley1.
GNU Compiler Collection (GCC) and GNU C compiler (gcc) tools used to compile programs in Linux.
Shell Script Programming. 2 Using UNIX Shell Scripts Unlike high-level language programs, shell scripts do not have to be converted into machine language.
Additional UNIX Commands. 222 Lecture Overview  Multiple commands and job control  More useful UNIX utilities.
Guide to Linux Installation and Administration, 2e1 Chapter 7 The Role of the System Administrator.
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
Shell Programming Any command or a sequence of UNIX commands stored in a text file is called a shell program. It is common to call this file a command.
#!/bin/sh echo Hello World cat Firstshellscript.sh Firstshellscript.sh.
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.
Chapter Four I/O Redirection1 System Programming Shell Operators.
UNIX shell environments CS 2204 Class meeting 4 Created by Doug Bowman, 2001 Modified by Mir Farooq Ali, 2002.
1 Lecture 9 Shell Programming – Command substitution Regular expressions and grep Use of exit, for loop and expr commands COP 3353 Introduction to UNIX.
Lecture 26: Reusable Methods: Enviable Sloth. Creating Function M-files User defined functions are stored as M- files To use them, they must be in the.
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
Introduction to Bash Shell. What is Shell? The shell is a command interpreter. It is the layer between the operating system kernel and the user.
Agenda The Bourne Shell – Part I Redirection ( >, >>,
File Processing. Introduction More UNIX commands for handling files Regular Expressions and Searching files Redirection and pipes Bash facilities.
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.
INTRODUCTION TO SHELL SCRIPTING By Byamukama Frank
Introduction to the bash Shell (Bourne-Again SHell)
Chapters 13 and 14 in Quigley's "UNIX Shells by Example"
Foreground and background processes
Implementation of a simple shell, xssh
Input from STDIN STDIN, standard input, comes from the keyboard.
Implementation of a simple shell, xssh (Section 1 version)
Bash Introduction (adapted from chapters 1 and 2 of bash Cookbook by Albing, Vossing, & Newham) CPTE 440 John Beckett.
How to link a test to a launcher (in this case a shell launcher)
Agenda Bash Shell Scripting – Part II Logic statements Loop statements
Implementation of a simple shell, xssh
Chapter 22 – part a Stream refer to any source of input or any destination for output. Many small programs, obtain all their input from one stream usually.
Lecture 9 Shell Programming – Command substitution
Unix Scripting Session 4 March 27, 2008.
CSE 303 Concepts and Tools for Software Development
Writing Shell Scripts ─ part 3
Agenda Control Flow Statements Purpose test statement
What is Bash Shell Scripting?
I/O in C Lecture 6 Winter Quarter Engineering H192 Winter 2005
Chapter Four UNIX File Processing.
More advanced BASH usage
Shell Programming.
Linux Shell Script Programming
Lab 4: Introduction to Scripting
Introduction to Bash Programming, part 3
LPI Linux Certification
Presentation transcript:

CST8177 Scripting 2: What?

What can you do in a script? 1. A script can run any program or script, anything that's executable. If it's a command-line program (CLI) the script can interact with the program or act as a filter to it for the keyboard user. If it's a graphical application (GUI), the script can set its start-up arguments but cannot interact with it or mediate its interaction with the user. In either case, the script can examine its exit value and report on that, to the screen or to the system log as appropriate. The script can also intercept failures of the program, restart it, shut- down gracefully, or ask the keyboard user what to do.

What can you do in a script? 2. A script can read input, examine it, and make a decision about what to do based on it. The input can be from any text file (binary files are impractical) that the user has at least read access to. Or the input can be from stdin, directly from the keyboard user. There are, as is usual in Linux/Unix, several ways to actually get the input, and selection of the method may make a difference to the operation of the script. There is a read service built-into the bash shell which is often used. Shell (sh) is often more complicated to use, but it's still possible.

What can you do in a script? 3. A script has many ways to write output. It can write to any text file (again, binary is impractical) using redirection either from another command or program or the echo and printf statements. This includes, of course, stdout and stderr as long as the script is running in the foreground. A script may also write to the system log files using the logger command. This is most useful for a script running in the background or as a daemon, when there is no access to any sort of console or terminal. Many regular commands also have a way to enable or disable normal output. As an example, grep has the -q, --quiet, and --silent options for stdout, as well as the -s or --no-messages options for most of stderr. For some commands and situations, redirecting output to /dev/null must be used.

What can you do in a script? 4. A script can calculate numbers using normal BEDMAS priorities (brackets, exponentiation, division, multiplication, addition, subtraction) following the rules for integer numbers *. You can use both constants and variables, including system variables and command-line arguments. You can manipulate strings and sub-strings with simple parameters, converting freely between strings and numbers. You can combine these with your own configuration files (and those of other programs) and use other techniques to set default values. * Integer arithmetic has no fractional values. Any fractional portion created is simply discarded at once. Thus ( ( 1 / 2 ) * 2 ) gives a result of 0, not 1.

What can you do in a script? 5. A script can control the sequence of execution of its own statements. It can compare numbers or strings, test file and directory status, search for strings and regexps, using the result to decide which statements to execute and which to skip. A script can process a group of commands repeatedly, in one of several varieties of loops. It can do this by a numeric count, by the result of another command, or by a condition or event. It can handle different cases for values, using different statements for each individual case. A script can even trap signals and choose how to proceed, ignoring any signal except SIGKILL if that's what is required.

What can you do in a script? 6. A script can decide when to terminate itself; it does not have to run through to the end. The termination can be early, in response to an error, a failure, or a detected condition. The termination can be at the end of processing input from a file or from the keyboard, or from a list of command-line arguments. It can even decide to run through to the end, if that is what's required. The termination normally has an exit value that can be tested by subsequent statements, just as a script can test the termination status of any command or program.

Sample Script #! /bin/bash # create variables with initial values declare n=$# declare count=0 # show how many command arguments there are echo Number of arguments: $n # for each command-line argument, repeat while (( $count <= $n )) do # show the arg number and its |value| eval "echo arg $count '|'\${$count}'|'" # count each argument processed let "count += 1" # end loop done echo all arguments evaluated exit 0