Bourne Shell1-1 Borne Shell  Background m Early Unix shell that was written by Steve Bourne of AT&T Bell Lab. m Basic shell provided with many commercial.

Slides:



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

Linux Shell Script. Shell script The first line is used to specify shell program #!/bin/sh Variables variable=“text” variable=0 variable=`program arguments`
Introduction to Unix – CS 21 Lecture 11. Lecture Overview Shell Programming Variable Discussion Command line parameters Arithmetic Discussion Control.
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 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
Introduction to Unix (CA263) Decisions, Decisions By Tariq Ibn Aziz Dammam Community College.
The Bourne Shell 吳 瑞 麟 National Cheng Kung University, Computer and Network Center.
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 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 Control Structures CSE 2031 Fall August 2015.
Shell Programming, or Scripting Shirley Moore CPS 5401 Fall August 29,
Agenda Control Flow Statements Purpose test statement if / elif / else Statements for loops while vs. until statements case statement break vs. continue.
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.
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.
An Introduction to Unix Shell Scripting
The if construct The general format of the if command is: Every command has exit status If the exit status is zero, then the commands that follow between.
Chap 3 – PHP Quick Start COMP RL Professor Mattos.
Bash shell programming Part II – Control statements Deniz Savas and Michael Griffiths November 2005 Corporate Information and Computing Services The University.
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.
Writing Shell Scripts ─ part 3 CSE 2031 Fall October 2015.
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.
Shell Programming. Introducing UNIX Shells  Shell is also a programming language and provides various features like variables, branching, looping and.
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.
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.
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 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.
Agenda Positional Parameters / Continued... Command Substitution Bourne Shell / Bash Shell / Korn Shell Mathematical Expressions Bourne Shell / Bash Shell.
More Shell Programming. Slide 2 Control Flow  The shell allows several control flow statements:  if  while  for.
Flow Control. The order in which commands execute in a shell script is called the flow of the script. When you change the commands that execute based.
Shell script – part 2 CS 302. Special shell variable $0.. $9  Positional parameters or command line arguments  For example, a script myscript take 2.
CSCI 330 UNIX and Network Programming Unit X: Shell Scripts II.
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]
Compunet Corporation Introduction to Unix (CA263) Round and Round By Tariq Ibn Aziz Dammam Community College.
Chapter 5 The Bourne Shell Graham Glass and King Ables, UNIX for Programmers and Users, Third Edition, Pearson Prentice Hall, Notes by Michael Weeks.
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.
1 Lecture 8 Shell Programming – Control Constructs COP 3353 Introduction to UNIX.
Shell Control Structures
Shell Scripting March 1st, 2004 Class Meeting 7.
Lecture 9 Shell Programming – Command substitution
Shell Programming (ch 10)
Writing Shell Scripts ─ part 3
Writing Shell Scripts ─ part 3
Agenda Control Flow Statements Purpose test statement
Pepper (Help from Dr. Robert Siegfried)
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
Shell Control Structures
Chapter 5 The Bourne Shell
Shell Control Structures
Chapter 5 Bourne Shells Scripts
More Shell Programming
Introduction to Bash Programming, part 3
Presentation transcript:

Bourne Shell1-1 Borne Shell  Background m Early Unix shell that was written by Steve Bourne of AT&T Bell Lab. m Basic shell provided with many commercial versions of UNIX m Many system shell scripts are written to run under Bourne Shell m A long and successful history

Bourne Shell1-2 Bourne Shell Programming  Control structures m if … then m for … in m while m until m case m break and continue

Bourne Shell1-3 if … then  Structure if test-command then commands fi Example: if test “$word1” = “$word2” then echo “Match” fi

Bourne Shell1-4 test  Command test is a built-in command  Syntax test expression [ expression ] m The test command evaluate an expression m Returns a condition code indicating that the expression is either true (0) or false (not 0)  Argument m Expression contains one or more criteria Logical AND operator to separate two criteria: -a Logical OR operator to separate two criteria: -o Negate any criterion: ! Group criteria with parentheses m Separate each element with a SPACE

