Shell Scripting Printing; Loops and Logic printf: formatted print printf is a more standardized method to print text to the screen. It can be used instead.

Slides:



Advertisements
Similar presentations
1 Chapter 3:Operators and Expressions| SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2006 | Last Updated: July 2006 Slide 1 Operators and Expressions.
Advertisements

CSE4251 The Unix Programming Environment
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.
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.
Bash, part 2 Prof. Chris GauthierDickey COMP Unix Tools.
Operators. Perl has MANY operators. –Covered in Chapter 3 of Prog.Perl Most operators have numeric and string version –remember Perl will convert variable.
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.
JavaScript, Third Edition
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 Programming, or Scripting Shirley Moore CPS 5401 Fall August 29,
Shell Scripting Awk (part1) Awk Programming Language standard unix language that is geared for text processing and creating formatted reports but it.
4. Python - Basic Operators
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.
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.
2440: 211 Interactive Web Programming Expressions & Operators.
1 Working with Data Types and Operators. 2 Using Variables and Constants The values stored in computer memory are called variables The values, or data,
C Programming n General Information on C n Data Types n Arithmetic Operators n Relational Operators n if, if-else, for, while by Kulapan Waranyuwat.
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.
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)
Basic Operators. What is an operator? using expression is equal to 9. Here, 4 and 5 are called operands and + is the operator Python language supports.
1 © 2001 John Urrutia. All rights reserved. Chapter 10 using the Bourne Again Shell.
Perl Language Yize Chen CS354. History Perl was designed by Larry Wall in 1987 as a text processing language Perl has revised several times and becomes.
Chapter 10: BASH Shell Scripting Fun with fi. In this chapter … Control structures File descriptors Variables.
C Operators. CONTENTS C OPERATORS TYPES OF OPERATOR UNARY BINARY TERNARY ARITHMATIC RELATIONAL LOGICAL.
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.
1 P51UST: Unix and Software Tools Unix and Software Tools (P51UST) More Shell Programming Ruibin Bai (Room AB326) Division of Computer Science The University.
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 Lecture 9 Shell Programming – Command substitution Regular expressions and grep Use of exit, for loop and expr commands COP 3353 Introduction to UNIX.
CSCI 330 UNIX and Network Programming Unit IX: Shell Scripts.
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,
Week Five Agenda Link of the week Review week four lab assignment This week’s expected outcomes Next lab assignment Break-out problems Upcoming deadlines.
OperatorstMyn1 Operators An operator is something that you feed with one or more values (or expressions, in programming jargon) which yields another value.
Gator Engineering Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 3 Formatted Input/Output.
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.
ECE 103 Engineering Programming Chapter 4 Operators Herbert G. Mayer, PSU CS Status 6/19/2015 Initial content copied verbatim from ECE 103 material developed.
Chapter 5 The Bourne Shell Graham Glass and King Ables, UNIX for Programmers and Users, Third Edition, Pearson Prentice Hall, Notes by Michael Weeks.
#!/bin/sh 2002/07/26 Jaeho Shin. Interface between user and the operating system sh, bash, csh, tcsh, ksh, … What is a “Shell”? OSuser Shell.
Bash Shell Scripting 10 Second Guide.
CSC 352– Unix Programming, Spring 2016, Final Exam Guide
University of Central Florida COP 3330 Object Oriented Programming
Agenda Bash Shell Scripting – Part II Logic statements Loop statements
Operators Operators are symbols such as + (addition), - (subtraction), and * (multiplication). Operators do something with values. $foo = 25; $foo – 15;
CSC 352– Unix Programming, Fall 2012
University of Central Florida COP 3330 Object Oriented Programming
Expressions and Control Flow in JavaScript
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
Chapter 5 The Bourne Shell
CSC 352– Unix Programming, Fall, 2011
Introduction to Bash Programming, part 3
Review The Unix Shells Graham Glass and King Ables,
Presentation transcript:

