Shell script – part 2 CS 302. Special shell variable $0.. $9  Positional parameters or command line arguments  For example, a script myscript take 2.

Slides:



Advertisements
Similar presentations
IF statement (i) Single statement. IF ( logical expression ) statement Example: read(*,*) a if (a. lt. 0) a = -a write(*,*) a Or read(*,*) a if (a < 0)
Advertisements

CIS 240 Introduction to UNIX Instructor: Sue Sampson.
Linux Shell Script. Shell script The first line is used to specify shell program #!/bin/sh Variables variable=“text” variable=0 variable=`program arguments`
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.
Information Networking Security and Assurance Lab National Chung Cheng University 1 if condition Condition is defined as: "Condition is nothing but comparison.
Bash, part 2 Prof. Chris GauthierDickey COMP Unix Tools.
Flow of Control MINS298c Fall 1998 Chapter 9. Overview ABAP Programming Structures for: –Iteration –Decisions Control Flow –If … Then –Do & While loops.
Further Shell Scripting Michael Griffiths Corporate Information and Computing Services The University of Sheffield
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.
Lab 8 Shell Script Reference:
Shell Control Structures CSE 2031 Fall August 2015.
Shell Programming, or Scripting Shirley Moore CPS 5401 Fall August 29,
LINUX Shell Scripting Advanced Issues
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.
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.
Bash shell programming Part II – Control statements Deniz Savas and Michael Griffiths November 2005 Corporate Information and Computing Services The University.
CMPSC 60: Week 6 Discussion Originally Created By: Jason Wither Updated and Modified By: Ryan Dixon University of California Santa Barbara.
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.
#!/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 scripting. Number check vi isnump_n #!/bin/sh # # Script to see whether argument is positive or negative # if [ $# -eq 0 ] then echo "$0 : You must.
1 System Administration Introduction to Scripting, Perl Session 3 – Sat 10 Nov 2007 References:  chapter 1, The Unix Programming Environment, Kernighan.
CSC 352– Unix Programming, Spring 2015 March 2015 Shell Programming (Highlights only)
Bash shell programming Part II – Control statements Deniz Savas and Michael Griffiths Corporate Information and Computing Services The University.
1 © 2001 John Urrutia. All rights reserved. Chapter 10 using the Bourne Again Shell.
Shell Programming. Creating Shell Scripts: Some Basic Principles A script name is arbitrary. Choose names that make it easy to quickly identify file function.
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.
(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.
©Colin Jamison 2004 Shell scripting in Linux Colin Jamison.
Xuan Guo Chapter 5 The Bourne Shell Graham Glass and King Ables, UNIX for Programmers and Users, Third Edition, Pearson Prentice Hall, Notes by Michael.
1 © 2000 John Urrutia. All rights reserved. Session 5 The Bourne Shell.
Shell Script2 Reference: Linux Shell Scripting Tutorial v1.05r3 A Beginner's handbook
Shell Programming Features “Full” programming language “Full” programming language Conditional statements Conditional statements Arithmetic, String, File,
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
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]
Chapter 5 The Bourne Shell Graham Glass and King Ables, UNIX for Programmers and Users, Third Edition, Pearson Prentice Hall, Notes by Michael Weeks.
Control Flow (Python) Dr. José M. Reyes Álamo. 2 Control Flow Sequential statements Decision statements Repetition statements (loops)
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.
CSC 352– Unix Programming, Spring 2016, Final Exam Guide
Agenda Bash Shell Scripting – Part II Logic statements Loop statements
CSC 352– Unix Programming, Fall 2012
Agenda Control Flow Statements Purpose test statement
Control Structures: if Conditional
Control Structures: for & while Loops
Command Substitution Command substitution is the mechanism by which the shell performs a given set of commands and then substitutes their output in the.
Presented by, Mr. Satish Pise
Shell Control Structures
Chapter 5 The Bourne Shell
CSC 352– Unix Programming, Fall, 2011
Shell Control Structures
The Selection Structure
Introduction to Bash Programming, part 3
Bash Scripting CS 580U - Fall 2018.
REPETITION Why Repetition?
Review The Unix Shells Graham Glass and King Ables,
Presentation transcript:

