Agenda Functions: What are functions? / Purpose of Functions

Slides:



Advertisements
Similar presentations
CS 106 Introduction to Computer Science I 02 / 26 / 2007 Instructor: Michael Eckmann.
Advertisements

Linux+ Guide to Linux Certification, Second Edition
Compunet Corporation Introduction to Unix (CA263) More on Parameters By Tariq Ibn Aziz Dammam Community College.
CS 497C – Introduction to UNIX Lecture 34: - Shell Programming Chin-Chih Chang
Guide To UNIX Using Linux Third Edition
Bash Shell Scripting 10 Second Guide Common environment variables PATH - Sets the search path for any executable command. Similar to the PATH variable.
Second edition Your UNIX: The Ultimate Guide Das © 2006 The McGraw-Hill Companies, Inc. All rights reserved. UNIX – Shell Programming The activities of.
Shell Programming, or Scripting Shirley Moore CPS 5401 Fall August 29,
Chapter Seven Advanced Shell Programming. 2 Lesson A Developing a Fully Featured Program.
Advanced Shell Programming. 2 Objectives Use techniques to ensure a script is employing the correct shell Set the default shell Configure Bash login and.
Agenda Control Flow Statements Purpose test statement if / elif / else Statements for loops while vs. until statements case statement break vs. continue.
Week 7 Working with the BASH Shell. Objectives  Redirect the input and output of a command  Identify and manipulate common shell environment variables.
C Functions Programmer-defined functions – Functions written by the programmer to define specific tasks. Functions are invoked by a function call. The.
© The McGraw-Hill Companies, 2006 Chapter 4 Implementing methods.
Programming With C.
Linux+ Guide to Linux Certification, Third Edition
Linux Operations and Administration
Shell Programming Any command or a sequence of UNIX commands stored in a text file is called a shell program. It is common to call this file a command.
Linux+ Guide to Linux Certification Chapter Eight Working with the BASH Shell.
Chapter 8: Arrays and Functions Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Computer Programming Skills
Chapter 10: BASH Shell Scripting Fun with fi. In this chapter … Control structures File descriptors Variables.
1 Lecture 9 Shell Programming – Command substitution Regular expressions and grep Use of exit, for loop and expr commands COP 3353 Introduction to UNIX.
Environment After log in into the system, a copy of the shell is given to the user Shell maintains an environment which is distinct from one user to another.
Agenda Positional Parameters / Continued... Command Substitution Bourne Shell / Bash Shell / Korn Shell Mathematical Expressions Bourne Shell / Bash Shell.
Agenda The Bourne Shell – Part II Special Characters Ambiguous File Reference Variable Names and Values User Created Variables Read-only Variables (Positional.
Controlling Program Flow with Decision Structures.
Linux+ Guide to Linux Certification, Second Edition
Introduction to Bash Shell. What is Shell? The shell is a command interpreter. It is the layer between the operating system kernel and the user.
Compunet Corporation Introduction to Unix (CA263) Round and Round By Tariq Ibn Aziz Dammam Community College.
Functions in C++ Top Down Design with Functions. Top-down Design Big picture first broken down into smaller pieces.
Lesson 8-Specifying Instructions to the Shell. Overview An overview of shell. Execution of commands in a shell. Shell command-line expansion. Customizing.
An Introduction to Programming with C++ Sixth Edition Chapter 5 The Selection Structure.
1 UNIX Operating Systems II Part 2: Shell Scripting Instructor: Stan Isaacs.
FUNCTIONS (C) KHAERONI, M.SI. OBJECTIVE After this topic, students will be able to understand basic concept of user defined function in C++ to declare.
Linux Administration Working with the BASH Shell.
Chapters 13 and 14 in Quigley's "UNIX Shells by Example"
Introduction to C Topics Compilation Using the gcc Compiler
Chapter 9: Value-Returning Functions
Functions Students should understand the concept and basic mechanics of the function call/return pattern from CS 1114/2114, but some will not. A function.
Chapter 10 Programming Fundamentals with JavaScript
User-Written Functions
Agenda Korn Shell Functions Korn Shell Variables
Agenda Bash Shell Scripting – Part II Logic statements Loop statements
Chapter 10: Void Functions
Algorithms Problem: Write pseudocode for a program that keeps asking the user to input integers until the user enters zero, and then determines and outputs.
Lecture 9 Shell Programming – Command substitution
The Selection Structure
Introduction to C Topics Compilation Using the gcc Compiler
Programmazione I a.a. 2017/2018.
Chapter 6: Functions Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Agenda Control Flow Statements Purpose test statement
Engineering Innovation Center
What is Bash Shell Scripting?
IPC144 Introduction to Programming Using C Week 2 – Lesson 1
Chapter 10 Programming Fundamentals with JavaScript
Chapter 4 void Functions
WEB PROGRAMMING JavaScript.
Functions Chapter 9 Copyright © 2008 W. W. Norton & Company.
Introduction to Visual Programming
IPC144 Introduction to Programming Using C Week 8 – Lesson 1
Copyright © – Curt Hill Bash Flow of Control Copyright © – Curt Hill.
Chapter 9: Value-Returning Functions
Linux Shell Script Programming
IPC144 Introduction to Programming Using C Week 4 – Lesson 1
The structure of programming
The structure of programming
CST8177 Scripting 2: What?.
Thinking procedurally
Introduction to C Topics Compilation Using the gcc Compiler
Parameters and Arguments
Presentation transcript:

Agenda Functions: What are functions? / Purpose of Functions Structure of a function Passing arguments to functions The getopts function

What are functions? Functions are considered to be “small shell scripts” which are run within shell scripts. Functions usually are small shell scripts that perform repetitive tasks. Functions reduce repetition within shell scripts. Once a function is created, it may be able to be used in other shell scripts that you create. Often, shell script functions are available on the Internet to copy and paste.

Structure of a function A function is simply a name (do not use utility or variable names), followed by open and closed round brackets. Instructions to be performed when function is run (or “called”) is contained within open and closed braces. Functions can be created and run from the shell prompt, or can be used in shell scripts

Structure of a function Here is an example of a function: wt() { echo “Here are people on $HOST to chat with:” echo Who –T | grep “+” } Note: to run or “call” function, simply type wt at the shell prompt. Functions must appear before main portion of shell script that runs or “calls” the function… Indent within braces to make it easier to read when contained in a shell script.

Passing arguments to functions Since functions can be considered “tiny shell scripts” within shell scripts, you can actually use arguments (also known as parameters) up to the function to be used. The same concept of “positional parameters” are used when arguments are passed up to a function. When the function ends, it does not affect the positional parameters that were set by the main shell script.

Passing arguments to functions Example: print_name () { echo “Your name is: $1” } print_name “Murray Saul” Function – appearing before main portion of shell script This is what happens: The function “print_name” is read and stored into memory. Then the main shell script runs the “print_name” function and passes up the argument “Murray Saul” and is stored as the first positional parameter. Then the function runs and uses the first positional parameter and displays message. The function then ends (child process dies) and returns to the main shell script (parent process). Main portion of shell script

The getopts function The getopts function is a useful function for error-checking your script’s options. The getopts function can be used in conjunction with the while statement to “take action” based on a valid argument letter or number. Symbols * and ? are used to generate error messages for incorrect options selected.

The getopts function A colon “:” after an option indicates that another argument must follow the option. while getopts abc:d: arg do case $arg in a) echo "option is -a“ ;; b) echo "option is -b“ ;; :) printf "$0: You must supply an argument to bash %OPTARG." >&2 ;; \?) echo "Usage: get_0 [-abc {arg} d {arg}] " >&2 exit 1 ;; esac done exit 0 Take action if option is either a or b. This example left-out actions to take if option was c or d. Colon as a “case” indicates error message if argument doesn’t match valid options