Conditional Control Structures. Dr.T.Logeswari

Similar documents
Shell programming. Introduction to Operating Systems

Control Structures. CIS 118 Intro to LINUX

CSC 2500: Unix Lab Fall 2016

bash Execution Control COMP2101 Winter 2019

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

COMP 4/6262: Programming UNIX

Title:[ Variables Comparison Operators If Else Statements ]

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

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

Computer Systems and Architecture

Shell Control Structures

Shells & Shell Programming (Part B)

Shell Programming (bash)

Bourne Shell Reference

LOG ON TO LINUX AND LOG OFF

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

Computer Systems and Architecture

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

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

Bash scripting basics

Shell Control Structures

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

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

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

9.2 Linux Essentials Exam Objectives

UNIX shell scripting

SHELL SCRIPT BASIC. UNIX Programming 2014 Fall by Euiseong Seo

CS Unix Tools & Scripting

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

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

SHELL SCRIPT BASIC. UNIX Programming 2015 Fall by Euiseong Seo

Windshield. Language Reference Manual. Columbia University COMS W4115 Programming Languages and Translators Spring Prof. Stephen A.

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

Week 6 Lesson 1: Control-Flow Statements (Continued)

Linux shell programming for Raspberry Pi Users - 2

Essentials for Scientific Computing: Bash Shell Scripting Day 3

Practice 5 Batch & Shell Programming

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

Chapter 5 The Bourne Shell

Vi & Shell Scripting

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

UNIX Programming Laboratory. Subject Code: 10MCA17

Shell Start-up and Configuration Files

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

Shell Programming (ch 10)

Linux Bash Shell Scripting

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.

Bash shell programming Part II Control statements

Practical 04: UNIX / LINUX Shell Programming 1

Useful Unix Commands Cheat Sheet

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

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

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

CS214 Advanced UNIX Lecture 4

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

Advanced Unix Programming Module 03 Raju Alluri spurthi.com

Linux Shell Script. J. K. Mandal

bash Tests and Looping Administrative Shell Scripting COMP2101 Fall 2017

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

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

Shells and Shell Programming

Linux Shell Scripting. Linux System Administration COMP2018 Summer 2017

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

Shells and Shell Programming

A shell can be used in one of two ways:

BASH Programming Introduction

Assignment clarifications

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

Lab 4: Shell scripting

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

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

Linux 系统介绍 (III) 袁华

Basic UNIX Commands BASIC UNIX COMMANDS. 1. cat command. This command is used to create a file in unix. Syntax: $ cat filename

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

What is Bash Shell Scripting?

Bourne Shell Programming Topics Covered

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

RHCE BOOT CAMP. System Administration

DAVE LIDDAMENT INTRODUCTION TO BASH

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

Last Time. on the website

DECISION CONTROL AND LOOPING STATEMENTS

Shell Programming (Part 2)

Shell. SSE2034: System Software Experiment 3, Fall 2018, Jinkyu Jeong

CSE 15L Winter Midterm :) Review

Indian Institute of Technology Kharagpur. PERL Part II. Prof. Indranil Sen Gupta Dept. of Computer Science & Engg. I.I.T.

Operating Systems. Lab Manual. Compiled by: Mr. Mohammed Al-Qadhi Computer Science Department

CS1101: Lecture 9 The Shell as a Programming Language

Introduction. C provides two styles of flow control:

Lecture 02 The Shell and Shell Scripting

n Group of statements that are executed repeatedly while some condition remains true

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

8. Control statements

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

Week 2. Relational Operators. Block or compound statement. if/else. Branching & Looping. Gaddis: Chapters 4 & 5. CS 5301 Spring 2018.

CSNB113: System Administration - 8 th Topic: Shell Scripts Programming I

Shell Programming Overview

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

More Scripting Techniques Scripting Process Example Script

Transcription:

Conditional Control Structures Dr.T.Logeswari

TEST COMMAND test expression Or [ expression ] Syntax Ex: a=5; b=10 test $a eq $b ; echo $? [ $a eq $b] ; echo $? 2 Unix Shell Programming - Forouzan 2

TEST COMMAND Numeric test String test 3 Unix Shell Programming - Forouzan 3

NUMERIC COMPARISON OPERATORS MEANING USAGE -eq Equal to if [ 5 eq 6 ] -ne Not equal to if [ 5 ne 6 ] -lt Less than if [ 5 lt 6 ] -le Less than or equal to if [ 5 le 6 ] -gt Greater than if [ 5 gt 6 ] -ge Greater than or equal to if [ 5 ge 6 ] Output: True : $?=0; False : $?=1 Unix Shell Programming - Forouzan 4 4

STRING COMPARISON OPERATORS str1 = str2 MEANING str1 is equal to str2 str1!= str2 str1 is not equal to str2 str1 str1 is not null or not defined -n str1 str1 is not null and exists -z str1 str1 is null and exists 5 Unix Shell Programming - Forouzan 5

