CHAPTER 3 SHELL PROGRAMS: SCRIPTS

Similar documents
CHAPTER 2 THE UNIX SHELLS

Assignment clarifications

Shells. A shell is a command line interpreter that is the interface between the user and the OS. The shell:

UNIX shell scripting

Table of contents. Our goal. Notes. Notes. Notes. Summer June 29, Our goal is to see how we can use Unix as a tool for developing programs

5/8/2012. Specifying Instructions to the Shell Chapter 8

Introduction to the Shell

Writing Shell Scripts part 1

Essential Unix (and Linux) for the Oracle DBA. Revision no.: PPT/2K403/02

CISC 220 fall 2011, set 1: Linux basics

5/20/2007. Touring Essential Programs

EECS 2031E. Software Tools Prof. Mokhtar Aboelaze

Bash Programming. Student Workbook

3/8/2017. Unix/Linux Introduction. In this part, we introduce. What does an OS do? Examples

Operating Systems. Copyleft 2005, Binnur Kurt

22-Sep CSCI 2132 Software Development Lecture 8: Shells, Processes, and Job Control. Faculty of Computer Science, Dalhousie University

Operating Systems 3. Operating Systems. Content. What is an Operating System? What is an Operating System? Resource Abstraction and Sharing

Appendix A GLOSSARY. SYS-ED/ Computer Education Techniques, Inc.

Standard. Shells. tcsh. A shell script is a file that contains shell commands that perform a useful function. It is also known as shell program.

Shells and Shell Programming

SOFTWARE ARCHITECTURE 3. SHELL

Lezione 8. Shell command language Introduction. Sommario. Bioinformatica. Mauro Ceccanti e Alberto Paoluzzi

Operating Systems, Unix Files and Commands SEEM

Introduction to Shell Scripting

Today. Review. Unix as an OS case study Intro to Shell Scripting. What is an Operating System? What are its goals? How do we evaluate it?

Lezione 8. Shell command language Introduction. Sommario. Bioinformatica. Esercitazione Introduzione al linguaggio di shell

Examples: Directory pathname: File pathname: /home/username/ics124/assignments/ /home/username/ops224/assignments/assn1.txt

S E C T I O N O V E R V I E W

UNIX Shell Scripts. What Is a Shell? The Bourne Shell. Executable Files. Executable Files: Example. Executable Files (cont.) CSE 2031 Fall 2012

Implementation of a simple shell, xssh

Chapter 9. Shell and Kernel

CS 307: UNIX PROGRAMMING ENVIRONMENT KATAS FOR EXAM 1

Processes and Shells

Shells and Shell Programming

The Online Unix Manual

Unix Internal Assessment-2 solution. Ans:There are two ways of starting a job in the background with the shell s & operator and the nohup command.

Implementation of a simple shell, xssh

System Programming. Unix Shells

Lecture 3. Unix. Question? b. The world s best restaurant. c. Being in the top three happiest countries in the world.

Linux & Shell Programming 2014

ACS Unix (Winter Term, ) Page 21

Introduction: What is Unix?

MTU Computer Structure

Last Time. on the website

ACS Unix (Winter Term, ) Page 92

The UNIX Shells. Computer Center, CS, NCTU. How shell works. Unix shells. Fetch command Analyze Execute

Essentials for Scientific Computing: Bash Shell Scripting Day 3

I/O and Shell Scripting

Introduction to the UNIX command line

Welcome to Linux. Lecture 1.1

CSC209H Lecture 1. Dan Zingaro. January 7, 2015

CS246 Spring14 Programming Paradigm Notes on Linux

More on Unix Shell SEEM

CHAPTER 1 UNIX FOR NONPROGRAMMERS

Introduction to Linux

Unix as a Platform Exercises + Solutions. Course Code: OS 01 UNXPLAT

Useful Unix Commands Cheat Sheet

bash, part 3 Chris GauthierDickey

Read the relevant material in Sobell! If you want to follow along with the examples that follow, and you do, open a Linux terminal.

CS 25200: Systems Programming. Lecture 10: Shell Scripting in Bash

Unix/Linux Basics. Cpt S 223, Fall 2007 Copyright: Washington State University

Introduction to UNIX. Logging in. Basic System Architecture 10/7/10. most systems have graphical login on Linux machines

System Programming. Introduction to Unix

M2PGER FORTRAN programming. General introduction. Virginie DURAND and Jean VIRIEUX 10/13/2013 M2PGER - ALGORITHME SCIENTIFIQUE

12.1 UNDERSTANDING UNIX SHELL PROGRAMMING LANGUAGE: AN INTRODUCTION Writing a Simple Script Executing a Script

