Writing Shell Scripts part 1

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

Command Line - Part 1

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

CS 170, Section 000, Fall 2009

CS Unix Tools & Scripting Lecture 9 Shell Scripting

HP-UX Essentials and Shell Programming Course Summary

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

Unix Scripts and Job Scheduling

An Introduction to Using the Command Line Interface (CLI) to Work with Files and Directories

Unix Sampler. PEOPLE whoami id who

A Crash Course on UNIX

Hands-On UNIX Exercise:

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

CS 2112 Lab: Version Control

Beginners Shell Scripting for Batch Jobs

LSN 10 Linux Overview

SFTP SHELL SCRIPT USER GUIDE

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

Extreme computing lab exercises Session one

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.

An Introduction to the Linux Command Shell For Beginners

Answers to Even- Numbered Exercises

UNIX, Shell Scripting and Perl Introduction

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

CS2720 Practical Software Development

1001ICT Introduction To Programming Lecture Notes

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

A UNIX/Linux in a nutshell

Lecture 4: Writing shell scripts

UNIX / Linux commands Basic level. Magali COTTEVIEILLE - September 2009

Bash shell programming Part II Control statements

CS 103 Lab Linux and Virtual Machines

This presentation explains how to monitor memory consumption of DataStage processes during run time.

UNIX Basics. Ian Darwin TCP Informatics January, Presented from a Mac using Apple s Keynote presentation software

AN INTRODUCTION TO UNIX

Hadoop Hands-On Exercises

New Lab Intro to KDE Terminal Konsole

Hadoop Shell Commands

Hadoop Shell Commands

Introduction to Shell Scripting

Introduction to Running Hadoop on the High Performance Clusters at the Center for Computational Research

Answers to Even-numbered Exercises

Lecture 25 Systems Programming Process Control

Lecture 18 Regular Expressions

LECTURE-7. Introduction to DOS. Introduction to UNIX/LINUX OS. Introduction to Windows. Topics:

Extreme computing lab exercises Session one

USEFUL UNIX COMMANDS

Command Line Crash Course For Unix

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

Introduction to Shell Programming

Linux System Administration on Red Hat

Unix Shell Scripting Tutorial Ashley J.S Mills

Setting Up the Site Licenses

Tutorial 0A Programming on the command line

How To Configure the Oracle ZFS Storage Appliance for Quest Authentication for Oracle Solaris

CLC Server Command Line Tools USER MANUAL

Offline Image Viewer Guide

LPI certification 101 exam prep, Part 1

1 Basic commands. 2 Terminology. CS61B, Fall 2009 Simple UNIX Commands P. N. Hilfinger

ICS 351: Today's plan

Thirty Useful Unix Commands

Introduction to Unix Tutorial

Systems Programming & Scripting

Korn Shell Script Writing

PetaLinux SDK User Guide. Application Development Guide

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

Secure Shell Demon setup under Windows XP / Windows Server 2003

L04 C Shell Scripting - Part 2

Welcome to Introduction to programming in Python

IBM Redistribute Big SQL v4.x Storage Paths IBM. Redistribute Big SQL v4.x Storage Paths

University of Hull Department of Computer Science. Wrestling with Python Week 01 Playing with Python

Bash Guide for Beginners

Installing Java. Table of contents

HDFS File System Shell Guide

Monitoring a Linux Mail Server

Secure File Transfer Installation. Sender Recipient Attached FIles Pages Date. Development Internal/External None 11 6/23/08

How To Use The Librepo Software On A Linux Computer (For Free)

PKZIP 6.0 Command Line Getting Started Manual

File System Shell Guide

STEP 4 : GETTING LIGHTTPD TO WORK ON YOUR SEAGATE GOFLEX SATELLITE

Setting up PostgreSQL

Basic Linux & Package Management. Original slides from GTFO Security

L01 Introduction to the Unix OS

Hadoop Hands-On Exercises

embeo Getting Started and Samples

The Linux Operating System and Linux-Related Issues

Lecture 22 The Shell and Shell Scripting

Table of Contents. V. UPGRADING LSTC License Manager Software for Microsoft Windows A. Run the Installation Program B. Verify the Installation

Introduction to Linux and Cluster Basics for the CCR General Computing Cluster

Programming for GCSE Topic H: Operating Systems

WebPublish User s Manual

CS 1133, LAB 2: FUNCTIONS AND TESTING

Automating FTP with the CP IT

