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

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

Conditional Control Structures. Dr.T.Logeswari

COMP 4/6262: Programming UNIX

Control Structures. CIS 118 Intro to LINUX

Prof. Navrati Saxena TA: R. Sachan

Shell programming. Introduction to Operating Systems

SHELL SCRIPT BASIC. UNIX Programming 2014 Fall by Euiseong Seo

SHELL SCRIPT BASIC. UNIX Programming 2015 Fall by Euiseong Seo

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

Computer Systems and Architecture

Vi & Shell Scripting

Bash scripting basics

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

Linux Shell Scripting. Linux System Administration COMP2018 Summer 2017

CSC 2500: Unix Lab Fall 2016

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

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

bash Execution Control COMP2101 Winter 2019

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

CS Unix Tools & Scripting

Computer Systems and Architecture

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

Bourne Shell Reference

Useful Unix Commands Cheat Sheet

Title:[ Variables Comparison Operators If Else Statements ]

A shell can be used in one of two ways:

Grep and Shell Programming

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

LING 408/508: Computational Techniques for Linguists. Lecture 5

Lab 5a Shell Script Lab 4 Using Arithmetic Operators in shell script

Practical 04: UNIX / LINUX Shell Programming 1

9.2 Linux Essentials Exam Objectives

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

Bash shell programming Part II Control statements

bash Tests and Looping Administrative Shell Scripting COMP2101 Fall 2017

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

Chapter 5 The Bourne Shell

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

UNIX shell scripting

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

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

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

Assignment clarifications

Advanced Unix Programming Module 03 Raju Alluri spurthi.com

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

Linux shell programming for Raspberry Pi Users - 2

CS1101: Lecture 9 The Shell as a Programming Language

bash Tests and Looping Administrative Shell Scripting COMP2101 Fall 2017

Lab 4: Shell scripting

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

Unix Shell scripting. Dr Alun Moon 7th October Introduction. Notation. Spaces

Shell Control Structures

Shells & Shell Programming (Part B)

Shell Scripting. Todd Kelley CST8207 Todd Kelley 1

Shell Control Structures

Shell Programming (bash)

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

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

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

Practice 5 Batch & Shell Programming

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

Shell Start-up and Configuration Files

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

Original Script. Display commands to manually creating an account. #!/bin/bash

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

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

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

What is 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.

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

Shell Programming (ch 10)

Shell Programming (Part 2)

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

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

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

RHCE BOOT CAMP. System Administration

More Scripting Techniques Scripting Process Example Script

Essential Skills for Bioinformatics: Unix/Linux

Lecture 5. Essential skills for bioinformatics: Unix/Linux

Here redirection. Case statement. Advanced Unix Tools Lecture 6 CS214 Spring 2004 Friday March 5, 2004

Manual Shell Script Linux If Not Equal String Comparison

Bash Shell Programming Helps

LOG ON TO LINUX AND LOG OFF

Essentials for Scientific Computing: Bash Shell Scripting Day 3

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

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

Looping Commands and Working with user Inputs CHAPTER 4

Linux Shell Script. J. K. Mandal

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

bash Args, Signals, Functions Administrative Shell Scripting COMP2101 Fall 2017

Chapter 4. Unix Tutorial. Unix Shell

Linux Systems Administration Shell Scripting Basics. Mike Jager Network Startup Resource Center

Bourne Shell Programming Topics Covered

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

Manual Shell Script Linux If Not Equal String Comparison

bash Args, Signals, Functions Administrative Shell Scripting COMP2101 Fall 2018

Lecture 02 The Shell and Shell Scripting

Linux 系统介绍 (III) 袁华

Assignment 3, Due October 4

The Unix Shell & Shell Scripts

Transcription:

Prof. Navrati Saxena navrati@ece.skku.ac.kr TA: R. Sachan rochak@skku.edu Bharat Jyoti Ranjan

Decision making Loops de-bugging the shell script 2

What is decision.? How to take decision? Knows only 0(zero) and 1(one) Knows only 0(false) and 1(True) How to check? Using bc command type bc press enter Now type 5+7 Output comes in 0 and 1 Try following in bc to clear your Idea and not down bc's response 5 > 12 5 == 10 5!= 2 5 == 5 12 < 2 3

Condition Condition is nothing but comparison between two values Relational An expression is nothing but combination of values, relational operator (su ch as >,<, <> etc) and mathematical operators (such as +, -, / etc ) Expression Meaning > Is grater than < Is less than >= Is grater than or equal <= Is less than or equal == Is equal to!= Is not equal to than 4

