1 UNIX Operating Systems II Part 2: Shell Scripting Instructor: Stan Isaacs.

Slides:



Advertisements
Similar presentations
CST8177 bash Scripting Chapters 13 and 14 in Quigley's "UNIX Shells by Example"
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.
CS 497C – Introduction to UNIX Lecture 33: - Shell Programming Chin-Chih Chang
23-Jun-15Advanced Programming Spring 2002 bash Henning Schulzrinne Department of Computer Science Columbia University.
Bash, part 2 Prof. Chris GauthierDickey COMP Unix Tools.
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.
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.
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.
An Introduction to Unix Shell Scripting
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 8 Shell.
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.
Shell Scripting Todd Kelley CST8207 – Todd Kelley1.
Introduction to Linux OS (IV) AUBG ICoSCIS Team Prof. Volin Karagiozov March, 09 – 10, 2013 SWU, Blagoevgrad.
1 Shell Scripting (C shell) SungHo Maeung 10/27/2000 Tutorial section Computer Science Lab.
Introduction to Bash Programming Ellen Zhang. Previous three classes What have we learnt so far ?
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.
Shell Scripting AFNOG IX Rabat, Morocco May 2008.
Shell Programming. Introducing UNIX Shells  Shell is also a programming language and provides various features like variables, branching, looping and.
CSC 352– Unix Programming, Spring 2015 March 2015 Shell Programming (Highlights only)
Shell Programming. Creating Shell Scripts: Some Basic Principles A script name is arbitrary. Choose names that make it easy to quickly identify file function.
Lecture 2: Advanced UNIX, shell scripts (ol)‏ Programming Tools And Environments.
Chapter 10: BASH Shell Scripting Fun with fi. In this chapter … Control structures File descriptors Variables.
Shells. Variables MESSAGE="HELLO WORLD" echo $MESSAGE MESSAGE="HELLO WORLD $LOGNAME" echo $MESSAGE MESSAGE="HELLO WORLD $USER" echo $MESSAGE.
LIN Unix Lecture 5 Unix Shell Scripts. LIN Command Coordination ; && || command1 ; command2 Interpretation: Do command 1. Then do command.
©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.
CISC3130 Spring 2013 Fordham Univ. 1 Bash Scripting: control structures.
Shell Control Statements and More
Agenda Positional Parameters / Continued... Command Substitution Bourne Shell / Bash Shell / Korn Shell Mathematical Expressions Bourne Shell / Bash Shell.
1 Unix/Linux commands and shell programming-Part 2 (Dr. Mohamed El Bachir Menai)
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.
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.
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.
#!/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.
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.
Chapters 13 and 14 in Quigley's "UNIX Shells by Example"
Bash Shell Scripting 10 Second Guide.
Shell Control Structures
CSC 352– Unix Programming, Spring 2016, Final Exam Guide
CSC 352– Unix Programming, Fall 2012
Lecture 9 Shell Programming – Command substitution
Shell Programming (ch 10)
Agenda Control Flow Statements Purpose test statement
Pepper (Help from Dr. Robert Siegfried)
Agenda Functions: What are functions? / Purpose of Functions
UNIX Reference Sheets CSE 2031 Fall 2010.
CSE 303 Concepts and Tools for Software Development
Shell Control Structures
Chapter 5 The Bourne Shell
CSC 352– Unix Programming, Fall, 2011
Shell Control Structures
Introduction to Bash Programming, part 3
Review.
Review The Unix Shells Graham Glass and King Ables,
Presentation transcript:

1 UNIX Operating Systems II Part 2: Shell Scripting Instructor: Stan Isaacs

2 Shell Scripts Parameters/arguments - what’s on the input line Control structures –for- repetition –if … then … else- conditional execution –while/until- conditional repetition –case- do one of several alternatives test command- useful for conditions read command - set local variables from file sleep- pause for a while echo,shift, tee, exit and other utilities

3 Shell Scripts Positional Parameters (arguments to shell program)  $0 - name of program  $1, $2, $3…  $# - how many  - all of them at once $$ - PID of process (Process Identification) $? - Return value from a command 3

4 variables.ex  echo \$0 is \"$0\"  echo \$1 is \"$1\"  echo is  echo \$* is \"$*\"  echo number of args is $#  echo Process ID is $$  echo Exit value of the previous echo command is -$?-  ls abcxyz  echo A bad exit value might be \"$?\"  echo Done 4

