C-Shell with Functions

Slides:



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

Chapter 7 Introduction to Procedures. So far, all programs written in such way that all subtasks are integrated in one single large program. There is.
Chapter 7: User-Defined Functions II
Chapter 9: The TC Shell Everything you thought you knew is now wrong.
CS Lecture 03 Outline Sed and awk from previous lecture Writing simple bash script Assignment 1 discussion 1CS 311 Operating SystemsLecture 03.
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.
Guide To UNIX Using Linux Third Edition
Introduction to Unix (CA263) Introduction to Shell Script Programming By Tariq Ibn Aziz.
FunctionsFunctions Systems Programming Concepts. Functions   Simple Function Example   Function Prototype and Declaration   Math Library Functions.
Bash Shell Scripting 10 Second Guide Common environment variables PATH - Sets the search path for any executable command. Similar to the PATH variable.
CTEC 1863 – Operating Systems Shell Scripting. CTEC F2 Overview How shell works Command line parameters –Shift command Variables –Including.
Scripting CBIS BASH Scripting Step 1 – Create the bash file. Usually a good idea to end it in.sh file1.sh Step 2 – Using CHMOD make the bash file.
Chapter 15 Introductory Bash Programming
Introduction to Shell Script Programming
Chapter Nine Advanced Shell Scripting1 System Programming Advanced Shell Scripting.
Week 7 Working with the BASH Shell. Objectives  Redirect the input and output of a command  Identify and manipulate common shell environment variables.
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.
Writing C-shell scripts #!/bin/csh # Author: Ken Berman # Date: # Purpose: display command and parameters echo $0 echo $argv[*]
C++ Programming: From Problem Analysis to Program Design, Fifth Edition, Fifth Edition Chapter 7: User-Defined Functions II.
Shell Script Programming. 2 Using UNIX Shell Scripts Unlike high-level language programs, shell scripts do not have to be converted into machine language.
Lecture 4  C Shell Scripts(Chapter 10). Shell script/program  Shell script: a series of shell commands placed in an ASCII text file  Commands include.
LINUX System : Lecture 6 Shell Programming
Linux+ Guide to Linux Certification, Third Edition
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.
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.
CS465 - UNIX The Bourne Shell.
UNIX Commands. Why UNIX Commands Are Noninteractive Command may take input from the output of another command (filters). May be scheduled to run at specific.
Linux+ Guide to Linux Certification Chapter Eight Working with the BASH Shell.
CSC 352– Unix Programming, Spring 2015 March 2015 Shell Programming (Highlights only)
Copyright © 2010 Certification Partners, LLC -- All Rights Reserved Perl Specialist.
Summer 2015 SILICON VALLEY UNIVERSITY CONFIDENTIAL 1 Introduction to UNIX / Linux - 13 Dr. Jerry Shiao, Silicon Valley University.
Introduction to c++ programming - object oriented programming concepts - Structured Vs OOP. Classes and objects - class definition - Objects - class scope.
LIN Unix Lecture 5 Unix Shell Scripts. LIN Command Coordination ; && || command1 ; command2 Interpretation: Do command 1. Then do command.
©Colin Jamison 2004 Shell scripting in Linux Colin Jamison.
Copyright © 2003 ProsoftTraining. All rights reserved. Perl Fundamentals.
Agenda Positional Parameters / Continued... Command Substitution Bourne Shell / Bash Shell / Korn Shell Mathematical Expressions Bourne Shell / Bash Shell.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Today: –Review declaration, implementation, simple class structure. –Add an exception class and show.
Linux+ Guide to Linux Certification, Second Edition
LECTURE 2 Python Basics. MODULES So, we just put together our first real Python program. Let’s say we store this program in a file called fib.py. We have.
1 Lecture 7 Introduction to Shell Scripts COP 3353 Introduction to UNIX.
Linux Administration Working with the BASH Shell.
CSE 374 Programming Concepts & Tools
Lecture 7 Introduction to Shell Programming
Lecture 7 Introduction to Shell Scripts COP 3353 Introduction to UNIX.
Chapter 7: User-Defined Functions II
How to link a test to a launcher (in this case a shell launcher)
CSC 352– Unix Programming, Fall 2012
CAP652 Lecture - Shell Programming
LINUX System : Lecture 5 (English-Only Lecture)
CSE 303 Concepts and Tools for Software Development
Writing Shell Scripts ─ part 3
Programmazione I a.a. 2017/2018.
What is Bash Shell Scripting?
Topics Introduction to File Input and Output
CISC101 Reminders Assn 3 due tomorrow, 7pm.
PHP.
Copyright © – Curt Hill Bash Flow of Control Copyright © – Curt Hill.
Notes about Homework #4 Professor Hugh C. Lauer CS-1004 — Introduction to Programming for Non-Majors (Slides include materials from Python Programming:
CSE 303 Concepts and Tools for Software Development
Linux Shell Script Programming
Week 1 – Lesson 2: Creating Shell Scripts, Linux Commands
CSC 352– Unix Programming, Fall, 2011
Introduction to Bash Programming, part 3
Topics Introduction to File Input and Output
CISC101 Reminders Assignment 3 due today.
 A function is a named sequence of statement(s) that performs a computation. It contains  line of code(s) that are executed sequentially from top.
