Chapter 5 Bourne Shells Scripts

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.
CS Lecture 03 Outline Sed and awk from previous lecture Writing simple bash script Assignment 1 discussion 1CS 311 Operating SystemsLecture 03.
Shell Programming Software Tools. Slide 2 Shells l A shell can be used in one of two ways: n A command interpreter, used interactively n A programming.
CS 497C – Introduction to UNIX Lecture 33: - Shell Programming Chin-Chih Chang
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 Programming. Shell Scripts (1) u Basically, a shell script is a text file with Unix commands in it. u Shell scripts usually begin with a #! and.
Shell Script Examples.
Shell Control Structures CSE 2031 Fall August 2015.
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.
1 Operating Systems Lecture 3 Shell Scripts. 2 Brief review of unix1.txt n Glob Construct (metacharacters) and other special characters F ?, *, [] F Ex.
Introduction to UNIX / Linux - 11
Chapter 5 Bourne Shells Scripts By C. Shing ITEC Dept Radford University.
1 Shell Programming – Extra Slides. 2 Counting the number of lines in a file #!/bin/sh #countLines1 filename=$1#Should check if arguments are given count=0.
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.
Chapter 4 UNIX Common Shells Commands By C. Shing ITEC Dept Radford University.
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.
#!/bin/sh echo Hello World cat Firstshellscript.sh Firstshellscript.sh.
Shell Programming. Introducing UNIX Shells  Shell is also a programming language and provides various features like variables, branching, looping and.
1 System Administration Introduction to Scripting, Perl Session 3 – Sat 10 Nov 2007 References:  chapter 1, The Unix Programming Environment, Kernighan.
Linux+ Guide to Linux Certification Chapter Eight Working with the BASH Shell.
CSC 352– Unix Programming, Spring 2015 March 2015 Shell Programming (Highlights only)
1 © 2001 John Urrutia. All rights reserved. Chapter 10 using the Bourne Again Shell.
Shell Programming. Creating Shell Scripts: Some Basic Principles A script name is arbitrary. Choose names that make it easy to quickly identify file function.
1 Operating Systems Lecture 2 UNIX and Shell Scripts.
LINUX programming UNIT-2 1. INDEX UNIT-IV PPT SLIDES Srl. No. Module as per Session planner Lecture No. PPT Slide No. 1.Working with Bourne shell L1 1-2.
Chapter 10: BASH Shell Scripting Fun with fi. In this chapter … Control structures File descriptors Variables.
(A Very Short) Introduction to Shell Scripts CSCI N321 – System and Network Administration Copyright © 2000, 2003 by Scott Orr and the Trustees of Indiana.
Shell Programming Learning Objectives: 1. To understand the some basic utilities of UNIX File 2. To compare UNIX shell and popular shell 3. To learn the.
©Colin Jamison 2004 Shell scripting in Linux Colin Jamison.
CIT 140: Introduction to ITSlide #1 CIT 140: Introduction to IT Shell Programming.
Xuan Guo Chapter 5 The Bourne Shell Graham Glass and King Ables, UNIX for Programmers and Users, Third Edition, Pearson Prentice Hall, Notes by Michael.
1 © 2000 John Urrutia. All rights reserved. Session 5 The Bourne Shell.
CSCI 330 UNIX and Network Programming Unit IX: Shell Scripts.
Shell Control Statements and More
Agenda Positional Parameters / Continued... Command Substitution Bourne Shell / Bash Shell / Korn Shell Mathematical Expressions Bourne Shell / Bash Shell.
UNIX Shell Script (2) Dr. Tran, Van Hoai Faculty of Computer Science and Engineering HCMC Uni. of Technology
Lab 8 Shell Script Reference: Linux Shell Scripting Tutorial v1.05r3 A Beginner's handbook
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.
Shell script – part 2 CS 302. Special shell variable $0.. $9  Positional parameters or command line arguments  For example, a script myscript take 2.
Assigning Values 1. $ set One Two Three [Enter] $echo $1 $2 $3 [Enter] 2. $set `date` [Enter] $echo $1 $2 $3 [Enter] 3. $echo $1 $2 $3 $4 $5 $6 [Enter]
Chapter 5 The Bourne Shell Graham Glass and King Ables, UNIX for Programmers and Users, Third Edition, Pearson Prentice Hall, Notes by Michael Weeks.
#!/bin/sh 2002/07/26 Jaeho Shin. Interface between user and the operating system sh, bash, csh, tcsh, ksh, … What is a “Shell”? OSuser Shell.
CS 403: Programming Languages Lecture 20 Fall 2003 Department of Computer Science University of Alabama Joel Jones.
By Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
Linux Administration Working with the BASH Shell.
Shell Control Structures CSE 2031 Fall June 2016.
1 Lecture 8 Shell Programming – Control Constructs COP 3353 Introduction to UNIX.
Bash Shell Scripting 10 Second Guide.
Shell Control Structures
CSC 352– Unix Programming, Fall 2012
Shell Programming (ch 10)
Shell Script Assignment 1.
Pepper (Help from Dr. Robert Siegfried)
LING 408/508: Computational Techniques for Linguists
Command Substitution Command substitution is the mechanism by which the shell performs a given set of commands and then substitutes their output in the.
Linux Shell Script Programming
Essential Shell Programming
Presented by, Mr. Satish Pise
Shell Control Structures
Chapter 5 The Bourne Shell
CSC 352– Unix Programming, Fall, 2011
Shell Control Structures
More Shell Programming
Chapter 3 The UNIX Shells
Introduction to Bash Programming, part 3
Presentation transcript:

Chapter 5 Bourne Shells Scripts By C. Shing ITEC Dept Radford University

Objectives Understand how to use Bourne shell to write shell script