Using SAS to Control and Automate a Multi SAS Program Process. Patrick Halpin November 2008

Fundamentals of UNIX Lab Networking Commands (Estimated time: 45 min.)

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

Running Hadoop on Windows CCNP Server

Recommended File System Ownership and Privileges

Tour of the Terminal: Using Unix or Mac OS X Command-Line

Command-Line Operations : The Shell. Don't fear the command line...

Transcription:

Writing Shell Scripts part 1 CSE 2031 Fall 2011 13 November 2011 1 What Is a Shell? A program that interprets ets your request to run other programs Most common Unix shells: Bourne shell (sh) C shell (csh) Korn shell (ksh) Bourne-again shell (bash) In this course we focus on Bourne shell (sh). 2 1

The Bourne Shell A high level programming language Processes groups of commands stored in files called scripts Includes variables control structures processes signals 3 Executable Files Contain one or more shell commands. These files can be made executable. # indicates a comment Except on line 1 when followed by an! % cat welcome echo Hello World! 4 2

Executable Files: Example % cat welcome echo Hello World! % welcome welcome: execute permission denied % chmod u+x welcome % ls -l welcome -rwxr--r-- 1 lan grad 20 Aug 29 2010 welcome % welcome Hello World! % welcome > greet_them % cat greet_them Hello World! 5 Executable Files (cont.) If the file is not executable, use sh followed by the file name to run the script. Example: % chmod u-x welcome % ls -l welcome 1 l d 20 A 29 2010 l rw-r--r-- 1 lan grad 20 Aug 29 2010 welcome % sh welcome Hello World! 3

Processes Consider the welcome program. 7 Processes: Explanation Every program is a child of some other program. Shell fires up a child shell to execute script. Child shell fires up a new (grand)child process for each command. Shell (parent) sleeps while child executes. Every process (executing a program) has a unique PID. Parent does not sleep while running background processes. 8 4

Variables: Three Types Standard UNIX variables Consist of shell variables and environment variables. Used to tailor the operating environment to suit your needs. Examples: TERM, HOME, PATH To display your environment variables, type set. User variables: variables you create yourself. Positional parameters Also called read-only variables, automatic variables. Store the values of command-line arguments. 9 User Variables Each variable has two parts: a name a value Syntax: name=value No space around the equal sign! All shell variables store strings (no numeric values). Variable name: combinations of letters, numbers, and underscore character ( _ ) that do not start with a number. Avoid existing commands and environment variables. Shell stores and remembers these variables and supplies value on demand. 10 5

User Variables (2) These are variables you, the user, create, read and change. To use a variable: $varname Variable substitution operator $ tells the shell to substitute the value of the variable name. dir=/usr/include/ echo $dir echo dir ls $dir grep ma Output: /usr/include/ / / dir malloc.h math.h numa.h semaphore.h 11 echo and Variables What if I d want to display the following? $dir Two ways to prevent variable substitution: echo $dir echo \$dir Note: echo $dir does the same as echo $dir 12 6

Command Line Arguments Command line arguments stored in variables called positional parameters. These parameters are named $1 through $9. Command itself is in parameter $0. In diagram format: command arg1 arg2 arg3 arg4 arg5 arg6 arg7 arg8 arg9 $0 $1 $2 $3 $4 $5 $6 $7 $8 $9 Arguments not present get null (absence of) value 13 Example 1 % cat display_args echo First four arguments from the echo command line are: $1 $2 $3 $4 % display_args William Mary Richard James First four arguments from the command line are: William Mary Richard James 14 7

Example 2 % cat chex # Make a file executable chmod u+x $1 echo $1 is now executable: ls l $1 % sh chex chex chex is now executable: -rwx------ 1 utn faculty 86 Nov 12 11:34 chex % chex showargs showargs is now executable: -rwx------ 1 utn faculty 106 Nov 2 14:26 showargs 15 Command Line Arguments (2) A macro is a stand-in for one or more variables $# represents the number of command line arguments $* represents all the command line arguments $@ represents all the command line arguments % cat check_args echo There are $# arguments. echo All the arguments are: $* # or echo All the arguments are: $@ % check_args Mary Tom Amy Tony There are 4 arguments. All the arguments are: Mary Tom Amy Tony 16 8

