Systems Programming & Scripting



Similar documents
Bash shell programming Part II Control statements

BASH Scripting. A bash script may consist of nothing but a series of command lines, e.g. The following helloworld.sh script simply does an echo.

Unix Shell Scripts. Contents. 1 Introduction. Norman Matloff. July 30, Introduction 1. 2 Invoking Shell Scripts 2

Introduction to Shell Scripting

CS Unix Tools & Scripting Lecture 9 Shell Scripting

Lecture 4: Writing shell scripts

Introduction to Shell Programming

Shell Scripts (1) For example: #!/bin/sh If they do not, the user's current shell will be used. Any Unix command can go in a shell script

Advanced Bash Scripting. Joshua Malone

Unix Scripts and Job Scheduling

Answers to Even-numbered Exercises

A Crash Course on UNIX

Computational Mathematics with Python

Computational Mathematics with Python

BASH scripting. Sebastian von Alfthan Scripting techniques CSC Tieteen tietotekniikan keskus Oy CSC IT Center for Science Ltd.

Computational Mathematics with Python

We will learn the Python programming language. Why? Because it is easy to learn and many people write programs in Python so we can share.

CS2720 Practical Software Development

Lecture 25 Systems Programming Process Control

Linux Shell Script To Monitor Ftp Server Connection

Lecture 22 The Shell and Shell Scripting

Automating admin tasks using shell scripts and cron Vijay Kumar Adhikari.

Sources: On the Web: Slides will be available on:

Perl in a nutshell. First CGI Script and Perl. Creating a Link to a Script. print Function. Parsing Data 4/27/2009. First CGI Script and Perl

Informatica e Sistemi in Tempo Reale

ESCI 386 IDL Programming for Advanced Earth Science Applications Lesson 6 Program Control

AN INTRODUCTION TO UNIX

Computer Systems II. Unix system calls. fork( ) wait( ) exit( ) How To Create New Processes? Creating and Executing Processes

PHP Tutorial From beginner to master

Beginners Shell Scripting for Batch Jobs

HP-UX Essentials and Shell Programming Course Summary

Introduction to Python

Hadoop Hands-On Exercises

Top 72 Perl Interview Questions and Answers

Classes and Objects in Java Constructors. In creating objects of the type Fraction, we have used statements similar to the following:

Introduction to Grid Engine

grep, awk and sed three VERY useful command-line utilities Matt Probert, Uni of York grep = global regular expression print

Chapter 15 Functional Programming Languages

Command Line - Part 1

Exercise 1: Python Language Basics

VHDL Test Bench Tutorial

Programming Database lectures for mathema

Forms, CGI Objectives. HTML forms. Form example. Form example...

Introduction to Java

Hands-On UNIX Exercise:

Hadoop Hands-On Exercises

Command Line Crash Course For Unix

It has a parameter list Account(String n, double b) in the creation of an instance of this class.

Programming Exercises

Example of a Java program

Learn Perl by Example - Perl Handbook for Beginners - Basics of Perl Scripting Language

Object-Oriented Design Lecture 4 CSU 370 Fall 2007 (Pucella) Tuesday, Sep 18, 2007

Basic C Shell. helpdesk@stat.rice.edu. 11th August 2003

CS177 MIDTERM 2 PRACTICE EXAM SOLUTION. Name: Student ID:

ESPResSo Summer School 2012

Sample CSE8A midterm Multiple Choice (circle one)

Government Girls Polytechnic, Bilaspur

TOPICS IN COMPUTER SECURITY

ASCII Encoding. The char Type. Manipulating Characters. Manipulating Characters

Bash Tutorial. Eigil Obrestad and Erik Hjelmås. August 18, 2015

SUMMARY. Satellite is an application Two Sigma wrote to monitor, alert, and auto-administer our Mesos clusters.

UNIX, Shell Scripting and Perl Introduction

CS 141: Introduction to (Java) Programming: Exam 1 Jenny Orr Willamette University Fall 2013

An Introduction to the Linux Command Shell For Beginners

1001ICT Introduction To Programming Lecture Notes

Embedded Systems. Review of ANSI C Topics. A Review of ANSI C and Considerations for Embedded C Programming. Basic features of C

Invitation to Ezhil : A Tamil Programming Language for Early Computer-Science Education 07/10/13