Shell Scripting Printing; Loops and Logic

printf: formatted print printf is a more standardized method to print text to the screen. It can be used instead of echo and in fact is what is commonly used in programming and other scripting languages (C, C++, Perl) #!/bin/bash -f printf “Hello world.\n” \n represents a newline a=`echo "Hello world." | wc | awk '{print $2}' ` printf "This phrase contains %d words\n" $a %./hello.sh Hello world. This phrase contains 2 words

printf: format specifiers %s String %c ASCII character. %d Decimal integer %f Floating-point format %E Scientific notation floating-point format

#!/bin/bash -f printf “Hello world.\n” \n represents a newline a=`echo "Hello world." | wc | awk '{print $2}' ` printf "This phrase contains %5.2f words\n" $a

Arithmetic bash shell arithmetic resembles C programming language arithmetic. In bash, the syntax $(( )) can be used to calculate arithmetic expressions or to set variables to complex arithmetic expressions %echo $(( 3+4 )) 7 %echo $(( x = 2 )) 2

Basic Arithmetic Operators shell arithmetic is integer only + : addition - : subtraction * : multiplication / : division % : remainder or modulus %echo (( 10%3 )) 1 %echo $(( 10/3 )) 3

Assignment Operators = : set variable equal to value on right %x = 2; echo $x 2 += : set variable equal to itself plus the value on right %x = 2; echo $(( x +=2 )) 4 -= : set variable equal to itself minus the value on right %x = 2; echo $((x -= 2)) 0

Assignment Operators *= : set variable equal to itself times the value on right %x = 2; echo $((x *= 4)) 8 /= : set variable equal to itself divided by value on right %x = 2; echo $((x/= 2)) 1 %= : set variable equal to the remainder of itself divided by the value on the right %x = 4; echo $((x %= 3)) 1

Unary Operations A unary expression contains one operand and one operator ++ : increment the operand by 1 if ++ occurs after, $x++, the original value of the operand is used in the expression and then incremented if ++ occurs before, ++$x, the incremented value of the operand is used in the expression -- : decrement the operand by 1 + : unary plus maintains the value of the operand, x=+x - : unary minus negates the value of the operand, -1*x=-x ! : logical negation evaluates if the operand is true (returns 1) or false (returns 0)

Relational Operators Returns 1 if true and 0 if false All relational operators are left to right associative < : test for less than <= : test for less than or equal to > : test for greater than >= : test for greater than or equal to == : test for equal to != : test for not equal

Boolean (Logical) Operators Boolean operators return 1 for true and 0 for false && : logical AND; tests that both expressions are true left to right associative %echo $(( (3 < 4) && (10<15) )) 1 %echo $(( (3 15) )) 0 || : logical OR ; tests that one or both of the expressions are true left to right associative %echo $(( (3 15) )) 1 ! : logical negation; tests that expression is true

Bitwise Operators Bitwise Operators treat scalars as 16-point bit binary values Example: 4019 equals ~ : bitwise negation changes 0 to 1 and vice versa & : bitwise AND ^ : bitwise exclusive OR | : bitwise OR << : bitwise left shift >> : bitwise right shift

test test: condition evaluation utility common scripting tool that tests expressions and many details about files using a long list of flags returns 0 if expression true and 1 if expression false or does not exist two formats in bash scripting test flag expression [ flag expression ]

useful flags return true (0) if -d : expression is a directory -f : expression is a regular file -e : expression is any type of file -w : expression is a writable file -x : expression is an executable (file or directory) -n : expression is a nonzero length string -z : expression is a zeo length string if [ -e ~/SUMA.NEW.loc ] then commands else exit $? #unsuccessful execution of script fi