Bourne Shell1-5 Test Criteria  Test Operator for integers: int1 relop int2 RelopDescription -gtGreater than -geGreater than or equal to -eqEqual to -neNot euqal to -leLess than or equal to -ltLess than

Bourne Shell1-6 Exercise  Create a shell script to check there is at least one parameter m Something like this: … if test $# -eq 0 then echo “ you must supply at least one arguments” exit 1 fi …

Bourne Shell1-7 Test Criteria  The test built-in options for files OptionTest Performed on file -d filenameExists and is a directory file -f filenameExists and is a regular file -r filenameExists and it readable -s filenameExists and has a length greater than 0 -u filenameExists and has setuid bit set -w filenameExists and it writable -x filenameExists and it is executable …

Bourne Shell1-8 Exercise  Check weather or not the parameter is a non-zero readable file name m Continue with the previous script and add something like if [ -r “$filename” –a –s “$filename” ] then … fi

Bourne Shell1-9 Test Criteria  String testing Criteriameaning StringTrue if string is not the null string -n stringTrue if string has a length greater than zero -z stringTrue if string has a length of zero String1 = string2True if string1 is equal to string2 String1 != string2True if string1 is not equal to string2

Bourne Shell1-10 Exercise  Check users confirmation m Frist, read user input echo -n “Please confirm: [Yes | No] “ read user_input m Then, compare it with standard answer ‘yes’ if [ “$user_input” = Yes ] then echo “Thanks for your confirmation!” fi m What will happen if no “” around $user_input and user just typed return?

Bourne Shell1-11 if…then…else  Structure if test-command then commands else commands fi m You can use semicolon (;) ends a command the same way a NEWLINE does. if [ … ]; then … fi if [ 5 = 5 ]; then echo "equal"; fi

Bourne Shell1-12 if…then…elif  Structure if test-command then commands elif test-command then commands. else commands fi

Bourne Shell1-13 Debugging Shell Scripts  Display each command before it runs the command m Set the –x option for the current shell $set –x m Use the –x to invoke the script $sh –x command arguments m Add the set command at the top of the script set –x  Then each command that the script executes is preceded by a plus sign (+) m Distinguish the output of trace from any output that the script produces  Turn off the debug with set +x

Bourne Shell1-14 for… in  Structure for loop-index in argument_list do commands done Example: for file in * do if [ -d “$file” ]; then echo $file fi done

Bourne Shell1-15 for  Structure for loop-index do commands done m Automatically takes on the value of each of command line arguments, one at a time. Which implies for arg in

Bourne Shell1-16 while  Structure while test_command do commands done Example: while [ “$number” –lt 10 ] do … … number=`expr $number + 1` done

Bourne Shell1-17 until  Structure until test_command do commands done Example: secretname=jenny name=noname until [ “$name” = “$secretname” ] do echo “ Your guess: \c” read name done

Bourne Shell1-18 break and continue  Interrupt for, while or until loop  The break statement m transfer control to the statement AFTER the done statement m terminate execution of the loop  The continue statement m Transfer control to the statement TO the done statement m Skip the test statements for the current iteration m Continues execution of the loop

Bourne Shell1-19 Example: for index in do if [ $index –le 3 ]; then echo continue continue fi echo $index if [ $index –ge 8 ]; then echo “break” break fi done

Bourne Shell1-20 case  Structure case test_string in pattern-1 ) commands_1 ;; pattern-2 ) commands_2 ;; … … esac  default case: catch all pattern * )

Bourne Shell1-21 case  Special characters used in patterns PatternMatches *Matches any string of characters. ?Matches any single character. […]Defines a character class. A hyphen specifies a range of characters |Separates alternative choices that satisfy a particular branch of the case structure

Bourne Shell1-22 Example #!/bin/sh echo “\n Command MENU\n” echo “ a. Current data and time” echo “ b. Users currently logged in” echo “ c. Name of the working directory\n” echo “Enter a,b, or c: \c” read answer echo case “$answer” in a) date ;; b) who ;; c) pwd ;; *) echo “There is no selection: $answer” ;; esac

