Notes on Shell Programming

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

Unix Scripts and Job Scheduling

Bash shell programming Part II Control statements

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

HP-UX Essentials and Shell Programming Course Summary

Lecture 4: Writing shell scripts

UNIX, Shell Scripting and Perl Introduction

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

SFTP SHELL SCRIPT USER GUIDE

Systems Programming & Scripting

Introduction to Shell Scripting

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.

Beginners Shell Scripting for Batch Jobs

A Crash Course on UNIX

Bash Guide for Beginners

CS2720 Practical Software Development

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

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

CS Unix Tools & Scripting Lecture 9 Shell Scripting

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

Lecture 22 The Shell and Shell Scripting

PRI-(BASIC2) Preliminary Reference Information Mod date 3. Jun. 2015

Lecture 25 Systems Programming Process Control

Advanced Bash Scripting. Joshua Malone

Introduction to Grid Engine

Introduction to Shell Programming

Microsoft Windows PowerShell v2 For Administrators

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.

Introduction to Java

L04 C Shell Scripting - Part 2

Windows PowerShell Essentials

The Linux Operating System

Outline. Unix shells Bourne-again Shell (bash) Interacting with bash Basic scripting References


Informatica e Sistemi in Tempo Reale

AN INTRODUCTION TO UNIX

Exercise 4 Learning Python language fundamentals

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

ESPResSo Summer School 2012

Introduction to Python

Introduction to Matlab

Answers to Even-numbered Exercises

Command Line - Part 1

Hands-On UNIX Exercise:

Problem 1. CS 61b Summer 2005 Homework #2 Due July 5th at the beginning of class

VIP Quick Reference Card

32-Bit Workload Automation 5 for Windows on 64-Bit Windows Systems

How to test and debug an ASP.NET application

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

PYTHON Basics

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

Simple File Input & Output

Introduction. Chapter 1

An Introduction to Assembly Programming with the ARM 32-bit Processor Family

Tutorial on C Language Programming

Member Functions of the istream Class

Computational Mathematics with Python

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

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

9.1 SAS. SQL Query Window. User s Guide

Setting up PostgreSQL

LSN 10 Linux Overview

Boolean Expressions, Conditions, Loops, and Enumerations. Precedence Rules (from highest to lowest priority)

Information and Computer Science Department ICS 324 Database Systems Lab#11 SQL-Basic Query

CSI 402 Lecture 13 (Unix Process Related System Calls) 13 1 / 17

Vim, Emacs, and JUnit Testing. Audience: Students in CS 331 Written by: Kathleen Lockhart, CS Tutor

Changing Passwords in Cisco Unity 8.x

Programming Languages CIS 443

Basics of I/O Streams and File I/O

Preparing your data for analysis using SAS. Landon Sego 24 April 2003 Department of Statistics UW-Madison

Linux Shell Script To Monitor Ftp Server Connection

CPSC 2800 Linux Hands-on Lab #7 on Linux Utilities. Project 7-1

Unix the Bare Minimum

Computational Mathematics with Python

Computational Mathematics with Python

Basic Java Constructs and Data Types Nuts and Bolts. Looking into Specific Differences and Enhancements in Java compared to C

Drupal Survey. Software Requirements Specification /10/2009. Chris Pryor Principal Project Manager

Cleo Communications. CUEScript Training

Command Scripts Running scripts: include and commands

An Introduction to the Linux Command Shell For Beginners

Introduction to Perl

Outline. Conditional Statements. Logical Data in C. Logical Expressions. Relational Examples. Relational Operators

Illustration 1: Diagram of program function and data flow

Creating a Simple Macro

Korn Shell Script Writing

Python Programming: An Introduction to Computer Science

MATLAB Tutorial. Chapter 6. Writing and calling functions

Code Composer Studio s Command Window

IBM WebSphere Application Server Version 7.0

Free Java textbook available online. Introduction to the Java programming language. Compilation. A simple java program

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

CLC Server Command Line Tools USER MANUAL

1.1 AppleScript Basics

Free Java textbook available online. Introduction to the Java programming language. Compilation. A simple java program

Unix Sampler. PEOPLE whoami id who

JavaScript: Control Statements I

Scheduling in SAS 9.3

Cisco Networking Academy Program Curriculum Scope & Sequence. Fundamentals of UNIX version 2.0 (July, 2002)

Example of a Java program

Quick Start SAP Sybase IQ 16.0

Transcription:

