CTEC 1863 – Operating Systems Shell Scripting. CTEC1863 - 2009F2 Overview How shell works Command line parameters –Shift command Variables –Including.

Slides:



Advertisements
Similar presentations
CIS 240 Introduction to UNIX Instructor: Sue Sampson.
Advertisements

Introduction to Unix – CS 21 Lecture 11. Lecture Overview Shell Programming Variable Discussion Command line parameters Arithmetic Discussion Control.
Find Command Characteristics –Locate files descending from multiple starting points –Employs regular expressions Examples On entire system: >find / -name.
More about Shells1-1 More about Shell  Shells (sh, csh, ksh) are m Command interpreters Process the commands you enter m High-level programming languages.
CS 497C – Introduction to UNIX Lecture 26: - The Process Chin-Chih Chang
CS Lecture 03 Outline Sed and awk from previous lecture Writing simple bash script Assignment 1 discussion 1CS 311 Operating SystemsLecture 03.
Shell Basics CS465 - Unix. Shell Basics Shells provide: –Command interpretation –Multiple commands on a single line –Expansion of wildcard filenames –Redirection.
Now, return to the Unix Unix shells: Subshells--- Variable---1. Local 2. Environmental.
More Shell Basics CS465 - Unix. Unix shells User’s default shell - specified in /etc/passwd file To show which shell you are currently using: $ echo $SHELL.
Guide To UNIX Using Linux Third Edition
Introduction to Unix (CA263) Introduction to Shell Script Programming By Tariq Ibn Aziz.
Shell Programming 1. Understanding Unix shell programming language: A. It has features of high-level languages. B. Convenient to do the programming. C.
Bash Shell Scripting 10 Second Guide Common environment variables PATH - Sets the search path for any executable command. Similar to the PATH variable.
Shell Script Examples.
Shell Control Structures CSE 2031 Fall August 2015.
Second edition Your UNIX: The Ultimate Guide Das © 2006 The McGraw-Hill Companies, Inc. All rights reserved. UNIX – Shell Programming The activities of.
Advanced Shell Programming. 2 Objectives Use techniques to ensure a script is employing the correct shell Set the default shell Configure Bash login and.
Welcome to CSE  Name: Di Cao   Classroom: DL357  Class Time: T 8:30am - 9:18am  Office.
Introduction to Shell Script Programming
1 Operating Systems Lecture 3 Shell Scripts. 2 Shell Programming 1.Shell scripts must be marked as executable: chmod a+x myScript 2. Use # to start a.
Writing Shell Scripts ─ part 1 CSE 2031 Fall September 2015.
1 Operating Systems Lecture 3 Shell Scripts. 2 Brief review of unix1.txt n Glob Construct (metacharacters) and other special characters F ?, *, [] F Ex.
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.
Shell Scripting Todd Kelley CST8207 – Todd Kelley1.
July 17, 2003Serguei A. Mokhov, 1 Shells and Shell Scripts COMP 444/5201 Revision 1.3 January 25, 2005.
Shell Script Programming. 2 Using UNIX Shell Scripts Unlike high-level language programs, shell scripts do not have to be converted into machine language.
Introduction to Linux OS (IV) AUBG ICoSCIS Team Prof. Volin Karagiozov March, 09 – 10, 2013 SWU, Blagoevgrad.
Introduction to Bash Programming Ellen Zhang. Previous three classes What have we learnt so far ?
Writing Shell Scripts ─ part 3 CSE 2031 Fall October 2015.
Linux Operations and Administration
UNIX Shell Script (1) Dr. Tran, Van Hoai Faculty of Computer Science and Engineering HCMC Uni. of Technology
Course materials may not be reproduced in whole or in part without the prior written permission of IBM. 5.1 © Copyright IBM Corporation 2008 Unit 11: Shell.
CS465 - UNIX The Bourne Shell.
UNIX Commands. Why UNIX Commands Are Noninteractive Command may take input from the output of another command (filters). May be scheduled to run at specific.
Shell Programming. Introducing UNIX Shells  Shell is also a programming language and provides various features like variables, branching, looping and.
©Colin Jamison 2004 Shell scripting in Linux Colin Jamison.
1 Lecture 9 Shell Programming – Command substitution Regular expressions and grep Use of exit, for loop and expr commands COP 3353 Introduction to UNIX.
1 © 2000 John Urrutia. All rights reserved. Session 5 The Bourne Shell.
CSCI 330 UNIX and Network Programming Unit IX: Shell Scripts.
Lesson 3-Touring Utilities and System Features. Overview Employing fundamental utilities. Linux terminal sessions. Managing input and output. Using special.
Shell Control Statements and More
Environment After log in into the system, a copy of the shell is given to the user Shell maintains an environment which is distinct from one user to another.
Agenda Positional Parameters / Continued... Command Substitution Bourne Shell / Bash Shell / Korn Shell Mathematical Expressions Bourne Shell / Bash Shell.
CSCI 330 UNIX and Network Programming Unit III Shell, Part 1.
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 Managing Processes (Jobs) Command Grouping Running jobs in background (bg) Bringing jobs to foreground (fg), Background job status (jobs) Suspending.
Agenda The Bourne Shell – Part I Redirection ( >, >>,
CS 403: Programming Languages Lecture 20 Fall 2003 Department of Computer Science University of Alabama Joel Jones.
1 Lecture 7 Introduction to Shell Scripts COP 3353 Introduction to UNIX.
By Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
1 UNIX Operating Systems II Part 2: Shell Scripting Instructor: Stan Isaacs.
Shell Control Structures CSE 2031 Fall June 2016.
Bash Shell Scripting 10 Second Guide.
Implementation of a simple shell, xssh
Lecture 7 Introduction to Shell Programming
Lecture 7 Introduction to Shell Scripts COP 3353 Introduction to UNIX.
Implementation of a simple shell, xssh (Section 1 version)
Implementation of a simple shell, xssh
LINUX System : Lecture 5 (English-Only Lecture)
Lecture 9 Shell Programming – Command substitution
Writing Shell Scripts ─ part 3
Writing Shell Scripts ─ part 3
CSCI The UNIX System Shell Startup and Variables
JavaScript What is JavaScript? What can JavaScript do?
Linux Shell Script Programming
JavaScript What is JavaScript? What can JavaScript do?
Chapter 3 The UNIX Shells
CSC 4630 Meeting 4 January 29, 2007.
Introduction to Bash Programming, part 3
Presentation transcript:

CTEC 1863 – Operating Systems Shell Scripting

CTEC F2 Overview How shell works Command line parameters –Shift command Variables –Including predefined/specials vars Functions

CTEC F3 Shells sh is a command interpreter –It supports a powerful command language –Each invocation of the sh program is called a shell

CTEC F4 Shells (2) OS’s allow invoking commands one at a time from a terminal Shells allow users to: –Combine commands to form new commands –Pass positional parameters to a command –Add or rename commands –Execute commands within loops –Execute commands conditionally –Send commands into the background

CTEC F5 How it works:

CTEC F6 How it works: When you type a command: –Your shell makes a copy of itself through the fork system call (PID 230) –Your shell puts itself to sleep (via the wait system call) until its child process (PID 230) calls exit –The child process calls exec(date) to overwrite itself with the code for date –When the date command is finished, it calls exit() –The child process (230) dies, and your shell wakes up again, and displays the prompt

CTEC F7 Variables Spring into existence by being mentioned –May be assigned string values –Syntax: NAME=STRING –Thereafter "$NAME" will yield "STRING" –No spaces around the = sign!!!!! Double quotes around the right-hand side allow STRING to contain semicolons, spaces, tabs, and newlines

CTEC F8 Examples: $ echo NAME NAME $ NAME=Howard $ echo $NAME Howard $ HIS="/usr/user3" $ ls $HIS

CTEC F9 Positional Parameters Implicitly created when a shell script runs –These variables are known as: $0, $1,..., $9 Try this script: echo "Example of positional parameters" echo "$5" echo $1 echo $2 echo $3 $4 $ PARAMS These parameters are passed Also try changing “$5” to ‘$5’

CTEC F10 shift Used when more than 10 ($0-$9) parameters –After the shift command is executed, the positional parameters from $2... are renamed $1... E.g. if commandfile is invoked as: commandfile arg1 arg2 arg3 arg4 arg5 arg6 $0 $1 $2 $3 $4 $5 $6 After a shift command is executed: commandfile arg1 arg2 arg3 arg4 arg5 arg6 $0 gone $1 $2 $3 $4 $5

CTEC F11 Shift example Try this script: echo "Example of shift command" echo $1 shift echo $1 shift echo $1 With: $./shiftit shows shift command

CTEC F12 set command Forces values into the positional parameters For example: set -- abc def ghi is equivalent to: $1=abc $2=def $3=ghi Note: –Positional parameters are not allowed on the left hand side of an assignment statement. Try this script: set -- THESE THREE VALUES echo $1 $2 $3

CTEC F13 Shell Maintained Variables HOME: Upon login, the shell assigns a user's login directory IFS: Internal field separators, default is blank, tab, newline MAIL: Pathname of the file where your mail is deposited PATH: Search path used to find commands

CTEC F14 Special Variables $# $? $! $$ $- $# records the number of arguments passed to a shell procedure e.g. $ someproc x y z sets $# to 3 –Tell if correct number of args passed in

CTEC F15 Special Variables (2) $? contains the exit status of the last command executed –Zero indicates successful completion –Anything from 1 to 255 indicates failure Often only want to execute next command if last was successful $! contains the process number of the last process run in the background (using &)

CTEC F16 Special Variables (3) $$ contains the process number of the currently running process. –Process numbers are unique among all existing processes. –Often used if a shell procedure needs to create temporary files: … temp=$HOME/temp.$$ ls > $temp … rm $temp …

CTEC F 17 Special Variables (4) $- is string containing the names of all the flags currently turned on in the shell Also… –$* is not considered a special variable –Contains $1, $2,... separated by spaces

CTEC F18 Testing Variables Test command Usage: test EXPRESSION –test evaluates EXPRESSION to true or false –True  exit with STATUS 0 –False  exit with STATUS !0 test is used for three categories of testing: –Files –Strings –Numbers

CTEC F19 Functions Sequence of commands Defining: function_name () { statements } Use: function_name

CTEC F20 Functions (2) Arguments: print_args () { echo “Arg 1 is $1” echo “Arg 2 is $2” } Passing arguments: print_args arg1 arg2