Shell Scripts A sequence of Unix commands Must be stored in Unix format (no <CR> character) file The file must be executable (use chmod u+x shellfile) To execute a shellfile: (type in the shell script filename) such as shellfile

Identify Shell Script If the first line has form: #!shellpath, then shellpath determines which shell the script file is interpreted. If the first line is just a #, then the current shell is used. other # form is a comment line (do not start the comment from 1st character of the 1st line, give at least a space if the comment line starts from 1st line) Otherwise, Bourne shell is used as default shell.

Bourne Shells More Predefined Environment Vaiables: $PS1: shell prompt for 1st line of shell command $PS2: shell prompt for 2nd line or more of the continued shell command

Bourne Shells More Predefined Local Variables: Predefined Local Explain $@ quoted list of all command line parameters (other than the command) $# total # of all command line parameters (don’t count command) $? exit value of the last command

Bourne Shells (Cont.) More Predefined Local Variables: Predefined Explain $! PID of the last background command $$ PID of the shell

Bourne Shells (Cont.) Example: Make sure finish of previous command before proceed to next command

Bourne Shells Assign value to a variable: variable=value Example: myIncludeDir = /usr/include

Bourne Shells Change from Local to Environment Variable: export variable Example: export myIncludeDir read variable Read a line of input and store in $variable

Bourne Shells Evaluate Expression: expr expression Expressions Explain string: regular_expression return string length if both sides match; return 0 otherwise

Signal - trap Execute command based on the signals received: trap command signal1 signal2 ... The shell will execute the command if either one of the signals received. If signal =0, shell executes the command when the the shell script terminates.

Signals Signals Explain 1 or SIGHUP hang up (logout) 2 or SIGINT Press interrupt key 3 or SIGQUIT quit 9 or SIGKILL kill

Redirection Associate standard input channel with file descriptor n command <& n Associate standard error channel with file descriptor n command >& n Where n: file descriptor n=0: standard input n=1: standard output n=2: standard error

Redirection (Cont.) Example: (find / -name handler.py > /dev/tty) > & /dev/null This finds the python file and sends errors to /dev/null (drop error message since /dev/null is a pseudo-device), and results to /dev/tty (one’s terminal), so you don’t see error in screen

Redirect Standard Error To an error file command 2> errorfile Example: gcc beepsyntax.c > beep.out 2>beep.err Then the syntax error will be stored in beep.err find / -name handler.py –print 2>/dev/null This will show the finding in screen without error messages

Redirect Standard Error (Cont.) To Standard Output: command > outputfile 2>&1 Example: gcc beepsyntax.c > beep.out 2>&1 Then the syntax error will be stored in beep.out along with the regular output.

Here Document here document: command << label or command >> label The shell copies standard input <<(or output >>) up to, but not including the line with the label into the shell buffer and then execute the command. (Note: label must start a line somewhere down.)

Control Structures - if if structure Syntax: if [condition] (or if test condition) then ... ... fi

Control Structures - if Condition: commonly used are as below -d dirfile: true if dirfile exists as a directory -f file: true if file exists as a regular file -r file: true if file exists as readable -x file: true if file exists as executable -s file: true if file contains at least one character str1 = str2 str1 != str2

Control Structures - if Condition: (Cont.) str1: true if str1 is not null (you can use this to check whether a process ID still exists) int1 -eq int2 int1 -ne int2 int1 -gt int2 int1 -ge int2 int1 -lt int2 int1 -le int2

Control Structures – if (Cont.) Condition: (Cont.) !expr expr1 -a expr2: and expr1 -o expr2: or \(expr\): grouping expression

Control Structures – if (Cont.) Example: If [ -f as5.pl ] then perl as5.pl fi This checks if file as5.pl exists, then run the Perl program.

Control Structures – if (Cont.) Example: (another way) If [ -f as5.pl ]; then perl as5.pl fi This checks if file as5.pl exists, then run the Perl program.

Control Structures – if (Cont.) Example: If [ “$1” = “” ] then echo Usage: check.sh uid fi This checks if the 1st command line argument exists. If not, display the error message by Usage clause.

Control Structures – if (Cont.) Example: If [ ! -f as5.php ] then perl as5.pl fi This checks if file as5.php does not exist, then run the Perl program.

Control Structures if … elif if structure Syntax: if [condition] (or if test condition) then ... elif ...(many times> then ... else ... fi

Control Structures - case case structure Syntax: case ... in value1) ... ;; value2) ... ;; ... *) ... ;; esac

Control Structures - while while structure Syntax: while [condition] do ... ... done

Control Structures - while while structure Syntax: (another way) while [condition]; do ... ... done

Control Structures - for for structure Syntax: for variable in ... do ... ... done

Control Structures - until until structure Syntax: until ... do ... ... done

Bourne Shells Example #! /bin/sh # Usage: gradehw 1 If [ $# -eq 1 ] then echo Enter a Grading System Program, Grade HW $1 cd hw$1 for i in `ls` do cd $i cp as$1.c as$1.txt vi as$1.txt cd .. done else echo Error: Usage: gradehw 1 fi

Shell Module Example: RunGrading() { } RunGrading A Shell Module/Subprogram RunGrading() { … } Execute the module in a shell script by RunGrading Check Compilation Example test2.sh

More Examples: track.sh track.cleanup track.sed menu.sh

Example of Stealing Superuser if superuser set Example of Stealing Superuser if superuser set . As 1st entry in search path (p. 113 Practical Unix) User: prepare shell script called ls in home directory and do the following: cd chmod 700 . touch ./-f Superuser: su cd /home/user ls The superuser suggests to user not to use file name –f, didn’t know /bin/sh being stolen as a hidden file .steal

Reference Ch. 5 Practical Unix & Internet Security by Garfinkel, Spafford & Schwartz, 2ed, O’Reilly