Shell Scripting. With Applications to HPC. Edmund Sumbar Copyright 2007 University of Alberta. All rights reserved

Advanced Linux Commands & Shell Scripting

UNIX System Programming Lecture 3: BASH Programming

(a) About Unix. History

The Unix Shell & Shell Scripts

Chapter Two. Lesson A. Objectives. Exploring the UNIX File System and File Security. Understanding Files and Directories

Oxford University Computing Services. Getting Started with Unix

UNIX Essentials Featuring Solaris 10 Op System

Linux shell programming for Raspberry Pi Users - 2

CS 307: UNIX PROGRAMMING ENVIRONMENT FIND COMMAND

Topic 2: More Shell Skills

EECS2301. Lab 1 Winter 2016

Unix as a Platform Exercises. Course Code: OS-01-UNXPLAT

Introduction. The UNIX Operating System is divided into three major components:

CMPS 12A Introduction to Programming Lab Assignment 7

SECTION -C. Getting Started with UNIX

Crash Course in Unix. For more info check out the Unix man pages -orhttp:// -or- Unix in a Nutshell (an O Reilly book).

IBM AIX Basic Operations V5.

CS Unix Tools. Lecture 3 Making Bash Work For You Fall Hussam Abu-Libdeh based on slides by David Slater. September 13, 2010

Introduction to UNIX. Introduction EECS l UNIX is an operating system (OS). l Our goals:

Introduction to Linux Basics

Bourne Shell (ch 8) Overview. Bourne Shell. Bourne Shell. Bourne Shell. Bourne Shell. Redirect standard error. Redirect standard error

UNIX Concepts COMPSCI 386

Unix Shell Environments. February 23rd, 2004 Class Meeting 6

Part 1: Basic Commands/U3li3es

UNIX COMMANDS AND SHELLS. UNIX Programming 2015 Fall by Euiseong Seo

What is the Shell. Whenever you login to a Unix system you are placed in a program called the shell. All of your work is done within the shell.

S E C T I O N O V E R V I E W

Introduction p. 1 Who Should Read This Book? p. 1 What You Need to Know Before Reading This Book p. 2 How This Book Is Organized p.

Introduction to Linux Workshop 1

85321, Systems Administration Chapter 6: The shell

Topic 2: More Shell Skills. Sub-Topic 1: Quoting. Sub-Topic 2: Shell Variables. Difference Between Single & Double Quotes

Using LINUX a BCMB/CHEM 8190 Tutorial Updated (1/17/12)

UNIX Kernel. UNIX History

Sub-Topic 1: Quoting. Topic 2: More Shell Skills. Sub-Topic 2: Shell Variables. Referring to Shell Variables: More

Transcription:

CHAPTER 3 SHELL PROGRAMS: SCRIPTS Any series of commands may be stored inside a regular text file for later execution. A file that contains shell commands is called a script. Before you can run a script, you must give it execute permission by using chmod utility. Then to run the script, you only need to type its name. When a script is run, the kernel determines which shell the script was written for, and then executes the shell using the script as its standard input. The shell is determined as specified in the following. 1. if the first line is just a #, then the script is interpreted by the C shell, 2. if the first line is of the form #!pathname, then the script is interpreted by the shell specified by the pathname, 3. if neither rule 1 nor rule 2 applies, then the script is interpreted by the Bourne shell. cat >script.csh # file extension has no importance # this is a sample C shell script echo n the date today is date # output today s date ^D cat >script.ksh #!/bin/ksh # this is a sample Korn shell script echo n the date today is date # output today s date ^D chmod +x script.csh script.ksh # make them executable ls l script.csh script.ksh -rwxr-xr-x halici 84 May 3... script.csh -rwxr-xr-x halici 104 May 3... script.ksh script.csh the date today is Mon May 7 10:40:40 MEDT 2005 script.ksh the date today is Mon May 7 10:41:10 MEDT 2005 The csh and ksh extensions of these scripts are used only to clarity, they do not have any effect on the shell type. A script doesn t even need an extension. SUBSHELLS When you log into UNIX, it supplies you with an initial login shell. Any simple comand that you enter is excuted by this initial shell. However there are several circumstaces when your current (parent) shell created a new (child) shell to perform some tasks. 17

