shell scriptingjsumey/csc400/ppts/scripting.pdfshell scripting csc400 - operating systems 4 csc400:...

12
Shell Scripting CSC400 - Operating Systems 1 SHELL SCRIPTING A Presentation for CSC400 Operating Systems by J. Sumey CSC400: Shell Scripting 2 of 23 scripting concepts & overview Bourne Shell Scripting Scripting with other scripting languages: VBscript, JavaScript, Perl, PHP • oops! that's in another course… Presentation Overview

Upload: others

Post on 07-Oct-2020

15 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: SHELL SCRIPTINGjsumey/CSC400/ppts/scripting.pdfShell Scripting CSC400 - Operating Systems 4 CSC400: Shell Scripting 7 of 23 •Perl –a stable, cross platform programming language

Shell Scripting

CSC400 - Operating Systems 1

SHELL SCRIPTING

A Presentation for

CSC400 Operating Systems

by J. Sumey

CSC400: Shell Scripting 2 of 23

scripting concepts & overview

Bourne Shell Scripting

Scripting with other scripting languages:

VBscript, JavaScript, Perl, PHP

• oops! that's in another course…

Presentation Overview

Page 2: SHELL SCRIPTINGjsumey/CSC400/ppts/scripting.pdfShell Scripting CSC400 - Operating Systems 4 CSC400: Shell Scripting 7 of 23 •Perl –a stable, cross platform programming language

Shell Scripting

CSC400 - Operating Systems 2

CSC400: Shell Scripting 3 of 23

Scripting Concept

ability to rapidly develop, test & deploy

algorithmic implementation without the

burdens of programming languages

highly popular among system

administrators & web developers

also considered a “staple1 skill” for

computer programmers in general

1. used, needed, or enjoyed constantly usually by many individuals

CSC400: Shell Scripting 4 of 23

Scripting as Programming

although scripting is, in essence, programming; there are important differences

• compiled vs. interpreted execution

• enforcement of syntax, types, etc.

basic programming skills still needed!

• variable & stream manipulation, constructs, function design & invocation, parameters, etc.

Page 3: SHELL SCRIPTINGjsumey/CSC400/ppts/scripting.pdfShell Scripting CSC400 - Operating Systems 4 CSC400: Shell Scripting 7 of 23 •Perl –a stable, cross platform programming language

Shell Scripting

CSC400 - Operating Systems 3

CSC400: Shell Scripting 5 of 23

Language Comparison

Compiled Languages

compiled: fast

execution

slower development

(edit-compile-test)

more “rules”

more portable

“application” oriented

Scripting Languages

interpreted: slower

execution

faster development

(no compilation!)

more flexible

less portable

“system” oriented

CSC400: Shell Scripting 6 of 23

Scripting Languages

many different scripting languages exist,

some general purpose, some for

specific purposes

• Unix – sh (Bourne), csh, ksh, zsh, bash

– “shell scripting”

• MS-DOS – “batch” files (.bat)

• Windows – VBscript, MSscript

• web browsers - JavaScript

Page 4: SHELL SCRIPTINGjsumey/CSC400/ppts/scripting.pdfShell Scripting CSC400 - Operating Systems 4 CSC400: Shell Scripting 7 of 23 •Perl –a stable, cross platform programming language

Shell Scripting

CSC400 - Operating Systems 4

CSC400: Shell Scripting 7 of 23

• Perl – a stable, cross platform programming language

supporting both procedural and object-oriented programming

– takes the best features from other languages, such as C, awk, sed, sh, and BASIC, among others

– database integration interface supports databases including Oracle, Sybase, Postgres, MySQL and others

– works with HTML, XML, and other mark-up languages

• PHP – PHP: Hypertext Preprocessor – a widely-used general-purpose scripting

language that is especially suited for Web development and can be embedded into HTML

The Bourne Shell

Page 5: SHELL SCRIPTINGjsumey/CSC400/ppts/scripting.pdfShell Scripting CSC400 - Operating Systems 4 CSC400: Shell Scripting 7 of 23 •Perl –a stable, cross platform programming language

Shell Scripting

CSC400 - Operating Systems 5

CSC400: Shell Scripting 9 of 23

Bourne Shell Background

shell: a command programming

language that provides an interface to

the UNIX operating system

developed in late ’70s by Steve Bourne

at Bell Labs

provides variables, string substitution,

flow control constructs, parameter

passing, 2-way comm. with commands

CSC400: Shell Scripting 10 of 23

Shell Scripting Overview

1st script: “hello world”

variables

wildcards & escape characters

external command execution

flow control constructs

functions

parameter passing

Page 6: SHELL SCRIPTINGjsumey/CSC400/ppts/scripting.pdfShell Scripting CSC400 - Operating Systems 4 CSC400: Shell Scripting 7 of 23 •Perl –a stable, cross platform programming language

Shell Scripting

