There are some string operators that can be used in the test statement to perform string comparison.

Similar documents
Then, we modify the program list.1 to list two files.

Conditional Control Structures. Dr.T.Logeswari

A shell can be used in one of two ways:

Bourne Shell Reference

Shell Scripting. Todd Kelley CST8207 Todd Kelley 1

COMP 4/6262: Programming UNIX

Scripting. More Shell Scripts. Adapted from Practical Unix and Programming Hunter College

example: name1=jan name2=mike export name1 In this example, name1 is an environmental variable while name2 is a local variable.

Shell Script Example. Here is a hello world shell script: $ ls -l -rwxr-xr-x 1 horner 48 Feb 19 11:50 hello* $ cat hello #!/bin/sh

Control Structures. CIS 118 Intro to LINUX

Shell Control Structures

Command Interpreters. command-line (e.g. Unix shell) On Unix/Linux, bash has become defacto standard shell.

Shell Control Structures

Shell programming. Introduction to Operating Systems

UNIX shell scripting

9.2 Linux Essentials Exam Objectives

CS Unix Tools & Scripting

Bash shell programming Part II Control statements

Essentials for Scientific Computing: Bash Shell Scripting Day 3

Computer Systems and Architecture

UNIX Shell Scripts. What Is a Shell? The Bourne Shell. Executable Files. Executable Files: Example. Executable Files (cont.) CSE 2031 Fall 2012

Shells & Shell Programming (Part B)

Linux shell programming for Raspberry Pi Users - 2

CSC 2500: Unix Lab Fall 2016

bash Execution Control COMP2101 Winter 2019

