Agenda Positional Parameters / Continued... Command Substitution Bourne Shell / Bash Shell / Korn Shell Mathematical Expressions Bourne Shell / Bash Shell.

Slides:



Advertisements
Similar presentations
CIS 240 Introduction to UNIX Instructor: Sue Sampson.
Advertisements

Introduction to Unix – CS 21 Lecture 11. Lecture Overview Shell Programming Variable Discussion Command line parameters Arithmetic Discussion Control.
Understanding bash Prof. Chris GauthierDickey COMP 2400, Fall 2008.
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
Linux+ Guide to Linux Certification, Second Edition
More Shell Basics CS465 - Unix. Unix shells User’s default shell - specified in /etc/passwd file To show which shell you are currently using: $ echo $SHELL.
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.
Guide To UNIX Using Linux Third Edition
Introduction to Unix (CA263) Introduction to Shell Script Programming By Tariq Ibn Aziz.
Shell Programming 1. Understanding Unix shell programming language: A. It has features of high-level languages. B. Convenient to do the programming. C.
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.
Shell Script Examples.
Shell Control Structures CSE 2031 Fall August 2015.
Chapter Seven Advanced Shell Programming. 2 Lesson A Developing a Fully Featured Program.
Agenda Control Flow Statements Purpose test statement if / elif / else Statements for loops while vs. until statements case statement break vs. continue.
Introduction to Shell Script Programming
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.
Writing Shell Scripts ─ part 1 CSE 2031 Fall September 2015.
1 Operating Systems Lecture 3 Shell Scripts. 2 Brief review of unix1.txt n Glob Construct (metacharacters) and other special characters F ?, *, [] F Ex.
Week 7 Working with the BASH Shell. Objectives  Redirect the input and output of a command  Identify and manipulate common shell environment variables.
Agenda User Profile File (.profile) –Keyword Shell Variables Linux (Unix) filters –Purpose –Commands: grep, sort, awk cut, tr, wc, spell.
Chapter 5 Bourne Shells Scripts By C. Shing ITEC Dept Radford University.
An Introduction to Unix Shell Scripting
The UNIX Shell. The Shell Program that constantly runs at terminal after a user has logged in. Prompts the user and waits for user input. Interprets command.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
Shell Script Programming. 2 Using UNIX Shell Scripts Unlike high-level language programs, shell scripts do not have to be converted into machine language.
Linux+ Guide to Linux Certification, Third Edition
Writing Shell Scripts ─ part 3 CSE 2031 Fall October 2015.
UNIX Shell Script (1) Dr. Tran, Van Hoai Faculty of Computer Science and Engineering HCMC Uni. of Technology
CS465 - UNIX The Bourne Shell.
Shell Programming. Introducing UNIX Shells  Shell is also a programming language and provides various features like variables, branching, looping and.
LINUX programming 1. INDEX UNIT-III PPT SLIDES Srl. No. Module as per Session planner Lecture No. PPT Slide No. 1.Problem solving approaches in Unix,Using.
1 System Administration Introduction to Scripting, Perl Session 3 – Sat 10 Nov 2007 References:  chapter 1, The Unix Programming Environment, Kernighan.
Linux+ Guide to Linux Certification Chapter Eight Working with the BASH Shell.
CSC 352– Unix Programming, Spring 2015 March 2015 Shell Programming (Highlights only)
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.
1 Operating Systems Lecture 2 UNIX and Shell Scripts.
LINUX programming UNIT-2 1. INDEX UNIT-IV PPT SLIDES Srl. No. Module as per Session planner Lecture No. PPT Slide No. 1.Working with Bourne shell L1 1-2.
Chapter 10: BASH Shell Scripting Fun with fi. In this chapter … Control structures File descriptors Variables.
(A Very Short) Introduction to Shell Scripts CSCI N321 – System and Network Administration Copyright © 2000, 2003 by Scott Orr and the Trustees of Indiana.
Writing Scripts Hadi Otrok COEN 346.
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.
1 © 2000 John Urrutia. All rights reserved. Session 5 The Bourne Shell.
Chapter Six Introduction to Shell Script Programming.
CSCI 330 UNIX and Network Programming Unit IX: Shell Scripts.
Lesson 3-Touring Utilities and System Features. Overview Employing fundamental utilities. Linux terminal sessions. Managing input and output. Using special.
Agenda The Bourne Shell – Part II Special Characters Ambiguous File Reference Variable Names and Values User Created Variables Read-only Variables (Positional.
Linux+ Guide to Linux Certification, Second Edition
Compunet Corporation Introduction to Unix (CA263) Round and Round By Tariq Ibn Aziz Dammam Community College.
Lesson 6-Using Utilities to Accomplish Complex Tasks.
Agenda The Bourne Shell – Part I Redirection ( >, >>,
1 UNIX Operating Systems II Part 2: Shell Scripting Instructor: Stan Isaacs.
Chapter 16 Advanced Bourne Shell Programming. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. Objectives To discuss numeric data processing.
Linux Administration Working with the BASH Shell.
Shell Control Structures CSE 2031 Fall June 2016.
1 Lecture 8 Shell Programming – Control Constructs COP 3353 Introduction to UNIX.
COMP075 OS2 Bash Scripting. Scripting? BASH provides access to OS functions, like any OS shell Also like any OS shell, BASH includes the ability to write.
CSC 352– Unix Programming, Spring 2016, Final Exam Guide
Agenda Korn Shell Functions Korn Shell Variables
Agenda Bash Shell Scripting – Part II Logic statements Loop statements
CSC 352– Unix Programming, Fall 2012
Agenda Control Flow Statements Purpose test statement
Pepper (Help from Dr. Robert Siegfried)
CSC 352– Unix Programming, Fall, 2011
Chapter 5 Bourne Shells Scripts
Introduction to Bash Programming, part 3
Presentation transcript:

Agenda Positional Parameters / Continued... Command Substitution Bourne Shell / Bash Shell / Korn Shell Mathematical Expressions Bourne Shell / Bash Shell / Korn Shell Redirection / Continued… Redirecting Standard Output / Standard Error Redirecting within a script (Here Document)

Positional Parameters Positional parameters can be used in other commands to accomplish tasks when creating a shell script. The following slides provide a “set of tools” in order to more effectively use these parameters for maximum benefit when creating your scripts

Positional Parameters $0 is name of command used to call script $1 to $9 are first nine positional parameters $* represents all positional parameters as one long string "$*" will put single pair of double quotes around string same as $*, except will put double quotes around each parameter $# contains number of parameters

Positional Parameters can also access beyond $9 by using set braces, eg. ${10} will access 10th argument set, etc... ${var} can be used instead of $var - useful if followed by alpha-numerics $? is the exit status of the last command, 0 means successful script can be ended with exit n where n is the exit status or condition code of the script

Positional Parameters shift shifts parameters left, so that the second one which was $2 becomes the first which is $1, etc., allowing parameters beyond the first nine to be accessed Arguments "falling off" the left side are no longer available, so should be saved in other variables if they are required later in the script shift nn, where nn is a number, has the same effect as that number of shift commands

Command Substitution Command substitution is the process of using standard output from a command as the argument or arguments of another command. For Example (Store arguments as positional paramaters): set `date` These positional parameters can also be contained as arguments in your scripts myscript arg1 arg2 arg 3 arg4

Command Substitution Other Example Assume our current directory contains only the following files: a1, a2, and a3 cat `ls` Is the same as: cat a1 a2 a3 but saves time, and automatically adjusts to files created or removed in current directory

Command Substitution Symbols Command Subsitution for various shells: Bourne Shell `command` Bash and Korn Shells $(command)

Expressions (expr) The Bourne Shell cannot automatically perform math calculations on numbers. Therefore, the expr command to used to convert text into numbers for mathematical operations (expr is performed automatically in c, korn, and bash shells) When the mathematical operation is performed, the result is converted back to a text string

expr Example: echo “Enter first number \c” read num1 echo “Enter second number \c” read num2 result=`expr $num1 + $num2` echo “$num1 + $num2 = $result”

expr Operators used with expr command (use backslash before operator symbols): Math operators: +, -, *, /, % Comparison of strings (text): =, > Compounds & (and), | (or) Other symbols such as -lt, -le, -eq, -ne, -gt, -ge may be needed to perform numerical comparisons, with “and” as -a and “or” as -o for compounds...

expr Other common features: `expr length “$var”` (counts character size of var) `expr substr “$var” 2 7` (Displays text in var from position 2 to 7 characters (eg if var=january, expression would give anuary) `expr index “$var1” “$var2”` (Displays position number of string 1 of first match or “occurrence” of same character)

Math Processing Symbols Bourne Shell result=`expr calculation` Bash Shell result=$[calculation] Korn Shell result=$((calculation))

Redirecting the Standard Error Since standard output can be modified to meet the user’s needs in UNIX via pipes and filters, there is a need to separate error messages from getting mixed into standard output for information processing This is very useful for redirecting errors to a file for later analysis (trouble-shooting) or to prevent errors from displaying

Redirecting the Standard Error When using the > symbol to redirect output, errors still appear on the terminal 1> file.name1 or > file.name1 is used to redirect standard output to file.name1 2> file.name2 is used to redirect the standard error message to file.name2. 2>&1 is used to redirect a standard error message to standard output (terminal or file) 1>&2 is used to send standard output to the terminal (or file if redirected) as a standard error message

Redirecting the Standard Error Examples: if [ $# = 0 ] then echo “type an argument after script”1>&2 exit 1 fi (assume file x does not exist) cat x y 1> hold1 2> hold2 cat x y 1> hold1 2>&1 Trap error message to file hold2 Combine both standard output & error to hold1

The “Here Document ” In this document the symbol “<<“ is used to read from within the script itself. The plus sign “+” is used as a delimiter to end the file, as well as to instruct the shell in the first line to read until delimiter found… See example on the next screen

The “Here Document ” Script call b_day as follows: cat <<+ MurrayJune 15 JoanApril 2 RajinderSeptember 23 AnthonyMay 31 + To run script type b_day. If you check the online lab scripts, you can see how the “here document” is used Redirect into cat command (i.e. display) + sign used to indicate end of file