Shell script – part 2 CS 302

Special shell variable $0.. $9  Positional parameters or command line arguments  For example, a script myscript take 2 arguments, foo and bar  Using special variable, you can access your script and any parameters it has. myscript $0 foo $1 Bar $2 $# tells you how many parameter your script was given myscript foo bar

If condition  if condition is used for decision making in shell script, If given condition is true then command1 is executed.  Syntax:  if condition  then  command1...  fi

test expression or [ expr ] Is used to see if an expression is true Syntax: test expression OR [ expression ] Script.txt # Script to see whether argument is positive if test $1 -gt 0 then echo "$1 number is positive" fi Run it as follows: sh script.txt 3

test expression or [ expr ] For test statement with if commandFor [ expr ] statement with if command Mathematical Operator in Shell Script Meaning if [ 5 -eq 6 ]if test 5 -eq 6 is equal to -eq if [ 5 -ne 6 ]if test 5 -ne 6 is not equal to -ne if [ 5 -lt 6 ]if test 5 -lt 6 is less than -lt if [ 5 -le 6 ]if test 5 -le 6 is less than or equal to -le if [ 5 -gt 6 ]if test 5 -gt 6 is greater than -gt if [ 5 -ge 6 ]if test 5 -ge 6 is greater than or equal to -ge

test expression or [ expr ] String Comparisons MeaningOperator string1 is equal to string2 string1 = string2 string1 is NOT equal to string2 string1 != string2 string1 is NOT NULL or not defined string1 string1 is NOT NULL and does exist -n string1 string1 is NULL and does exist -z string1

test expression or [ expr ] Logical Operators MeaningOperator Logical NOT ! expression Logical AND expression1 -a expression2 Logical OR expression1 -o expression2

If.. else.. fi  If given condition is true then command1 is executed otherwise command2 is executed.  Syntax:  if condition  then  command1  else  if condition is not true then execute all commands up to fi  fi

Example (1) if [ $# -eq 0 ] then echo " Enter one argument “ exit 1 else echo " The first argument is $1 " fi

Class exercise (1)  Write a Script to see whether argument is positive or negative. First, you should ensure that a user enter an argument, if not exist and print this message “You must give/supply one integers”

# Script to see whether argument is positive or negative if [ $# -eq 0 ] then echo “You must give/supply one integer" exit 1 fi if test $1 -ge 0 then echo "$1 number is positive" else echo "$1 number is negative" fi

Nested if-else-fi Script.txt choice=0 echo "1. for coffee" echo "2. for tea" echo -n "select your drink choice [1 or 2] ?" read choice if test $choice -eq 1 then echo "you chosen coffee" else if test $choice -eq 2 then echo "you chosen tea" else echo "invalid input enter 1 or 2" fi

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

Example : Script.txt if test $1 -gt 0 then echo "positive" elif test $1 -lt 0 then echo "negative" elif test $1 -eq 0 then echo "Zero" else echo "Not a number !" fi

For loop Syntax: for { variable name } in { list } Do execute one for each item in the list until the list is not finished execute one for each item in the list until the list is not finisheddone or: for ((initial vale ; condition ;)) Do execute one for each item in the list until the list is not finished execute one for each item in the list until the list is not finisheddone

Example Script.txt for NUMBER in do echo "welcome ! $NUMBER" done Script.txt LIMIT = 10 for (( i = 1 ; i <= LIMIT; i++ )) do echo "Welcome $i times" done

While loop Syntax : while [ condition ] do do command1 command1 command2 command2 command3 command done

Example Script.txt i=$1 while test $i != 0 do echo –n "$i," i=`expr $i - 1` done echo " "