Bourne Shell1-23 echo and read  The backslash quoted characters in echo m \c suppress the new line m \n new line m \r return m \t tab  Read m read variable1 [variable2 …] Read one line of standard input Assign each word to the corresponding variable, with the leftover words assigned to last variables If only one variable is specified, the entire line will be assigned to that variable.

Bourne Shell1-24 Example: bundle #!/bin/sh #bundle: group files into distribution package echo "# To Uble, sh this file" for i do echo "echo $i" echo "cat >$i <<'END of $i' " cat $i echo "END of $i" done  Will this program work for sure?

Bourne Shell1-25 Built-in: exec  Execute a command: m Syntax: exec command argument m Run a command without creating a new process Quick start Run a command in the environment of the original process Exec does not return control to the original program Exec can be the used only with the last command that you want to run in a script Example, run the following command in your current shell, what will happen? $exec who

Bourne Shell1-26 Built-in: exec  Redirect standard output, input or error of a shell script from within the script exec < infile exec > outfile 2> errfile m Example: sh-2.05b$ more redirect.sh exec > /dev/tty echo "this is a test of redirection" sh-2.05b$./redirect.sh 1 > /dev/null 2 >& 1 this is a test of redirection

Bourne Shell1-27 Catch a signal: builtin trap  Built-in trap m Syntax: trap ‘commands’ signal-numbers m Shell executes the commands when it catches one of the signals m Then resumes executing the script where it left off. Just capture the signal, not doing anything with it trap ‘ ‘ signal_number m Often used to clean up temp files m Signals SIGHUP1disconnect line SIGINT2control-c SIGKILL9kill with -9 SIGTERM15default kill SIGSTP24control-z …

Bourne Shell1-28 Example ~/cs3451]$ more inter #!/bin/sh trap 'echo PROGRAM INTERRUPTED' 2 while true do echo "programming running." sleep 1 done

Bourne Shell1-29 A partial list of built-in  bg, fg, jobsjob control  break, continuechange the loop  cd, pwdworking directory  echo, readdisplay/read  evalscan and evaluate the command  execexecute a program  exitexit from current shell  export, unsetexport/ remove a val or fun  testcompare arguments

Bourne Shell1-30 A partial list of builtin  killsends a signal to a process or job  set sets flag or argument  shiftpromotes each command line argument  timesdisplays total times for the current shell and  traptraps a signal  typeshow whether unix command, build-in, function  umaskfile creation mask  waitwaits for a process to terminate.  ulimit print the value of one or more resource limits

Bourne Shell1-31 functions  A shell function is similar to a shell script m It stores a series of commands for execution at a later time. m The shell stores functions in the memory m Shell executes a shell function in the same shell that called it.  Where to define m In.profile m In your script m Or in command line  Remove a function m Use unset built-in

Bourne Shell1-32 functions  Syntax function_name() { commands }  Example: sh-2.05b$ whoson() > { > date > echo "users currently logged on" > who > } sh-2.05b$ whoson Tue Feb 1 23:28:44 EST 2005 users currently logged on ruihong :0 Jan 31 08:46 ruihong pts/1 Jan 31 08:54 (:0.0) ruihong pts/2 Jan 31 09:02 (:0.0)

Bourne Shell1-33 Example sh-2.05b$ more.profile setenv() { if [ $# -eq 2 ] then eval $1=$2 export $1 else echo "usage: setenv NAME VALUE" 1>&2 fi } sh-2.05b$..profile sh-2.05b$ setenv T_LIBRARY /usr/local/t sh-2.05b$ echo $T_LIBRARY /usr/local/t

Bourne Shell1-34 Exercise  Let’s look at some system scripts m /etc/init.d/syslog m /etc/init.d/crond

Bourne Shell1-35 Summary  Shell is a programming language  Programs written in this language are called shell scripts. m Variable m Built-in m Control structure m Function m Call utilities outside of shell find, grep, awk