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

Similar documents
Bourne Shell Reference

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

Introduction Variables Helper commands Control Flow Constructs Basic Plumbing. Bash Scripting. Alessandro Barenghi

CS1101: Lecture 9 The Shell as a Programming Language

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

Bashed One Too Many Times. Features of the Bash Shell St. Louis Unix Users Group Jeff Muse, Jan 14, 2009

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

SECTION II: LANGUAGE BASICS

CS 135 Lab Assignments Week 1

CS Unix Tools & Scripting

Documentation for LISP in BASIC

Informatics Ingeniería en Electrónica y Automática Industrial

COMP 4/6262: Programming UNIX

\n is used in a string to indicate the newline character. An expression produces data. The simplest expression

Recursion and Induction: Haskell; Primitive Data Types; Writing Function Definitions

Bits, Words, and Integers

Basic Linux (Bash) Commands

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

CS 25200: Systems Programming. Lecture 10: Shell Scripting in Bash

System Programming. Session 6 Shell Scripting

A shell can be used in one of two ways:

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

CS113: Lecture 3. Topics: Variables. Data types. Arithmetic and Bitwise Operators. Order of Evaluation

CS112 Lecture: Variables, Expressions, Computation, Constants, Numeric Input-Output

Learning the Language - V

RUBY OPERATORS. Ruby Arithmetic Operators: Ruby Comparison Operators:

Two Types of Types. Primitive Types in Java. Using Primitive Variables. Class #07: Java Primitives. Integer types.

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #23 Loops: Precedence of Operators

Output with printf Input. from a file from a command arguments from the command read

CS2900 Introductory Programming with Python and C++ Kevin Squire LtCol Joel Young Fall 2007

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

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

Welcome to the Bash Workshop!

GO - OPERATORS. This tutorial will explain the arithmetic, relational, logical, bitwise, assignment and other operators one by one.

9 Using Equation Networks

sottotitolo A.A. 2016/17 Federico Reghenzani, Alessandro Barenghi

CMPS 12A Introduction to Programming Lab Assignment 7

IT441. Network Services Administration. Perl: File Handles

Essentials for Scientific Computing: Bash Shell Scripting Day 3

Operators. Java operators are classified into three categories:

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

Log into Linux Reminders: Homework 1 due today; Homework 2 due on Thursday Questions?

Bash scripting basics

Shell Scripting. Todd Kelley CST8207 Todd Kelley 1

Vi & Shell Scripting

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

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

Linux shell programming for Raspberry Pi Users - 2

More Scripting Todd Kelley CST8207 Todd Kelley 1

And Parallelism. Parallelism in Prolog. OR Parallelism

9.2 Linux Essentials Exam Objectives

Environment Variables

9/21/17. Outline. Expression Evaluation and Control Flow. Arithmetic Expressions. Operators. Operators. Notation & Placement

Introduction to Programming, Aug-Dec 2008

CSE 452: Programming Languages. Outline of Today s Lecture. Expressions. Expressions and Control Flow

Department of Computer Science

Chapter 5 The Bourne Shell

Scheme: Data. CS F331 Programming Languages CSCE A331 Programming Language Concepts Lecture Slides Monday, April 3, Glenn G.

Introduction. Following are the types of operators: Unary requires a single operand Binary requires two operands Ternary requires three operands

CS112 Lecture: Working with Numbers

Linux Shell Scripting. Linux System Administration COMP2018 Summer 2017

Bash Programming. Student Workbook

The Arithmetic Operators. Unary Operators. Relational Operators. Examples of use of ++ and

More Scripting Techniques Scripting Process Example Script

The Arithmetic Operators

bash, part 3 Chris GauthierDickey

Announcements. Lab Friday, 1-2:30 and 3-4:30 in Boot your laptop and start Forte, if you brought your laptop

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

Work relative to other classes

LECTURE 3 C++ Basics Part 2

Computational Applications in Nuclear Astrophysics using Java Java course Lecture 2

The SPL Programming Language Reference Manual

Operators in C. Staff Incharge: S.Sasirekha

Assoc. Prof. Dr. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

bash Execution Control COMP2101 Winter 2019

Chapter 1 Summary. Chapter 2 Summary. end of a string, in which case the string can span multiple lines.

Last Time. on the website

Java enum, casts, and others (Select portions of Chapters 4 & 5)

CS /534 Compiler Construction University of Massachusetts Lowell. NOTHING: A Language for Practice Implementation

Module 2 - Part 2 DATA TYPES AND EXPRESSIONS 1/15/19 CSE 1321 MODULE 2 1

Scheme: Expressions & Procedures

Review. Preview. Functions. Functions. Functions with Local Variables. Functions with Local Variables 9/11/2017

Shell programming. Introduction to Operating Systems

Basic operators, Arithmetic, Relational, Bitwise, Logical, Assignment, Conditional operators. JAVA Standard Edition

TED Language Reference Manual

Bash command shell language interpreter

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 Data Administrative Shell Scripting COMP2101 Fall 2017

Information Science 1

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

5/8/2012. Exploring Utilities Chapter 5

ECE 122 Engineering Problem Solving with Java

COMP2100/2500 Lecture 17: Shell Programming II

Not Quite C Compiler version 1.0 b1

LECTURE 17. Expressions and Assignment

ECEN 468 Advanced Logic Design

Shell scripting and system variables. HORT Lecture 5 Instructor: Kranthi Varala