Bourne shell (sh): #!/bin/sh Prompt: $ ($PS1) Variables Notes on Shell Programming Individual line arguments $0, $1,, $9 All line arguments $*, $@ Command line argument count $# Shell process ID $$ Last exit status $? Secondary line prompt $PS2 The If Construct if [ test options ] then else (or elif) fi (NOTE: else is optional) The Case Construct case value in pattern1) ;; pattern2) ;; (default ) esac *) ;; The While Construct while [ test options ] do done The Until Construct until [ test options ] do done The For Construct for var in word1 word2 wordn do done

NOTE: In the If, While, and Until constructs, a can be used in place of [ test options ] Special Commands Meaning exit n Terminates a shell script with exit status = n break Terminates a loop (while, until, for) continue Skip remaining steps in current loop cycle shift Move line arguments to the left (new $1 = old $2 and so on and so forth) : Null (has exit status = 0, i.e. TRUE) Test Options Operator Returns TRUE (exit status of 0) if Integer Operators int1 eq int2 int1 is equal to int2 int1 ge int2 int1 is greater than or equal to int2 int1 gt int2 int1 is greater than int2 int1 lt int2 int1 is less than int2 int1 le int2 int1 is less than or equal to int2 int1 ne int2 int1 is not equal to int2 String Operators str1 = str2 str1 is equal str2 str1!= str2 str1 is not equal to str2 string string is not null -n string string is not null (string must be seen by test) -z string string is null (string must be seen by test) File Operators -d file file is a directory -f file file is an ordinary file -r file file is readable by the process -s file file has nonzero length -w file file is writable by the process -x file file is executable Logical Operators (for compound test conditions) -a and -o or Example: [ $a eq 0 o $b lt 10 ] Logical Operators (for compound evaluation) && and i.e. 1 && 2 means 2 will be executed only if 1 returns exit status of 0 or i.e. 1 2 means 2 will be executed only if 1 returns exit status not equal to 0

Calculations Use the expr i.e. var = expr $var1 op $var2 where op is +, -, \*, /, % Korn Shell (ksh): superset of Bourne Shell: #!/bin/ksh Variables Individual line arguments $0, $1,, $9, ${10}, ${11}, $RANDOM Random number between 0 and 32767 $SECONDS Number of seconds since shell began $PS3 Prompt for the select statement NOTE: The rest is the same as the Bourne shell. Expressions Relational operators >, ==, <, <=, >=,!= Arithmetic operators +, -, *, /, %, ++, +=, -=, *=, /=, %= For advanced relational expressions, use [ [ ] ] or ( ( ) ). Variable Assignment String assignment Numerical assignment Same as the Bourne shell Let var=expression var=$( ( expression ) ) Variable Attributes typeset option var Variable Substrings ${varname:startpos:clength} expands to the substring of varname, starting from the character at startpos, for a length up to a maximum of clength characters Example: ${scatter:2:3} starts at position 2 and a substring length of 3 produces the substring cat Arrays set A arrayvar word_list typeset arrayvar[num_elements] or create dynamically arrayvar[pos] = value var=${arrayvar[${pos}]}

Special Commands alias unalias fc [-l] N Meaning Create new name for a builtin Remove new name for a builtin Fix [list] the for event N in the history Must be preceded by FCEDIT=/usr/bin/vi Command Line Editing Must be preceded by set o vi Press Esc button Use vi s on the current line Press Enter to execute the new Can press v to invoke the full screen vi editor, modify the existing line, and add additional lines Commands will be executed when you leave the editor Extended Pattern Matching *(p) +(p)?(p)!(p) @(p1 p2 ) Zero or more occurrences of pattern p One or more occurrences of pattern p Zero or one occurrences of pattern p Matches what does not match pattern p Matches patterns p1 or p2 or

C shell (csh): #!/bin/csh Prompt: % ($prompt) Variables Individual line arguments $0, $argv[1], $argv[2], All line arguments $argv[*] Command line argument count $#argv Shell process ID $$ Last exit status $status The If Construct if ( expr ) then else (or else if) endif (NOTE: else is optional) The Switch Construct The While Construct while ( expr ) end The Foreach Construct foreach var (wordlist) end switch ( string ) case pattern1: breaksw case pattern2: breaksw default: endsw

Expressions Relational operators >, ==, <, <=, >=,!= Arithmetic operators +, -, *, /, %, ++, +=, -=, *=, /=, %= Variable Assignment String assignment Numerical assignment set var = value @ var = expression Arrays set arrayvar = ( word_list ) set arrayvar[pos] = value $#arrayvar: the number of elements in the arrayvar Special Commands Meaning exit(n) Exit with status = n alias Create new name for a builtin unalias Remove new name for a builtin set history = 20 Turn on logging of 20 most recent s! N Re-execute event N from the history list pushd Stack directories popd Unstack directories set var = head l Read a variable from the keyboard