STRING COMPARISON = sign must be preceded and followed by at least one blank space If string contains more than one word separated by white space, then they must be enclosed in double quotes Ex: str1= New Horizon College While comparing such strings they must be enclosed in quotes Ex: [ str1 = str2 ] 6 Unix Shell Programming - Forouzan 6

Exit command Terminates the execution of shell scripts If program is executed successfully, it returns non-zero; otherwise zero value is returned $? : variable that stores the status of exited command 7 Unix Shell Programming - Forouzan 7

Introduction Conditional control structure are also known as branching control structure or selection structures Decision making can be carried out by using branching control structure or selection structures

Branching Control structures If then fi statement If then else fi statement If then elif else fi statement Case easc statement

If then fi statement if conditional expression then true block fi statements are executed only if command succeeds, i.e. has return status 0 $?= 0, if true $?=1, if false 10

Find largest of two numbers Clear echo enter two number Read a b large=$a If [ $b gt $large ]; then Large=$b fi

If then else fi statement if conditional expression then true block else false block fi 12

Leap year or not echo enter a year read year x=`expr $year % 4` If [ $x eq 0 ] Then echo $year is a leap year else echo $year is not leap year fi

clear echo enter a number read n if [expr $num % 2` -eq 0] then echo n is a even number else echo n is not a even number fi Odd or Even

What is wrong with this interactive shell script? echo What month is this? read $month echo $month is as good a month as any.

In a file word UNIX is appearing many times? How will you count number?

grep -c "Unix" filename

Write a script that will show the following as output: Give me a U! U! Give ma a N! N! Give me a I! I! Give me a X! X!

for i in U N I X echo Give me a $i! echo $i! done

Write a script that prints out date information in this order: time, day of week, day number, month, year(sample output: 17:34:51 PDT Sun 12 Feb 2012) Sat march 15 14 : 35 :30 IST 2018

Sat march 15 14 : 35 :30 IST 2018 set date echo $4 $5 $1 $3 $2 $6

if then elif else fi statement if [ condition1 ]; then statement1 elif [ condition2 ]; then statement2 elif [ condition3 ]; then statement3 else default_statement fi The word elif stands for else if It is part of the if statement and cannot be used by itself 22

Find whether a number is positive, negative or zero echo enter a number Read num if [ $num gt 0 ]; then echo $num is positive elif [ $num lt 0 ]; then echo $num is negative elif [ $num eq 0 ]; then echo $num is zero else echo kindly enter a valid input fi

case esac statement Used for a decision that is based on multiple choices Syntax: case value in pattern1) command-list1 ;; pattern2) command-list2 ;; patternn) command-listn ;; *) default-list ;; esac 24

The value is compared against the patterns until a match is found The case statement starts with the keywords case and ends with the keyword easc Block of commands attached to every pattern must be terminated with double semicolon(;;) but not compulsory with default pattern The default *) pattern gets executed when no match is found Case patterns (label) can be in any order

Unix commands using case statement 1) display list of files 2) display todays date 3) display calendar 4) display logged user 5) display current directory 6) quit echo menu echo 1.list of files echo 2.todays date echo 3.display month of calender echo 4.logged user echo 5.display current directory echo 6.quit echo"enter the choice" read ch case $ch in 1) ls ;; 2) date ;; 3)cal ;; 4) who ;; 5)pwd ;; 6) exit ;; *) echo invalid choice ;; esac

Looping control structures Loops are required whenever a set of statement must be executed repeatedly The repeated execution also need decision making to terminate the loop The three types of looping are while loop for loop until loop

while loop To execute commands in command-list as long as expression evaluates to true Syntax: while [ expression ] do command-list done 28

Sum of digits clear sum=0 echo "enter a number" read num n=$sum while [ $num -gt 0 ] do rem=`expr $num % 10` sum=`expr $sum + $rem` num=`expr $num / 10` done echo the sum of digit of $n is $sum

EXAMPLE: Using while loop COUNTER=0 while [ $COUNTER -lt 10 ] do echo $COUNTER let COUNTER + =1 done 30

Purpose: UNTIL LOOP To execute commands in command-list as long as expression evaluates to false Syntax: until [ expression ] do command-list done 31

EXAMPLE: USING THE UNTIL LOOP #!/bin/bash COUNTER=20 until [ $COUNTER -lt 10 ] do echo $COUNTER let COUNTER - =1 done 32

Purpose: THE FOR LOOP To execute commands as many times as the number of words in the argument-list Syntax: for variable in argument-list do commands done 33

#!/bin/bash EXAMPLE 1: THE FOR LOOP for i in 7 9 2 3 4 5 do echo $i done 34

Jumping control structures Break The break statement is used to exit from a loop structure based on certain condition The break statement cannot exit from nested loops, it can exit only from the loop containing it Syntax: break

Continue The continue statement is used to skip the rest of the statement in a loop and the execution proceeds directly to the next iteration of the loop Syntax exit continue The exit statement is used to terminate a program Syntax exit