if condition which is used for decision making in she ll script, If given condition is true command1 is executed. Syntax: if condition command1 if condition is true or if exit status of condition is 0 (zero)...... fi For compression you can use test or [ expr ] stateme nts or even exist status can be also used. 5

Example $ cat > showfile!/bin/sh Script to print file if cat $1 echo -e "\n\nfile $1, found and successfully echoed" fi Run above script: $ chmod 755 showfile $./showfile foo 6

test command or [ expr ] is used to see if an expression is true, and if it is true it return zero(0), otherwise returns nonzero for false. Syntax: test expression OR [ expression ] Example $ cat > ispositive!/bin/sh Script to see whether argument is positive if test $1 -gt 0 echo "$1 number is positive" fi 7

Run it as follows $ chmod 755 ispostive $ ispostive 5 5 number is positive $ispostive -45 Nothing is printed $ispostive./ispostive: test: -gt: unary operator expected 8

Mathematic al Operator in Shell Script Meaning Normal Arithmetical/ Mathematical Statements But in Shell For test But in Shell statement with if command For [ expr ] statement with if command -eq is equal to 5 == 6 if test 5 -eq 6 if [ 5 -eq 6 ] -ne is not equal to 5!= 6 if test 5 -ne 6 if [ 5 -ne 6 ] -lt is less than 5 < 6 if test 5 -lt 6 if [ 5 -lt 6 ] -le is less than or equal to 5 <= 6 if test 5 -le 6 if [ 5 -le 6 ] -gt is greater than 5 > 6 if test 5 -gt 6 if [ 5 -gt 6 ] -ge is greater than or equal to 5 >= 6 if test 5 -ge 6 If [ 5 -ge 6 ] For Mathematics, use above operator in Shell Script 9

Operator Meaning string1 = string2 string1 is equal to string2 string1!= string2 string1 is NOT equal to string2 string1 string1 is NOT NULL or not defined -n string1 string1 is NOT NULL and does exist -z string1 string1 is NULL and does exist 10

Logical operators are used to combine two or more condition at a time Operator Meaning! expression Logical NOT expression1 -a expression2 Logical AND 11

If given condition is true command1 is executed otherwise command2 is executed. Syntax: if condition condition is zero (true - 0) execute all commands up to else statement else if condition is not true execute all commands up to fi fi 12

Example $ vi isnump_n!/bin/sh Script to see whether argument is positive or negative if [ $ -eq 0 ] echo "$0 : You must give/supply one integers" exit 1 fi if test $1 -gt 0 echo "$1 number is positive" else echo "$1 number is negative" fi Try it as follows: $ chmod 755 isnump_n $ isnump_n 5 5 number is positive $ isnump_n -45 13

the entire if-else construct within either the body of the if state ment of the body of an else statement. Syntax: if condition if condition..... do this else..... do this fi else...... do this fi 14

Example: $ vi nestedif.sh osch=0 echo "1. Unix (Sun Os)" echo "2. Linux (Red Hat)" echo -n "Select your os choice [1 or 2]? " read osch if [ $osch -eq 1 ] ; echo "You Pick up Unix (Sun Os)" else nested if i.e. if within if if [ $osch -eq 2 ] ; echo "You Pick up Linux (Red Hat)" else echo "What you don't like Unix/Linux OS." fi fi 15

Syntax: if condition condition is zero (true - 0) execute all commands up to elif statement elif condition1 condition1 is zero (true - 0) execute all commands up to elif statement else fi condition2 is zero (true - 0) execute all commands up to elif statement elif condition2 None of the above condtion,condtion1,condtion2 are true (i.e. all of the above nonzero or false) execute all commands up to fi 16