1) When a grouped command is executed (if not executed in background, the parent shell sleeps until the child shell terminates) 2) When a script is executed (if not executed in background, the parent shell sleeps until the child shell terminates) 3) When a background job is executed (the parent shell continues to run concurrently with the child shell) A child shell (subshell) has its own working directory, and so cd command executed in a subshell does not affect the working directory of the parent shell pwd /home122/halici (cd class; pwd)... the subshell moves to subdirectory and executes pwd /home122/halici/class pwd... my login shell not moved /home122/halici VARIABLES The shell supports two kind of variables 1) Local variables 2) Environment variables A child shell inherits a copy of is its parent s environment variables while has a clean local variable space. Parent shell data area Child shell data area Environment copy Environment copied from parent Local Local clean, initialised Every shell has a set of predefined environment variables that are initialised by the start up files. Similarly every shell has a set of local varibles that has special meanings to the shell and are particularly useful when writing scripts. 18

PREDEFINED ENVIRONMENT VARIABLES Some of the predefined environment varibles are listed below: HOME : The full pathname of your home directory PATH : A list of directories to search for commands MAIL : A full pathname of your mailbox USER : Your user id SHELL : The full pathname of your login shell TERM : The type of your terminal The syntax for assigning value to variables differs between shells, but the way to access a variable is the same. If you write followed by the name of a variable, this token sequence is replaced by the shell with the value of the named variable. echo HOME=HOME, PATH=PATH HOME=/home122/halici, PATH=.:/bin:/home122/bin echo MAIL=MAIL MAIL=/home122/spool/mail/halici echo USER=USER, SHELL=SHELL USER=halici, SHELL=/bin/sh, TERM=vt100 The syntax for assigning value to a varible in the Bourne and Korn shells is as follows: variable=value...place no spaces around = firstname=ugur # assign value to a local variable named firstname lastname=halici # assign value to local variable lastname echo firstname lastname # see their values Ugur Halici export lastname # lastname becomes environment variable sh # start a child shell echo firstname lastname # notice that firstname is not copied Halici lastname=newone # change lastname ^D #terminate child shell echo firstname lastname # parent shell remains unchanged, Ugur Halici 19

BUILT IN VARIABLES WITH SPECIAL MEANINGS The special meanings of some built in variables are listed below: : The process id of the shell 0 : The name of the shell script (if applicable) 1..9 : n refers to the nth command line argument (if applicable) * : a list of all the command line arguments cat script.sh echo the name of this script is 0 echo the first argument is 1 echo a list of all arguments is * echo this script places the date into a temporary file called 1. date >1. # redirect the output of date ls 1. # list the file rm 1. # remove the file script.sh ali cem can nur #execute the script with four arguments the name of this script is script.sh the first argument is ali a list of all arguments is ali cem can nur this script places the date into a temporary file called ali.28107 ali.28107 The shift shell command causes all of the positional parameters 2..n to be renamed as 1..(n-1) and 1 to be lost. If there are no positional arguments left to shift an error message is diplayed. cat shift.csh #!bin/csh echo first argument is 1, all args are * shift echo first argument is 1, all args are * shift.csh a b c d first argument is a, all args are a b c d first argument is b, all args are b c d shift.csh a first argument is a, all args are a first argument is, all args are shift.csh first argument is, all args are Shift: No more words... error message 20

QUOTING To inhibit wildcard replacement, and variable/command substitutions quoting is used as follows: - Single quotes ( ) inhibits wildcard replacement (*,?, [...]), variable substitution (), and command substitution ( `cmd` ), - Double quotes ( ) inhibits wildcard replacement only, - When quotes are nested, its only the outer quotes that have any effect ls a.c b.c cc.c echo 3 * 4 = 12 #remember * is a wildcard 3 a.c b.c cc.c 4 = 12 echo 3 * 4 = 12 3 * 4 = 12 name=ugur echo my name is name and the date is `date` my name is name and the date is `date` echo my name is name and the date is `date` my name is ugur and the date is Mon, May 11 10:41:10 MEDT 2005 HERE DOCUMENTS: <<label Here document facility is used to provide an immediate input to a command in script. The input starts just after <<label and ends at where label appears alone in a line. cat here.sh mail 1 <<EndOfText Dear 1 Please see me immediately! - USER EndOfText echo mail is sent to 1 here.sh halici mail is sent to halici mail # look at my mail &1 # read mail 1 From: Ugur Halici <halici@metu.edu.tr> To: halici@metu.edu.tr Dear halici Please see me immediately! - halici &q # quit out of mail 21

JOB CONTROL Some commands used for job control are listd below: ps : generates a list of processes and their attributes including their name, process id etc. kill : allows you to terminate a process based on its process id wait : allows a shell to wait for one of its child process to terminate sleep n : sleeps for the specified numbr of seconds and then terminates (sleep 10; echo done) & 27387 ps PID T STAT TIME COMMAND 27355 p3 S 0:00 -sh(sh) the login shell 27387 p3 S 0:00 22