5 More on variables ${var:-default}  If var not set, use default ${var :=default}  If var not set, set it to default and use it ${var :?error message}  If var not set, print error message (to STDERR) ${var#start}, ${var##longstart}  If left end matches start, delete it  # for shortest match, ## for longest match  ${path##*/} will remove all but file name (everything up to last slash) ${var%end}, ${var%longend}  If right end matches end, delete it  % for shortest match, % for longest match  ${filename%.*} will get rid of last file extension (last dot and everying after it) 5

6 Arithmetic (( … )), $(( … )), let …, $[ … ] All of these do arithmetic  $[ … ] (in textbook) is denigrated  i=1  i=$(($i+1)) $i is now 2  ((i = i + 1 )) $i is now 3  let “i = i + 1 “ $i is now 4 6

7 Command substitution $( … )  Can contain any pipeline  The result of the commands will replace it  echo $( wc myfile ) would be the same as just executing “wc myfile”  dt=$( date ) would save the date in $dt  list=$( cat filename | tail -10 ) $list would contain the last 10 lines of the file 7

8 Shell Script example cat > reverse –file name or name of program or name of shell script or command name echo $3, $2, $1 –that’s the program! ^D (to end the program) reverse one two three –prints: “three, two, one” 8

9 read command reads a line at a time from standard input, filling in variables with “words” (tokens separated by spaces) read num1 num2 num3 –echo :$num1:$num2:$num3: echo “prompt string” read var1 var2 –will print “prompt string” before reading variables 9

10 read example  echo Please type in 3 numbers.  read num1 num2 num3  echo :$num1:$num2:$num3: 10

11 echo Just “prints” to standard output echo hello echo “hello” echo \”hello\” echo $hello –blank hello=“Hi, there” echo $hello echo -n This line doesn’t have a newline 11

12 Summary: script input parameters –script parm1 parm2 parm2 interactive stdin –using echo/read combination file stdin –expects file input; uses read Flags? (script -a -fname) –just parameters, you need to check for them –try getopts command 12

13 Script layout and form #! path to shell or program # Description of what the script does, and any other information for users/maintainers # usage information usage=“name [-f] …” #Definitions, defaults INFILE=~isaacs/class/unix.part2/phone.txt # Read input parameters and flags, or get input (case statement? getopts? read statement?) rest of script 13

14 test! test! test! Test with no input and with bad input (the script should end nicely, with good messages) if stdin: try pipe, try “<“, try terminal if numbers, what happens with letters? (Dos to UNIX: ^M problem at ends of lines) debuging –echo statements (delete or comment when OK) –bash -x 14

15 for loop for name in word … do command list done 15

16 simple for example  for num in one two three  do  echo -$num-  done 16

17 Some test examples while test $# -gt 0 while test -n $1 if test $count -lt 10 if test “$answer” != “y” –(but what if it is “Y” or “yes”?) if test ! -r “$1” -o -f “$1” Can use “[ … ]” instead of “test”! 17

18 test, [ … ], [[ … ]] test … and [ … ] are exactly the same. You need spaces around the [ and ] test doesn’t allow any wildcards, but [[ … ]] does Things like [ $abc = “xyz” ] or [ 5 gt 3 ] need spaces around the operators 18

19 if conditional if expression then command list else command list fi * else is optional; can have elif. 19

20 simple if example  if true  then  echo yes  fi 20

21 case construct case word in pattern1) command list ;; pattern2) command list;; pattern3) command list ;; … esac 21

22 case example  case "$1"  in  0) echo zero ;;  1) echo one ;;  2) echo two ;;  3) echo three ;;  4) echo four ;;  5) echo five ;;  6) echo six ;;  7) echo seven ;;  8) echo eight ;;  9) echo nine ;;  *) echo “This is not an integer.”;;  esac 22

23 while (or until) loop while command list 1 do command list 2 done * can use until instead of while 23

24 While example echo "Type a line to process: " read input while test $input != "q" do echo processing input line: $input Echo "Type another line:" read input done echo Finished process 24

25 while true example  while true  do  myprogram  sleep 300  done 25

26 until example  until who | grep "$1" > /dev/null  do  sleep 30  done  echo "\t\t$1 is on the system."  exit 0 26

27 Some other shell commands shift - moves parameter $2->$1, etc exit [n] - leave a script, with an exit value break - exit the current for, while, or until continue - begin next iteration of loop 27

28 getopts getopts string name –string is option letters, with “:” if args –name is any variable name –returns OPTIND next option to process –returns OPTARG the argument, if any Will report if bad option, or missing arg Leading “:” silences error report 28

29 getopts example while getopts dclw option do case $option in d) date ;; c) cal ;; l) ls ;; w) who | wc -l ;; esac done 29

30 Another getopts example while getopts :ab:cd: name do case $name in a) aset=TRUE ;; b) bval=$OPTARG ;; c) cflag=1 ;; d) dvalue=“list $OPTARG” ;; \?) echo $usage; exit 1 ;; esac done shift $(( $OPTIND - 1 )) 30