Lab 1: Introduction to C, ASCII ART and the Linux Command Line Environment

SFTP SHELL SCRIPT USER GUIDE

How To Script Administrative Tasks In Marklogic Server

Working with forms in PHP

Basics of I/O Streams and File I/O

Computers. An Introduction to Programming with Python. Programming Languages. Programs and Programming. CCHSG Visit June Dr.-Ing.

Translating to Java. Translation. Input. Many Level Translations. read, get, input, ask, request. Requirements Design Algorithm Java Machine Language

Using Loops to Repeat Code

High-Level Programming Languages. Nell Dale & John Lewis (adaptation by Michael Goldwasser)

NLP Programming Tutorial 0 - Programming Basics

Systems Programming & Scripting

3 Pillars of Object-oriented Programming. Industrial Programming Systems Programming & Scripting. Extending the Example.

PYTHON Basics

Using SVN to Manage Source RTL

Linux command line. An introduction to the Linux command line for genomics. Susan Fairley

Passing 1D arrays to functions.

13 Classes & Objects with Constructors/Destructors

Programming Languages CIS 443

CP Lab 2: Writing programs for simple arithmetic problems

Moving from CS 61A Scheme to CS 61B Java

Welcome to Introduction to programming in Python

Advanced PostgreSQL SQL Injection and Filter Bypass Techniques

Chulalongkorn University International School of Engineering Department of Computer Engineering Computer Programming Lab.

Setting Up the Site Licenses


Optimizing Help Desk Size

Building Java Programs

CLC Server Command Line Tools USER MANUAL

Member Functions of the istream Class

Exercise 4 Learning Python language fundamentals