SPL – PS1 Introduction to C++.
Presentation transcript:

C-Shell with Functions Arlindo da Silva Arlindo.daSilva@nasa.gov November 2006

Overview Motivation Basic Functionality Implementation Overview Available Keywords Style Guide Passing arguments by reference Examples Summary

Motivation Modern Unix shells such as bash and ksh permit definition of user defined functions at the top of the file, e.g., Both csh and tcsh lack this feature which makes any kind of encapsulation very difficult. However, one can simulate this functionality within the csh by a combination of the alias and source commands. #!/bin/bash hello() { echo "Hello, $1!" } hello World

Basic Functionality A sourceable file Functions.csh defines the relevant aliases and must be included at the top of the user code As in bash/ksh functions are defined at the top of the file with the keywords Sub and /end: #!/bin/csh Use Functions.csh Sub hello echo "Hello, $1!" \end Call hello World

Implementation Overview For each function, all code between Sub and \end are written to a temporary script file with the function name as part of the file name Functions are invoked with the keyword Call which in turn sources this temporary file passing the specified parameters In addition to the usual $1, $2. … arguments one can also define named arguments Functions can also be Run, in which case the corresponding script is executed instead of being sourced. Useful to avoid clobbering local variables

Available Keywords Use module Sub name ( arg1, arg2,…) Sources the first occurrence of file “module.csm” on your path Module files usually contain function definitions Module files must have extension .csm Sub name ( arg1, arg2,…) Defines a new function named name The named argument arg1, arg2, etc., are optional One call always use $1, $2… When present, code fragment is automatically generated with the assignments Set arg1 = $1 Set arg2 = $2, etc. Notice that when using Call there is only one name space

Keywords, cont… Call name ( arg1, arg2, …) Call_ name arg1 arg2 … Invokes a function by sourcing the corresponding temporary script file Parenthesis and commas are optional and stripped out when present Current implementation is dumb The arguments must be simple words with no spaces All arguments are passed by value, but one can simulate references by passing the name of a csh variable Call_ name arg1 arg2 … Like Call but without parenthesis and commas Arguments can be strings

Keywords, cont… Run name arg1 arg2 … Clean Like Call_ but in this case the temporary script file is run as an external script There is no good way to return variables in this case Since the script runs on its own name space, there is not danger of clobbering shell variables Clean Cleans up temporary script files Can be used to erase the internal “function table” It is a good idea to execute this alias at the end of your main script This alias is automatically run when the shell variable Verify is set and the function returns a non-zero status.

Error trapping Verify Set this variable for automatic error handling When a function is called or run, and returns a non-zero error code, your calling script will exit with the same error code when this variable is set Alias Clear will be executed prior to an abnormal exit You can go back to the default behavior by unsetting Verify