Command Line Arguments (3) Note: $# does NOT include the program name (unlike argc in C programs) What if the number of arguments is more than 9? How to access the 10 th, 11 th, etc.? Use shift operator. 17 shift Operator shift promotes each argument one position to the left. Operates as a conveyor belt. Allows access to arguments beyond $9. shifts contents of $2 into $1 shifts contents of $3 into $2 shifts contents of $4 into $3 etc. Eliminates argument(s) positioned immediately after the command. Syntax: shift # shifting arguments one position to the left After a shift, the argument count stored in $# is automatically decremented by one. 18 9

Example 1 % cat args echo "arg1 = $1, arg8 = $8, arg9 = $9, ARGC = $#" myvar=$1 # save the first argument shift echo "arg1 = $1, arg8 = $8, arg9 = $9, ARGC = $#" echo "myvar = $myvar % args 1 2 3 4 5 6 7 8 9 10 11 12 arg1 = 1, arg8 = 8, arg9 = 9, ARGC = 11 arg1 = 2, arg8 = 9, arg9 = 10, ARGC = 10 myvar = 1 19 Example 2 % cat show_shift echo arg1=$1, arg2=$2, arg3=$3 shift echo arg1=$1, arg2=$2, arg3=$3 shift echo arg1=$1, arg2=$2, arg3=$3 % show_shift William Richard Elizabeth arg1=william, arg2=richard, arg3=elizabeth arg1=richard, arg2=elizabeth, arg3= arg1=elizabeth, arg2=, arg3= 20 10

Example 3 % my_copy dir_name filename1 filename2 filename3 # This shell script copies all the files to directory dir_name % cat my_copy # Script allows user to specify, as the 1 st argument, # the directory where the files are to be copied. location=$1 shift files=$* cp $files $location 21 Shifting Multiple Times Shifting arguments three positions: 3 ways to write it shift shift shift shift; shift; shift shift 3 22 11

User Variables and Quotes name=value If value contains no space no need to use quotes dir=/usr/include/ echo $dir unless you want to protect the literal, in which case use single quotes. % cat quotes # Test values with quotes myvar1=$100 myvar2='$100' echo The price is $myvar1 echo The price is $myvar2 % quotes 5000 The price is 500000 The price is $100 23 User Variables and Quotes (2) If value contains one or more spaces: use single quotes for NO interpretation of metacharacters (protect the literal) use double quotes for interpretation of metacharacters % cat quotes myvar=`whoami` squotes='today is `date`, $myvar.' dquotes="today is `date`, $myvar." echo $squotes echo $dquotes % quotes Today is `date`, $myvar. Today is Fri Nov 12 12:07:38 EST 2010, cse12345. 24 12

Example % cat my_script dirs= /usr/include/ /usr/local/ echo $dirs ls -l $dirs # need single quotes % my_script /usr/include/ /usr/local/ /usr/include/: total 2064 -rw-r--r-- 1 root root 5826 Feb 21 2005 FlexLexer.h drwxr-xr-x 2 root root 4096 May 19 05:39 GL... /usr/local/: total 72 drwxr-xr-x 2 root root 4096 Feb 21 2005 bin drwxr-xr-x 2 root root 4096 Feb 21 2005 etc... 25 Reading User Input Reads from standard input. Stores what is read in user variable. Waits for the user to enter something followed by <RETURN>. Syntax: read varname # no dollar sign $ To use the input: echo $varname 26 13

Example 1 % cat greeting echo n Enter your name: read name echo Hello, $name. How are you today? % readit Enter your name: Jane Hello, Jane. How are you today? 27 Example 2 % cat doit echo n Enter a command: read command $command echo I m done. Thanks % doit Enter a command: ls lab* lab1.c lab2.c lab3.c lab4.c lab5.c lab6.c I m done. Thanks % doit Enter a command: who lan pts/200 Sep 1 16:23 (indigo.cs.yorku.ca) jeff pts/201 Sep 1 09:31 (navy.cs.yorku.ca) anton pts/202 Sep 1 10:01 (red.cs.yorku.ca) I m done. Thanks 28 14

Reading User Input (2) More than one variable may be specified. Each word will be stored in separate variable. If not enough variables for words, the last variable stores the rest of the line. 29 Example 3 % cat read3 echo Enter some strings: read string1 string2 string3 echo string1 is: $string1 echo string2 is: $string2 echo string3 is: $string3 % read3 Enter some strings: This is a line of words string1 is: This string2 is: is string3 is: a line of words 30 15

Next time Control structures (if, for, while, ) Difference between $* and $@ Shell variables Reading for this lecture: posted notes (chapter 33) 31 16