Preview. Review. The test, or [, command. Conditions. The test, or [, command (String Comparison) The test, or [, command (String Comparison) 2/5/2019

Bourne Shell Programming Topics Covered

Програмиранев UNIX среда

Basic Linux (Bash) Commands

Shell Programming (bash)

Vi & Shell Scripting

Advanced Unix Programming Module 03 Raju Alluri spurthi.com

CSCI 2132: Software Development. Norbert Zeh. Faculty of Computer Science Dalhousie University. Shell Scripting. Winter 2019

While Statement Examples. While Statement (35.15) Until Statement (35.15) Until Statement Example

1. Hello World Bash Shell Script. Last Updated on Wednesday, 13 April :03

Last Time. on the website

Linux Shell Scripting. Linux System Administration COMP2018 Summer 2017

OPERATING SYSTEMS LAB LAB # 6. I/O Redirection and Shell Programming. Shell Programming( I/O Redirection and if-else Statement)

Computer Systems and Architecture

28-Nov CSCI 2132 Software Development Lecture 33: Shell Scripting. 26 Shell Scripting. Faculty of Computer Science, Dalhousie University

Lecture 5. Essential skills for bioinformatics: Unix/Linux

A Big Step. Shell Scripts, I/O Redirection, Ownership and Permission Concepts, and Binary Numbers

Bash scripting basics

Unix Scripts and Job Scheduling. Overview. Running a Shell Script

Scripting. Shell Scripts, I/O Redirection, Ownership and Permission Concepts, and Binary Numbers

Assignment clarifications

SHELL SCRIPT BASIC. UNIX Programming 2014 Fall by Euiseong Seo

CS214 Advanced UNIX Lecture 4

Lecture 02 The Shell and Shell Scripting

Useful Unix Commands Cheat Sheet

INd_rasN SOME SHELL SCRIPTING PROGRAMS. 1. Write a shell script to check whether the name passed as first argument is the name of a file or directory.

Prof. Navrati Saxena TA: R. Sachan Bharat Jyoti Ranjan

Shell Start-up and Configuration Files

SHELL SCRIPT BASIC. UNIX Programming 2015 Fall by Euiseong Seo

Assignment 3, Due October 4

Practical 04: UNIX / LINUX Shell Programming 1

CSCI 211 UNIX Lab. Shell Programming. Dr. Jiang Li. Jiang Li, Ph.D. Department of Computer Science

Cisco IOS Shell. Finding Feature Information. Prerequisites for Cisco IOS.sh. Last Updated: December 14, 2012

Essential Skills for Bioinformatics: Unix/Linux

bash Tests and Looping Administrative Shell Scripting COMP2101 Fall 2017

My Favorite bash Tips and Tricks

Introduction to Perl. c Sanjiv K. Bhatia. Department of Mathematics & Computer Science University of Missouri St. Louis St.

Title:[ Variables Comparison Operators If Else Statements ]

Bash scripting Tutorial. Hello World Bash Shell Script. Super User Programming & Scripting 22 March 2013

COMP 2718: Shell Scripts: Part 1. By: Dr. Andrew Vardy

Introduction to Linux Basics Part II. Georgia Advanced Computing Resource Center University of Georgia Suchitra Pakala

Unix Guide. Meher Krishna Patel. Created on : Octorber, 2017 Last updated : December, More documents are freely available at PythonDSP

Linux shell & shell scripting - II

Shells and Shell Programming

Shell Programming. Introduction to Linux. Peter Ruprecht Research CU Boulder

Wildcards and Regular Expressions

A control expression must evaluate to a value that can be interpreted as true or false.

Scripting. More Shell Scripts Loops. Adapted from Practical Unix and Programming Hunter College

Shells and Shell Programming

What is Bash Shell Scripting?

CSE 390a Lecture 6. bash scrip'ng con'nued; remote X windows; unix 'dbits

Chapter 5 The Bourne Shell

Grep and Shell Programming

Lab 4: Shell scripting

Chapter 4. Unix Tutorial. Unix Shell

/smlcodes /smlcodes /smlcodes. Shell Scripting TUTORIAL. Small Codes. Programming Simplified. A SmlCodes.Com Small presentation

Common File System Commands

Unix as a Platform Exercises + Solutions. Course Code: OS 01 UNXPLAT

More Scripting and Regular Expressions. Todd Kelley CST8207 Todd Kelley 1

CSE 390a Lecture 6. bash scripting continued; remote X windows; unix tidbits

Practice 5 Batch & Shell Programming

Table of Contents. 1. Introduction. 2. Environment. 3. Shell Scripting Shell Scripting Introduction

EECS2301. Example. Testing 3/22/2017. Linux/Unix Part 3. for SCRIPT in /path/to/scripts/dir/* do if [ -f $SCRIPT -a -x $SCRIPT ] then $SCRIPT fi done

bash Tests and Looping Administrative Shell Scripting COMP2101 Fall 2017

EECS2301. Special variables. $* and 3/14/2017. Linux/Unix part 2

Shell script. Shell Scripts. A shell script contains a sequence of commands in a text file. Shell is an command language interpreter.

GNU Bash. an introduction to advanced usage. James Pannacciulli Systems Engineer.

S E C T I O N O V E R V I E W

More Scripting Techniques Scripting Process Example Script

EECS 470 Lab 5. Linux Shell Scripting. Friday, 1 st February, 2018

Shell Programming (Part 2)

More Scripting Todd Kelley CST8207 Todd Kelley 1

ACS Unix (Winter Term, ) Page 92

Introduction to Shell Scripting

Understanding bash. Prof. Chris GauthierDickey COMP 2400, Fall 2008

Transcription:

ACS-294- Unix (Winter Term, 26-7) Part II: Shell Programming Page 3 The test also returns a nonzero exit value if there is no argument: test String Operators There are some string operators that can be used in the test statement to perform string comparison. For example, by using test, the following will return a zero exit value: set name=sliao test "$name" = sliao The = operator tests if the two operands are identical. In the above case, we tested whether the contents of the shell variable name is identical to the string sliao. If the two operands are identical, the test returns an exit value of zero; otherwise, it would return a nonzero. The!= operator tests two strings for inequality. The exit status from test is zero if the two strings are not equal, and nonzero if the two string are identical. set name = sliao test "$name"!= sliao test "$name"!= xliao

ACS-294- Unix (Winter Term, 26-7) Part II: Shell Programming Page 3 Space characters can be part of a string. Example: set name="sliao " test "$name" = sliao test "$name"!= sliao The double quotes quoting $name are necessary here to contain the space character as part of the string assigned to name. set name="sliao " test $name = sliao In this example, the last character of the string "sliao ", a space character, is lost because $name is not enclosed by double quotes. A string itself can also be used as an operator to test if the string has a null value. The test returns an exit value zero if the string is not null, and returns a nonzero if the string has a null value. Example: set name=sliao test "$name" set name= test "$name"

ACS-294- Unix (Winter Term, 26-7) Part II: Shell Programming Page 32 Another operator is -n, with the general format -n string The -n operator returns an exit value of zero if the length of string is nonzero. The -z operator is the complement of -n. The expression -z string returns an exit status of zero if the length of string is zero. set name=sliao test -n "$name" test -z "$name" set name= test -n "$name" test -z "$name" Operator Returns true if string = string2 string and string2 are identical string!= string2 string and string2 are not identical string string is not the null string -n string The length of string is nonzero -z string The length of string is zero

ACS-294- Unix (Winter Term, 26-7) Part II: Shell Programming Page 33 Integer Operators The test can be used to test numerical values as well. The format of test remains the same: test expression For example, the operator -eq tests if two integers are equal, and the operator -lt tests if the rst integer is less than the second one. Examples: set count= test "$count" -eq set count= test "$count" -eq test "$count" -lt 5 Some of the integer operators are listed here: Operator int eq int2 int ge int2 int gt int2 int le int2 int lt int2 int ne int2 Returns true if int is equal to int2 int is greater than or equal to int2 int is greater than int2 int is less than or equal to int2 int is less than int2 int is not equal to int2 The shell itself makes no distinction about the type of value stored in a variable. It is the test operator that interprets the value as an integer when an integer operator is applied.

ACS-294- Unix (Winter Term, 26-7) Part II: Shell Programming Page 34 Examples: set x="5" set x2=" " test "$x" = 5 test "$x" -eq 5 test "$x2" = test "$x2" -eq It is clear that the string operator = and the integer operator -eq are doing different things. File Operators Most shell programs have to deal with one or more les. Naturally, Unix allows test to test on the existence and properties of les. Some le operators are list in here: Operator Returns true if r le le exists and has read permission w le le exists and has write permission x le le exists and has execute permission f le le exists and is a regular le d le le exists and is a directory s le le exists and has a size greater than All of the above le operators are unary operators and expect a single argument to follow.

ACS-294- Unix (Winter Term, 26-7) Part II: Shell Programming Page 35 Examples: ls ~ 294 bin mail mbox test -f ~/mbox test -d ~/mbox test -d ~/bin test -x ~/mbox test -r ~/mbox The Logical Operators: -a and o The operator -a performs a logical AND operation of two expressions and will return a true (an exit value of zero) only if the two joined expressions are both true. Examples: set num=3 test "$num" -ge -a "$num" -lt set num=5 test "$num" -ge -a "$num" -lt test -f ~/mbox -a -r ~/mbox test -f ~/mbox -a -x ~/mbox

ACS-294- Unix (Winter Term, 26-7) Part II: Shell Programming Page 36 The o performs a logical OR operation and works in similar format as the -a operator to form a logical OR of two expressions. The evaluation of the two ORed expressions will be true if either the rst expression or the second expression is true. Examples: test -r ~/mbox -o -x ~/mbox test -x ~/mbox -o -x ~/obox test -x ~/mbox -o -x ~/year25 The o operator has lower precedence than the a operator. But the a operator has lower precedence than the integer, string, and le operators. So, the expression test "$num" -ge -a "$num" -lt will be evaluated as test ( $num -ge ) -a ( $num -lt ) However, the above test cannot be executed and will get a message from the system: Badly placed (.

ACS-294- Unix (Winter Term, 26-7) Part II: Shell Programming Page 37 because the parentheses ( and )have a special meaning to the shell, so the above will not be interpreted appropriately by the shell. You still can use parentheses in a test expression to change the order of evaluation, but you need to quote the parentheses. Example: set num=5 test \( $num -ge \) -a \( $num -lt \) An Alternate Format for test The test is used so often by shell programmers that an alternate format of the is recognized. This format improves the readability of the, especially when used in if s. The general format of the test test expression can be expressed in the alternate format as [ expression ] The [ initiates execution of the same test, and in this format, test expects to see a closing ] at the end of the expression. It is important to remember that when you use this form, you must surround the brackets, [ and ], with spaces.

ACS-294- Unix (Winter Term, 26-7) Part II: Shell Programming Page 38 Control Statements A shell is a programming language. As other programming languages, it provides the control structures that allow making decisions. The if- Statement The general format of the if- statement is: if test- The system rst executes test- following the if keyword. If test- returns a zero exit value, all s between and are executed. If test- is unsuccessful, all s between and will be skipped. The following is a program named classday. to remind you every Wednesday that you have class at 6: pm. date Wed Feb 25 :3:48 CST 25 cat classday. #!/bin/sh # Program classday, version if date grep "Wed" > /dev/null echo "It's Wednesday, you have class at 6: pm." classday. It's Wednesday, you have class at 6: pm.

ACS-294- Unix (Winter Term, 26-7) Part II: Shell Programming Page 39 If you do not want to display the output from pipeline if date grep " Wed" you can redirect it to the device /dev/null. Since the redirection task will always be successful, the exit status of the pipeline only depends on if there is a string Wed in the output of the date. The if--else Statement The introduction of the else statement allows the if- structure to make a two-way decision. The general format of the if--else control structure is: if test- else If test- that follows if returns an exit status zero, the if--else structure executes all s between and else, and the s between else and are skipped. If the test- returns a nonzero exit value, the s between and else will be skipped and the s between else and are executed. In either case, only one set of s will be executed: the rst set if the exit status is zero, or the second set if it is nonzero. As an example, we want to improve the program classday. to display a message if today is not a class day.

ACS-294- Unix (Winter Term, 26-7) Part II: Shell Programming Page 4 cat classday.2 #!/bin/sh # Program classday, version 2 if date grep "Wed" > /dev/null echo "It's Wednesday, you have class at 6: pm." else echo "You don't have class today." date Wed Feb 25 :4:58 CST 25 classday.2 It's Wednesday, you have class at 6: pm. The if--elif Statement When your programs become more complex and have more decisions to make, you may need to use nested if-else statements. However, a special elif construct provided by Unix can do this task easily. The general format of the if--elif statement is: if test- elif test-2 elif test-n else

ACS-294- Unix (Winter Term, 26-7) Part II: Shell Programming Page 4 test-, test-2,, test-n are executed in turn and their exit status are tested. If one of them returns an exit status of zero, the s between the follows and another elif, else, or are executed. If none of the test-s returns a zero exit value, the s after the else are executed. The else is optional in this structure. If you have classes on Tuesday and Thursday, you may want to improve the program classday.2 further by using the if--elif structure: cat classday.3 #!/bin/sh # Program classday, version 3 if date grep "Tue" > /dev/null echo "It's Tuesday, you have class at 4: pm." elif date grep "Thu" > /dev/null echo "It's Thursday, you have class at 4: pm." else echo "You don't have class today." date Wed Feb 25 :6:54 CST 25 classday.3 You don't have class today. The exit Command The exit causes a shell program, or a shell to exit immediately. The general format of the exit is exit n

ACS-294- Unix (Winter Term, 26-7) Part II: Shell Programming Page 42 where n is the exit value returned to the system. If you omit n, the exit value will be that of the last executed. The value of n can be from to 255, inclusive. When we use a program that requires arguments, we may pass a wrong number of arguments to the program. For example, the program rmname.2 should remove one entry every time. In the newer version rmname.3, we ask the program to inform user if a wrong number of arguments is passed to it. cat rmname.3 #!/bin/sh # Program rmname, version 3 if [ "$#" -ne ] echo "Incorrect number of arguments." echo "Usage: $ name" exit grep -v "$" phonebook > /tmp/phonebook$$ mv /tmp/phonebook$$ phonebook cat phonebook Perry Don 222-234 Pesce Tony 333-2345 Pester Larry 555-4567 Pestrak Stan 333-5432 Petate James 555-7654 Peter Brian 444-9876 Peter Bruno 555-6789 rmname.3 Perry Don Incorrect number of arguments. Usage: rmname.3 name rmname.3 'Perry Don' cat phonebook Pesce Tony 333-2345 Pester Larry 555-4567 Pestrak Stan 333-5432 Petate James 555-7654 Peter Brian 444-9876 Peter Bruno 555-6789

ACS-294- Unix (Winter Term, 26-7) Part II: Shell Programming Page 43 First, the program rmname.3 checks for the correct number of arguments. If the number does not t the requirement, the program will display a two-line message and is terminated by the exit. If the right number of arguments is supplied, the program will remove the name and the associated telephone number from the le phonebook. The case Statement Another useful structure for making decisions is the case. The format of the case statement is case test-string in pattern_ ) ;; pattern_2 ) ;; pattern_n ) ;; esac Please note that each block of s is terminated by double semicolons, ;;. The string test-string is compared with the patterns pattern_, pattern_2,, and pattern_n successively until a match is found. Then, the s listed between the matching pattern and the double semicolons are executed. Once the double semicolons are reached, the execution of the case is terminated. The following program named translator takes the English numbers and translates it to its French equivalent:

ACS-294- Unix (Winter Term, 26-7) Part II: Shell Programming Page 44 cat translator. #!/bin/sh # Program translator, version if test "$#" -ne echo "Usage: $ word" exit case "$" in zero) echo "zero";; one) echo "un";; two) echo "deux";; three) echo "trois";; four) echo "quatre";; ve) echo "cinq";; six) echo "six";; seven) echo "sept";; eight) echo "huit";; nine) echo "neuf";; esac translator. Usage: translator. word translator. one un translator. seven sept translator. ten The last example shows what happens when you type in one word which does not match any pattern in the case statement, there is no echo is executed.

ACS-294- Unix (Winter Term, 26-7) Part II: Shell Programming Page 45 Special Pattern Matching Characters The shell allows us to use the same special characters for specifying the patterns in a case statement as you can for lename substitution. The special characters and strings you can use in the case statement: Pattern [ ] Matches * Matches any string of characters? Matches any single character Matches any one of the characters enclosed Since the pattern * matches everything, it is frequently used at the very end of the case as a default. If none of the previous patterns in the case is matched, the * will be matched. Here is another version of the program translator: cat translator.2 #!/bin/sh # Program translator, version 2 if test "$#" -ne echo "Usage: $ word" exit case "$" in zero) echo "zero";; one) echo "un";; two) echo "deux";; three) echo "trois";; four) echo "quatre";; ve) echo "cinq";; six) echo "six";; seven) echo "sept";; eight) echo "huit";; nine) echo "neuf";;

ACS-294- Unix (Winter Term, 26-7) Part II: Shell Programming Page 46 *) echo "Sorry, I was not trained to translate this word" esac translator.2 ve cinq translator.2 ten Sorry, I was not trained to translate this word && and The shell has two simple s, && and, to make a decision based on whether the preceding succeeds or fails. && causes the following list to be executed if the preceding pipeline returns a zero exit value; executes the following list if the preceding pipeline returns a nonzero exit value. For example, && 2 will execute rst, and if returns an exit value of zero, 2 will be executed. If returns an exit value of nonzero, 2 will be skipped. We can rewrite the rst version of the program classday. to: cat classday.. #!/bin/sh # Program classday, version. date grep "Wed" > /dev/null && echo "It's Wednesday, you have class at 6: pm." date Wed Feb 25 :2:36 CST 25 classday.. It's Wednesday, you have class at 6: pm.