Shell echo and verbose The verbose and echo variables and the related -v and -x command line options can be used to help trace the actions of the shell. Since the source command does not recognize lines of the form One will need to explicit set the echo and verbose shell variables to obtain the desired echoing of csh commands as they are executed #!/bin/csh -fx

Reserved variables The following variables are used internally and should be considered as reserved ARG___ ARGV___ ARGC___ FILE___ NAME___ RC___

Style Guide Because the source command works on the same name space as the calling script, care should be taken to avoid clobbering shell variables In particular, the implementation of named arguments creates variables with the same name as the arguments, for example Will create variable $name which will be set to “World” after this function invocation #!/bin/csh Use Functions.csh Sub hello ( name ) echo "Hello, $name!" \end Call hello World

Style Guide, cont… To avoid this side effect, add underscore to the end of input variables Variable $name_ will still be created but if one reserves this name convention for argument variables no conflict will arise. #!/bin/csh Use Functions.csh Sub hello ( name_ ) echo "Hello, $name_!" \end Call hello World

Style Guide, cont… All arguments are passed by value, in the sense that a function argument variable is never modified However, one can pass a variable name and have the script set this variable with the desired result As a convention we prefix the output argument expected to contain a variable name with an underscore. #!/bin/csh Use Functions.csh Sub add ( first_, second_, _result ) @ result_ = $first_ + $second_ set $_result = $result_ \end Call add(3,5,result) echo “Result is $result”

Examples Along with the main sourceable file Functions.csh you will find a some example/test code utFunctions.csm: module file with function definitions utFunctions.csh: driver The next slides reproduce some of these examples

Example 1 Simple function passing unnamed arguments; there is no automatic exiting since Verify is not set by default. % Call Hello 'World' % if ( $status ) \ echo "Function <Hello> returned $status“ Hello, World! Got unamed argument World Function <Hello> returned 7

Example 2 Regular Call; arguments must be simple words, no strings % set first = "arlindo" % set times = 7 % Call Say ( $first, maryland, 301-592-0303, dasilva ) % echo "Variable times now has value $times“ Arguments in reversed order: dasilva 301-592-0303 maryland arlindo Variable times now has value 8

Example 3 Naked Call_; arguments can be strings, global variable something reset % set something = "set in <main>" % Call_ Entry 'Arlindo Moraes da Silva' \ '212 Whitmoor Terrace' '29' % echo "Variable 'something' $something" Full name: Arlindo Moraes da Silva Address: 212 Whitmoor Terrace Age: 29 Variable 'something' set by function <Example>

Example 4 Run function as script; global variable something is not reset in this case % set something = "set in <main>" % Run Entry 'Arlindo Moraes da Silva' \ '212 Whitmoor Terrace' '29' % echo "Variable 'something' $something" Full name: Arlindo Moraes da Silva Address: 212 Whitmoor Terrace Age: 29 Variable 'something' set by function <main>

Example 5 Passing arguments by reference % Call Add ( 3, 5, result ) % echo "Result is $result“ Result is 8

Example 6 Simulating error condition with Verify set % set Verify % Call Aloha error: function <Aloha> not defined error: aborting <Aloha> with rc = 1

Summary A simple mechanism has been presented for introducing user defined functions in the C-Shell Although encapsulation is now possible, there are still serious limitations in the csh that makes it not recommended for medium to high complexity scripts Perl, python or even bash/ksh are much better choices However, the encapsulation afforded here can be the first step towards conversion of a large script to another language.

Source code on 11/28/2006 follows… Functions.csh Source code on 11/28/2006 follows…

Function.csh # Short-hand to make fortran/perl programmers happy # Syntax: Use module_name # Example: Use utFunctions # Module files are required to have extension ".csm", # but the extension is not required in the module name. # ---------------------------------------------------- alias Use \ 'set FILE___ = "\!*"\ if ( "$FILE___:e" != "csm" ) set FILE___ = $FILE___.csm\ set ARG___ = `which $FILE___`\ set RC___ = $status\ if ( $RC___ ) then\ echo "error: cannot find module $FILE___"\ else\ source $ARG___[1]\ endif\ if ( $?Verify && $RC___ ) then\ echo "error: aborting <Use> with rc = $RC___"\ exit $RC___\ set status = $RC___'