CSC400 - Operating Systems 6

CSC400: Shell Scripting 11 of 23

1st Shell Script

special 1st line: “#!” directive declares

path to proper interpreter

• ex.2: #!/usr/bin/perl

‘#’ begins comment to end-of-line

echo is a command

#!/bin/sh

# a comment line

echo "Hello World" # another comment

CSC400: Shell Scripting 12 of 23

Script Execution

shell scripts may executed 1 of 2 ways:

submit the script to a (sub)shell

$ sh scriptname

make script “executable” & use as any

other command

$ chmod +x scriptname

$ scriptname

Page 7: SHELL SCRIPTINGjsumey/CSC400/ppts/scripting.pdfShell Scripting CSC400 - Operating Systems 4 CSC400: Shell Scripting 7 of 23 •Perl –a stable, cross platform programming language

Shell Scripting

CSC400 - Operating Systems 7

CSC400: Shell Scripting 13 of 23

Script Variables

are typeless

do not need to be declared

are referenced with ‘$’ prefix

GREETING="Hello World"

PI=3.1416

echo $GREETING

CSC400: Shell Scripting 14 of 23

User Input

may be read into variables…

echo "What’s your name?"

read U_NAME

echo "Welcome to scripting, $U_NAME!"

Page 8: SHELL SCRIPTINGjsumey/CSC400/ppts/scripting.pdfShell Scripting CSC400 - Operating Systems 4 CSC400: Shell Scripting 7 of 23 •Perl –a stable, cross platform programming language

Shell Scripting

CSC400 - Operating Systems 8

CSC400: Shell Scripting 15 of 23

Predefined Variables

$0 : name of script

$1..$9 : 1st nine parameters to the script

$# : the number of parameters given

$@ : all the parameters

$? : return result of last command

echo "hi, my name is $0"

echo "my parameters are: $@"

CSC400: Shell Scripting 16 of 23

Command Execution

system commands may be readily

executed from a script

command output may be captured for

use within the script…

echo "directory contents:" ls

nfiles=`ls | wc –l`

echo "$nfiles items listed"

Page 9: SHELL SCRIPTINGjsumey/CSC400/ppts/scripting.pdfShell Scripting CSC400 - Operating Systems 4 CSC400: Shell Scripting 7 of 23 •Perl –a stable, cross platform programming language

Shell Scripting

CSC400 - Operating Systems 9

CSC400: Shell Scripting 17 of 23

Flow Control Constructs

for loop: known iteration

while loop: unknown iteration

if condition: 2-way statement selection

case statement: n-way selection

CSC400: Shell Scripting 18 of 23

for Loop

iterates thru a list of given values

for i in 1 2 3 4

do

echo "$i potata"

done

Page 10: SHELL SCRIPTINGjsumey/CSC400/ppts/scripting.pdfShell Scripting CSC400 - Operating Systems 4 CSC400: Shell Scripting 7 of 23 •Perl –a stable, cross platform programming language

Shell Scripting

CSC400 - Operating Systems 10

CSC400: Shell Scripting 19 of 23

while Loop

iteration as long as condition is true

"[…]" is shorthand for the test command

INPUT="?"

while [ "$INPUT" != "who's there?" ]

do

echo "knock, knock."

read INPUT

done

CSC400: Shell Scripting 20 of 23

if Condition

1- or 2-way conditional execution

• else part is optional

if [ "`date +%p`" = "AM" ]

then

echo "good morning"

else

echo "how was lunch?"

fi

Page 11: SHELL SCRIPTINGjsumey/CSC400/ppts/scripting.pdfShell Scripting CSC400 - Operating Systems 4 CSC400: Shell Scripting 7 of 23 •Perl –a stable, cross platform programming language

Shell Scripting

CSC400 - Operating Systems 11

CSC400: Shell Scripting 21 of 23

case Statement

echo "Roman converter, enter a number"

read n

case $n in

1) echo "1 = I"

;;

5) echo "5 = V"

;;

10) echo "10 = X"

;;

*) echo "I'm not that smart yet" ;;

esac

CSC400: Shell Scripting 22 of 23

Shell Functions

the shell supports the usual function

mechanism in scripts & interactively

• also used as procedures

functions are read & "stored" until called

shell functions use $1..$9 for input

parameters

may return a result to caller or simply

output something

Page 12: SHELL SCRIPTINGjsumey/CSC400/ppts/scripting.pdfShell Scripting CSC400 - Operating Systems 4 CSC400: Shell Scripting 7 of 23 •Perl –a stable, cross platform programming language

Shell Scripting

CSC400 - Operating Systems 12

CSC400: Shell Scripting 23 of 23

factorial()

{

if [ "$1" -le "1" ]; then

echo 1

else

i=`expr $1 - 1`

j=`factorial $i`

k=`expr $1 \* $j`

echo $k

fi

}

echo "Enter a number:"

read x

factorial $x