Grep and Shell Programming

Lexical Considerations

There are four numeric types: 1. Integers, represented as a 32 bit (or longer) quantity. Digits sequences (possibly) signed are integer literals:

Transcription:

Advanced Unix Tools Lecture 6 CS214 Spring 2004 Friday March, 2004 Here redirection Recall that redirection allows you to redirect the input to a command from a file (using <), or the output to a file (using >, >>, depending on whether we want to overwrite the file or append to it). A special form of redirection is useful, called a here document. It allows you to redirect input not from a file but from explictly provided data. The general form of this form of redirection is as follows: << FLAG line1 line2 line3 line4 FLAG This executes, feeding it line1, line2, line3 and line4 as input. The FLAG after << indicates until where to read the input; the first line containing FLAG by itself is the end of the input to feed to the command. The terminating expression need not be FLAG. It can be anything. Generally, it will be something that does not appear in the text to feed to the command. Note that expansion is performed on the lines to pass to the command. Expansion is not performed on FLAG. Here s a concrete example, to mail a piece of text to joe : mail -s "value of path var" joe << XYZ Joe, here s the value of my PATH variable: $PATH XYZ Case statement The case statement is sometimes a nice alternative to conditionals, as it allows for some amount of pattern matching. 1

case word in patt... patt ) patt... patt ) esac As usual, the argument word is expanded according to the shell rules. Then, the system goes through each clause and sees if word matches one of the patterns in the clause. (Patterns in a clause are separated by.) A pattern uses the same special symbols as pathname expansion (*,?, [...],...). For example, both unix and linux match the pattern *n?x. The first time a match is found, the corresponding commands are executed. A typical example of use of case is to conditionally set variables depending, say, on the operating system you are running. To get a hold of the operating system, you can either refer to the bash environment variable OSTYPE, or use the unix command uname. Let s use the latter. The command uname returns information about the system you are currently running. Command-line options controls the exact information returned. What we want is uname -s, which returns the name of the operating system: case $(uname -s) in in?inux) echo "You re running linux..." opsystem= LINUX Sun*) echo "You re running SunOS..." opsystem= SUNOS *) echo "Unrecognized operating system..." exit 1 esac Notice the use of the pattern * as a catch-all default case. 2

Arithmetic expressions The bash shell allows some amount of arithmetic expressions to be evaluated directly. Two forms are used. The first is a substitution form. The shell will expand the form $(( expr )) by evaluating the arithmetic expression expr and substituting the result. Arithmetic is performed using long integers, without checks for overflow. Division by zero is reported as an error. Note that expr is treated as though it were inside double-quotes; hence, variable and command substitution can occur, but not pathname expansion. Operations are reminiscent of C operations. Logical values for integers is as in C: true is zero, false is non-zero. These operations are listed in the decreasing order of precedence: - + unary minus, plus! logical, bitwise negation ** exponentiation * / % multiplication, division, remainder + - addition, subtraction << >> bitwise shifts <= >= < > comparisons ==!= equality, inequality & bitwise AND ^ bitwise exclusive OR bitwise OR && logical AND logical OR expr?expr:expr conditional evaluation = *= /= %= += -= assignment Parentheses can be used to affect the order of operations, as usual. If you use an identifier such as foo in the arithmetic expression, the value of the corresponding shell variable is used. The value is coerced into an integer. If the value does not correspond to an integer, 0 is used. Consider the following simple examples: $ echo $((1 + 1)) 2 $ foo=10 $ echo $((foo)) 10 $ echo $((foo * 2)) 20 $ echo $((foo = 40)) 3

40 $ echo $foo 40 Notice the use of assignment in echo $((foo = 40)). The variable foo is assigned value 40, which has the side-effect of changing the value of the shell variable foo to 40. Hence, arithmetic evaluation can affect the value of shell variables, which can be quite useful. Note that variable substitution occurs in $((...)) before arithmetic evaluation (refer to lecture 2). A priori, there is no difference between $((foo + 20)) and $(($foo + 20)), except that in the latter case, the shell performs variable substitution before performing the arithmetic evalation, so that if foo has value 10, then the shell will in fact evaluate $((10 + 20)), never seeing that 10 came from a shell variable. This can lead to some pretty interesting effects. If you write $(($foo = 20)), you can put the name of a variable to update in foo, and variable substitution will plug it in the arithmetic expression before evaluating it. So you can parametrize an expression by the variables it updates. For instance: $ a=10 $ b=20 $ c=30 $ foo=a $ echo $(($foo=)) $ echo $a $ echo $b 20 $ echo $c 30 An alternative form exists, typically used to assign the result of evaluating expressions to variables. The shell command let simply evaluates its arguments as arithmetic expressions. Since no substitution occurs, the only way for this to be useful is for its side-effects, i.e., the assignment of values to variables. For example: $ a=10 $ let c=a*2+a*3 $ echo $c 0 If you have more complicated arithmetic to perform, say, with floating point numbers, then you can revert to an external command. The command bc implements a fairly complete calculator, 4

evaluating expressions from stdin and returning the results to stdout. (Although some amount of care is needed to catch errors, etc.) Invoking bc -l will provide access to the full standard math library. For instance, to compute the sine of 3.4 and assigning it to shell variable result, you can use: $ result=$(echo s (3.4) bc -l) $ echo $result -.24110202683131924