String tests, true if s1 = s2 : strings s1 and s2 are identical. s1 != s2: strings s1 and s2 are not identical. s1 < s2 : string s1 comes before s2 based on the ASCII value s1 > s2 : string s1 comes after s2 based on the ASCII value Algebraic integer tests, true if n1 -eq n2 : integers n1 and n2 are algebraically equal. n1 -ne n2 : integers n1 and n2 are not algebraically equal. n1 -gt n2 : integer n1 is algebraically greater than the integer n2. n1 -ge n2 : integer n1 is algebraically greater than or equal to the integer n2. n1 -lt n2 : integer n1 is algebraically less than the integer n2. n1 -le n2 : integer n1 is algebraically less than or equal to the integer n2.

if/then/fi If expression is true, then run the following. End the if command if [ $1 = "Heather” ] then printf "Hi %s. We were expecting you.\n" $1 fi

if/then/else/fi If expression is true, then run the first set of commands. Else if false, run the second set of commands. End the if command if [ $1 = "Heather” ] then printf "Hi %s. We were expecting you.\n" $1 else printf "Hi %s. Nice to meet you.\n" $1 fi

if/then/elif/else/fi If expression is true, then run the first set of commands. Else if a second expression is true, run the second set of commands. Else if neither is true, run a third set of commands. End the if command if [ $1 = "Heather” ] then printf "Hi %s. We were expecting you.\n" $1 elif [ $1 = "Andy" ] printf "Hi %s. We were expecting you too.\n" $1 else printf "Hi %s. Nice to meet you.\n" $1 fi

if/then/else/fi If either of the expressions is true, then run the first set of commands. Else if the second expression is true, run the second set of commands. Else if nothing as been true, run a third set of commands. End the if command if [ $1 = "Heather” ] || [ $1 = “Andy” ] then printf "Hi %s. We were expecting you.\n" $1 elif [ $1 = "Andy" ] printf "Hi %s. We were expecting you too.\n" $1 else printf "Hi %s. Nice to meet you.\n" $1 fi Would this script ever output “We were expecting you too”?

do/done do the following set of commands until done in bash, this construct is used in conjunction with loop structures for, while, and until

for one of the most common loop structures is the for loop, which iterates over an array of objects for x values in array, do this a=" ” for i in $a do echo $(( ++i )) done What is the first integer printed to the screen?

while while: continues to loop as long as condition exited successfully #!/bin/sh cat cities.d #Create the file cities.d HANOILM LIMALM SUVALM EOF while read clon clat city junk #setting the variables clon clat and city for each line do echo $city $clon $clat done < cities.d %sh –f junk.sh HANOI LIMA SUVA

until until: continues to loop as long as condition exits unsuccessfully the until loop is used much less than the while loop #!/bin/bash myvar=0 until [ $myvar –eq 5 ]#until this expression is false do echo $myvar myvar=$(( $myvar + 1 )) done %sh –f junk.sh

break break: allows you to break out of a loop can be used with a number to specify what do loop to break out of while condition1 # Outer loop, loop 2 do... while condition2 # Inner loop, loop 1 do... break 2 # Break out of outer loop done... # Execution continues here after break

foreach : a tcsh command the foreach command is a powerful way to iterate over files from the tcsh command line %foreach file ( 828/*BHZ* ) #set variable file to each sac file foreach? echo $file foreach? set name = `echo $file | cut -f2 -d'/' ` foreach? set sta = `echo $name | cut -f1 -d'.' ` foreach? echo “copy $file to $sta.BHZ.SAC foreach? cp $file $sta.BHZ.SAC foreach? end 828/GAR.BHZ_00.D : copy 828/GAR.BHZ_00.D : to GAR.BHZ.SAC

case in tcsh syntax foreach plane( ) set cnt=`expr $cnt + 1` switch ($cnt) case 1: set xpos=-5. set ypos=4.75 set min=-2.5 set max=2.5 breaksw case 2: set xpos=-6.6 set ypos=-3.5 set min=2.5 set max=7 breaksw endsw excessive amounts of GMT end