1998. (R. Bird and P. Wadler, Introduction to Functional Programming, Prentice

L04 C Shell Scripting - Part 2

Linux Syslog Messages in IBM Director

Transcription:

Systems Programming & Scripting Lecture 14 - Shell Scripting: Control Structures, Functions Syst Prog & Scripting - Heriot Watt University 1

Control Structures Shell scripting supports creating more complex programs using control structures. Decision: if statement Iteration (looping): while/until statement for statement Syst Prog & Scripting - Heriot Watt University 2

The if statement if command then statement else statement fi Similar in behaviour to the if statement in other programming languages with few differences: The command is executed which could be a script. Based on the return value, the statement after the then or else get executed. Syst Prog & Scripting - Heriot Watt University 3

The if statement (cont'd) Return value 0 is true and any other value is considered to be false. if script1; then ls l else echo script1 returned false fi Syst Prog & Scripting - Heriot Watt University 4

The test Command The if statement is used to run executable. To use the if statement to only compare two values, it is required to run a program to do that. The test program is provided by the operating system for this purpose. The test program is invoked by running [, a symbolic link to /bin/test. Syst Prog & Scripting - Heriot Watt University 5

Example #! /bin/sh FIRST_ARGUMENT= $1 if [ x$first_argument = xhans ]; then echo Hello Hans, good to see you else echo Hello World fi Syst Prog & Scripting - Heriot Watt University 6

Example Explained Reading from standard input Sending a message to standard output based on input. The test program is called to compare input with Hans Note spaces after [ and before ]. Note spaces before and after =. Those spaces are required. Syst Prog & Scripting - Heriot Watt University 7

Example Explained (cont'd) Two arguments in the comparison statement are proceeded by an x To take into account when FIRST_ARGUMENT has no value. Could also use double quote marks to solve the empty variable problem. Syst Prog & Scripting - Heriot Watt University 8

Example #!/bin/bash # copy a file, creating a backup if the target file exists if [ $# -lt 2 ] then echo "Usage: $0 <fromfile> <tofile>" exit 1 fi if [ -f $2 ] then mv $2 $2.bak fi cp $1 $2 Syst Prog & Scripting - Heriot Watt University 9

Example Explained The first conditional checks whether enough arguments are supplied. The second conditional checks whether the target file already exists. If so, it is moved to a.bak file Then the source file is copied to the target file. Syst Prog & Scripting - Heriot Watt University 10

Comparison Operators is equal to: if [ "$a" -eq "$b" ] Is not equal to: if [ "$a" -ne "$b" ] Is greater than: if [ "$a" -gt "$b" ] Is greater than or equal to: if [ "$a" -ge "$b" ] Other integer operators: -lt, -le String operators: =, ==,!=, <, >, -z (string is null) Syst Prog & Scripting - Heriot Watt University 11

The expr Command Performs string comparison and integer mathematics. Takes a number of arguments, each representing a token from the expression to be evaluated. (2*5) + 7 expr ( 2 * 5 ) + 7 To compare two string alphabetically expr hello < why Syst Prog & Scripting - Heriot Watt University 12

The while Statement while command; do statement done The while statement block is between while and done. Similar to the if statement, the command (or script) is executed. The statement will be executed as long as the command evaluates to true. The test (bracket) command is commonly used as with the if statement. Syst Prog & Scripting - Heriot Watt University 13

Example while [ $*!= ] do echo value is $1 shift done Source: http://lowfatlinux.com/linux-script-looping.html Prints arguments passed to the script while the input argument string is not null. The shift command moves the values stored in the command line variables to the left one position. Syst Prog & Scripting - Heriot Watt University 14

The until Statement until command do statement done Differ from the while statement in that its body is executed as long as the condition is false. Syst Prog & Scripting - Heriot Watt University 15

Example count=1 until [ $* = ] do echo value number $count $1 shift count=$[ $count + 1 ] # count= expr $count + 1 done Source: http://lowfatlinux.com/linux-script-looping.html Syst Prog & Scripting - Heriot Watt University 16

The for Statement for item in list do statement done Similar behaviour to foreach statement in other programming languages. Iterates through each item in a list Typically statement mentions $item Syst Prog & Scripting - Heriot Watt University 17

Example for item in $* do echo value is $item done Source: http://lowfatlinux.com/linux-script-looping.html In each iteration, the value of item is assigned the nth item in the list. The loop terminates when all items in the list are processed. Syst Prog & Scripting - Heriot Watt University 18

Functions functionname () { } statement Functions in shell-scripts are very restricted The () indicates that this is a function, but it is always empty. Arguments are referred to in the body of the function as $1, $2 etc A function may return an integer value as the return code Syst Prog & Scripting - Heriot Watt University 19

A Larger Example #!/bin/bash # From: Advanced Bash-Scripting Guide # http://tldp.org/ldp/abs/html/ # Arabic number to Roman numeral conversion # Usage: roman number-to-convert LIMIT=200 E_ARG_ERR=65 E_OUT_OF_RANGE=66 if [ -z "$1" ] then echo "Usage: `basename $0` number-to-convert" exit $E_ARG_ERR fi num=$1 if [ "$num" -gt $LIMIT ] then echo "Out of range!" exit $E_OUT_OF_RANGE Syst Prog & Scripting - Heriot Watt University 20 fi

A Larger Example (cont'd) to_roman () # Must declare function before # first call to it. { number=$1 factor=$2 rchar=$3 let "remainder = number - factor" while [ "$remainder" -ge 0 ] do echo -n $rchar let "number -= factor" let "remainder = number - factor" done return $number } Syst Prog & Scripting - Heriot Watt University 21

A Larger Example (cont'd) to_roman $num 100 C num=$? to_roman $num 90 LXXXX num=$? to_roman $num 50 L num=$? to_roman $num 40 XL num=$? to_roman $num 10 X num=$? to_roman $num 9 IX num=$? to_roman $num 5 V num=$? to_roman $num 4 IV num=$? to_roman $num 1 I echo exit Syst Prog & Scripting - Heriot Watt University 22

Further Reading Arnold Robbins, Classic Shell Scripting: Hidden Commands that Unlock the Power of Unix, O Reilly, 2005. My web page on programming languages: http://www.macs.hw.ac.uk/~hwloidl/prg-lang.html#sc Mendel Cooper Advanced Bash Scripting Guide, http://tldp.org/ldp/abs/html/index.html Bash Reference Manual, http://www.gnu.org/software/bash/manual/bashref.ht On-line Unix and Shell tutorials http://www.cyberciti.biz/tips/linux-unix-commands-cheat-she Syst Prog & Scripting - Heriot Watt University 23

Exercises In the roman numeral example, extend the range of numbers that can be covered. In the line count example, count the number of lines of files that have been changed within the last n days, or files owned by yourself. Work through the exercises in Chapters 10-12, 23, 26 of the Advanced Bash-Scripting Guide. Syst Prog & Scripting - Heriot Watt University 24