Example: $ cat > elf!/bin/sh Script to test if..elif...else if [ $1 -gt 0 ]; echo "$1 is positive" elif [ $1 -lt 0 ] echo "$1 is negative" elif [ $1 -eq 0 ] echo "$1 is zero" else echo "Opps! $1 is not number, give number" fi Try above script as follows: $ chmod 755 elf $./elf 1 $./elf -2 $./elf 0 $./elf a Here o/p for last sample run:./elf: [: -gt: unary operator expected./elf: [: -lt: unary operator expected./elf: [: -eq: unary operator expected Opps! a is not number, give number 17

Computer can repeat particular instruction again and again, until particular condition satisfies. A group of instruction that is executed repeatedly is called a loop Loops for While Note: that in each and every loop First, the variable used in loop condition must be initialized, exe cution of the loop begins. A test (condition) is made at the beginning of each iteration. The body of loop ends with a statement that modifies the value of th e test (condition) variable. 18

Syntax: for { variable name } in { list } do execute one for each item in the list until the list is not finished (And repeat all statement between do and done) done Example: $ cat > testfor for i in 1 2 3 4 5 do echo "Welcome $i times" done Run it script as follows: $ chmod +x testfor $./testfor Note: The for loop first creates i variable and assigned a number to i from the list of number from 1 to 5, The shell execute echo statement for each assignment of i. (This is usually know as iteration) This process will continue until all the items in the list were not finished, because of this it will repeat 5 echo statements. 19

Another Example: $ cat > mtable!/bin/sh Script to test for loop if [ $ -eq 0 ] echo "Error - Number missing form command line argument" echo "Syntax : $0 number" echo "Use to print multiplication table for given number" exit 1 fi n=$1 for i in 1 2 3 4 5 6 7 8 9 10 do echo "$n * $i = `expr $i \* $n`" done Save above script and run it as: $ chmod 755 mtable $./mtable 7 $./mtable 20

Syntax: for (( expr1; expr2; expr3 )) do...... repeat all statements between do and done until expr2 is TRUE Done Example: $ cat > for2 for (( i = 0 ; i <= 5; i++ )) do echo "Welcome $i times" done Run it script as follows: $ chmod +x for2 $./for2 Welcome 0 times Welcome 1 times Welcome 2 times Welcome 3 times Welcome 4 times Welcome 5 times 21

Similar to Nested if statement Example: $ vi nestedfor.sh for (( i = 1; i <= 5; i++ )) Outer for loop do for (( j = 1 ; j <= 5; j++ )) Inner for loop do echo -n "$i " done echo "" print the new line done 22

Loop is executed as long as given condition i s true. Syntax: while [ condition ] do command1 command2 command3..... done 23

Example: $cat > nt1!/bin/sh Script to test while statement if [ $ -eq 0 ] echo "Error - Number missing form command line argument" echo "Syntax : $0 number" echo " Use to print multiplication table for given number" exit 1 fi n=$1 i=1 while [ $i -le 10 ] do echo "$n * $i = `expr $i \* $n`" i=`expr $i + 1` done Run it script as follows: $ chmod 755 nt1 $./nt1 7 24

The case statement is good alternative to Multilevel if--else-fi statement. It enable you to match several values against one variable. Its easier to read and write. case $variable-name in pattern1) command..... command;; pattern2) command..... command;; patternn) command..... command;; *) command..... command;; esac The default is * and its executed if no match is found. 25

Example: $ cat > car if no vehicle name is given i.e. -z $1 is defined and it is NULL if no command line arg if [ -z $1 ] rental="*** Unknown vehicle ***" elif [ -n $1 ] otherwise make first arg as rental Save it by pressing CTRL+D and run it as follows: $ chmod +x car $ car van $ car car $ car Hyundai 26

While programming shell sometimes you need to find the errors (bugs ) in shell script and correct the errors (remove errors - debug). For this purpose you can use -v and -x option with sh or bash comman d to debug the shell script. General syntax is as follows: Option: v Print shell input lines as they are read. x After expanding each simple-command, bash displays the expanded value of PS4 syst em variable, followed by the command and its expanded arguments. sh option { shell-script-name } OR bash option { shell-script-name } 27

Example: $ cat > dsh1.sh Script to show debug of shell tot=`expr $1 + $2` echo $tot Du-bug the Program $ sh -x dsh1.sh 4 5 Script to show debug of shell tot=`expr $1 + $2` expr $1 + $2 ++ expr 4 + 5 + tot=9 echo $tot + echo 9 9 Save it by pressing CTRL+D and run it as follows: $ chmod 755 dsh1.sh $./dsh1.sh 4 5 9 $ sh -v dsh1.sh 4 5 28

Richard Petersen; "The Complete Reference, Linux, Fourth Edition", Osborne/McGraw-Hi ll, Christopher Negus; "Linux Bible, 2007 Editi on", Wiley Publishing, Inc http://www.freeos.com/guides/lsst/ http://bash.cyberciti.biz/guide/main_page 29

rochak@skku.edu