Function.csh, cont… # Sub declaration: un-quoted here documents save to tmp file # --------------------------------------------------- alias Sub \ 'set ARGV___ = (`echo "\!*"|sed -e "s/(//g" -e "s/)//g" -e "s/,/ /g"`)\ set NAME___=$ARGV___[1]\ shift ARGV___\ echo "# Auto-generated by Functions.csh" > $TMPDIR/.cshFunctions.$$.$NAME___\ chmod +x $TMPDIR/.cshFunctions.$$.$NAME___\ echo "shift" >> $TMPDIR/.cshFunctions.$$.$NAME___\ set ARGC___ = 1\ foreach ARG___ ( $ARGV___ )\ echo "set $ARG___ =" \"{\$}argv\[$ARGC___\]\" >> $TMPDIR/.cshFunctions.$$.$NAME___\ @ ARGC___++\ end\ cat <<\end>>$TMPDIR/.cshFunctions.$$.$NAME___'

Function.csh, cont… ## Function call of the form: Call function ( arg1, arg2, ... ) # --------------------------------------------------- alias Call \ 'set ARGV___ = (`echo "\!*"|sed -e "s/(//g" -e "s/)//g" -e "s/,/ /g"`)\ set NAME___=$ARGV___[1]; set FILE___ = "$TMPDIR/.cshFunctions.$$.$NAME___"\ if ( $?Trace ) echo "---------------> Entering <$NAME___>"\ if ( -e $FILE___ ) then\ source $FILE___ $ARGV___\ set RC___ = $status\ else\ echo "error: function <$NAME___> not defined"\ set RC___ = 1\ endif\ if ( $?Verify && $RC___ ) then\ echo "error: aborting <$NAME___> with rc = $RC___"\ /bin/rm -rf $TMPDIR/.cshFunctions.$$.*\ exit $RC___\ set status = $RC___'

Function.csh, cont… # Naked function call: Call_ function arg1 arg2 ... argn # ----------------------------------------------------------- alias Call_ \ 'set ARGV___ = ( \!* ); set NAME___ = "$ARGV___[1]"; set FILE___ = "$TMPDIR/.cshFunctions.$$.$NAME___"; if ( -e $FILE___ ) source $FILE___ \!*\ set RC___ = $status\ if ( $?Trace ) echo "---------------> Entering <$NAME___>"\ if ( ! -e $FILE___ ) then\ echo "error: function <$NAME___> not defined"\ set RC___ = 1\ endif\ if ( $?Verify && $RC___ ) then\ echo "error: aborting <$NAME___> with rc = $RC___"\ /bin/rm -rf $TMPDIR/.cshFunctions.$$.*\ exit $RC___\ set status = $RC___'

Function.csh, cont… # Run: Like Call_ but run function as a separate shell script # This is useful to preserve your name space. # ----------------------------------------------------------- alias Run \ 'set ARGV___ = ( \!* ); set NAME___ = "$ARGV___[1]"; set FILE___ = "$TMPDIR/.cshFunctions.$$.$NAME___"; if ( -e $FILE___ ) $FILE___ \!*\ set RC___ = $status\ if ( $?Trace ) echo "---------------> Entering <$NAME___>"\ if ( ! -e $FILE___ ) then\ echo "error: function <$NAME___> not defined"\ set RC___ = 1\ endif\ if ( $?Verify && $RC___ ) then\ echo "error: aborting <$NAME___> with rc = $RC___"\ /bin/rm -rf $TMPDIR/.cshFunctions.$$.*\ exit $RC___\ set status = $RC___' # Verify status, sort of like GEOS-5 # ---------------------------------- alias Verify_ 'if ( $status ) exit $status' # Delete temp files: Clean [all] # ------------------------------ alias Clean \ 'if ( "\!*" == "all" ) then\ /bin/rm -rf $TMPDIR/.cshFunctions.*\ else\ endif'