Shell Control Structures

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
Integer variables #!/bin/csh # sum of numbers from $argv[1] to $argv[2] set low = $argv[1] set high = $argv[2] set sum = 0 set current = $low while ( $current.
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.
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.
Lab 8 Shell Script Reference:
Shell Control Structures CSE 2031 Fall August 2015.
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.
Chapter 5 Bourne Shells Scripts By C. Shing ITEC Dept Radford University.
Day 8,9,10 COP 3502 Introduction to Shell Scripts David A. Gaitros Department of Computer Science Florida State University.
Writing C-shell scripts #!/bin/csh # Author: Ken Berman # Date: # Purpose: display command and parameters echo $0 echo $argv[*]
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.
#!/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.
Shell Programming. Creating Shell Scripts: Some Basic Principles A script name is arbitrary. Choose names that make it easy to quickly identify file function.
Shell Programming. An example u tr abcdefghijklmnopqrstuvwxyz \ thequickbrownfxjmpsvalzydg file2 –encrypts file1 into file2 u record this command with.
(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.
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.
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)
UNIX Shell Script (2) Dr. Tran, Van Hoai Faculty of Computer Science and Engineering HCMC Uni. of Technology
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.
Lab 8 Shell Script Reference: Linux Shell Scripting Tutorial v1.05r3 A Beginner's handbook
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]
Shell Scripting What is Shell Programming? Putting UNIX commands in a file Seldom used where speed is important Often used to manipulate files To create.
CS 403: Programming Languages Lecture 20 Fall 2003 Department of Computer Science University of Alabama Joel Jones.
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.
Bash Shell Scripting 10 Second Guide.
Writing Shell Scripts ─ part 1
Agenda Bash Shell Scripting – Part II Logic statements Loop statements
Part 1: Basic Commands/Utilities
Lecture 9 Shell Programming – Command substitution
Unix Scripting Session 4 March 27, 2008.
Writing Shell Scripts ─ part 3
Agenda Control Flow Statements Purpose test statement
What is Bash Shell Scripting?
Pepper (Help from Dr. Robert Siegfried)
UNIX Reference Sheets CSE 2031 Fall 2010.
Command Substitution Command substitution is the mechanism by which the shell performs a given set of commands and then substitutes their output in the.
Copyright © – Curt Hill Bash Flow of Control Copyright © – Curt Hill.
CSE 303 Concepts and Tools for Software Development
Linux Shell Script Programming
Writing Shell Scripts ─ part 1
Presented by, Mr. Satish Pise
Shell Control Structures
Shell Control Structures
Chapter 5 Bourne Shells Scripts
More Shell Programming
Essential Shell Programming
Introduction to Bash Programming, part 3
Review.
Review The Unix Shells Graham Glass and King Ables,
Presentation transcript:

Shell Control Structures CSE 2031 Fall 2010 12 May 2018

Control Structures if then else for while case (which) until

if Statement and test Command Syntax: if condition then command(s) elif condition_2 else fi Command test is often used in condition.

if – then – else Example % cat if_else #!/bin/sh echo -n 'Enter string 1: ' read string1 echo -n 'Enter string 2: ' read string2 if test $string1 = $string2 then echo 'They match!' else echo 'No match!' fi % if_else Enter string 1: acd Enter string 2: 123 No match! Enter string 1: 123 They match!

test Command

test Command (2) Parentheses can be used for grouping test conditions.

test Example 1 % cat check_file #!/bin/sh if test ! -s $1 then echo "File $1 is empty.“ exit 1 else ls -l $1 fi % touch z.txt % check_file z.txt File z.txt is empty.

test Example 2 % cat check_file #!/bin/sh if test $# -eq 0 then echo Usage: check_file file_name exit 1 fi if test ! -s $1 echo "File $1 is empty.“ else ls -l $1

for Loops for variable in list do command(s) done variable is a user-defined variable. list is a sequence of strings separated by spaces.

for Loop Example 1 % cat fingr #!/bin/sh for name in $* do finger $name done Recall that $* stands for all command line arguments the user enters.

for Loop Example 2 % cat fsize #!/bin/sh for i in $* do echo "File $i: size `wc -c $i`" done % fsize chex chfile chfile2 File chex: size 86 chex File chfile: size 90 chfile File chfile2: size 163 chfile2

Arithmetic Operations Using expr The shell is not intended for numerical work (use Java, C, or Perl instead). However, expr utility may be used for simple arithmetic operations on integers. expr is not a shell command but rather a UNIX utility. To use expr in a shell script, enclose the expression with backquotes. Example: #!/bin/sh sum=`expr $1 + $2` echo $sum Note: spaces are required around the operator + (but not allowed around the equal sign).

expr Example % cat cntx #!/bin/sh # Count the number of executable files in … # the current working directory count=0 for i in * # what if we replace * with $* ? do if test -x $i then count=`expr $count + 1` ls -l $i fi done echo “$count executable files.”

while Loops while condition do command(s) done Command test is often used in condition. Execute command(s)when condition is met.

while Loop Example #!/bin/sh # Display the command line arguments, one per line. count=1 argc=$# while test $count -le $argc do echo "Argument $count is: $1" count=`expr $count + 1` shift # shift arg 2 into arg 1 position done # What happens if the while statement is as follows? # while test $count -le $#

until Loops until condition do command(s) done Command test is often used in condition. Exit loop when condition is met.

until Loop Example % cat grocery #!/bin/sh # Enter a grocery list and … # store in a file indicated by $1 # echo To end list, enter \"all\". item=nothing until test $item = “all” do echo -n "Enter grocery item: " read item echo $item >> $1 done

until Loop Example Output % grocery glist To end list, enter "all". Enter grocery item: milk Enter grocery item: eggs Enter grocery item: lettuce Enter grocery item: all % cat glist milk eggs lettuce all

case Statement case variable in pattern1) command(s);; . . . patternN) command(s);; *) command(s);; # all other cases esac Why the double semicolons?

case Statement Example #!/bin/sh # Course schedule echo -n "Enter the day (mon, tue, wed, thu, fri): " read day case $day in mon) echo 'CSE2031 2:30-4:30 CLH-H' echo 'CSE2021 17:30-19:00 TEL-0016';; tue | thu) echo 'CSE2011 17:30-19:00 SLH-E';; wed) echo 'No class today. Hooray!';; fri) echo 'CSE2031 2:30-4:30 LAB 1006';; *) echo 'Day off. Hooray!';; esac

Process-Related Variables Variable $$ is PID of the shell. % cat shpid #!/bin/sh ps echo PID of shell is = $$ % shpid PID TTY TIME CMD 5658 pts/75 00:00:00 shpid 5659 pts/75 00:00:00 ps 11231 pts/75 00:00:00 tcsh PID of shell is = 5658

Process Exit Status All processes return exit status (return code). Exit status tells us whether the last command was successful or not. Stored in variable $? 0 (zero) means command executed successfully. 0 is good; non-zero is bad. Good practice: Specify your own exit status in a shell script using exit command. default value is 0 (if no exit code is given).

Process Exit Status: Example An improved version of grep. % cat igrep #!/bin/sh # Arg 1: search pattern # Arg 2: file to search # grep $1 $2 if test $? -ne 0 then echo Pattern not found. fi % igrep echo phone echo –n “Enter name: ” % igrep echo2 chex Pattern not found.

Next time … Scripting with awk Shell scripting – part 3 Reading for this lecture: 3.6 to 3.8 and Chapter 5, UNIX textbook Posted notes (chapter 33)