Lecture 5  More about Shell Script. Script Example  Task: Write a script that accepts an optional command line parameter – a directory name and prints.

Slides:



Advertisements
Similar presentations
ICE1341 Programming Languages Spring 2005 Lecture #13 Lecture #13 In-Young Ko iko.AT. icu.ac.kr iko.AT. icu.ac.kr Information and Communications University.
Advertisements

CIS 240 Introduction to UNIX Instructor: Sue Sampson.
Introduction to Unix – CS 21 Lecture 11. Lecture Overview Shell Programming Variable Discussion Command line parameters Arithmetic Discussion Control.
CS Lecture 03 Outline Sed and awk from previous lecture Writing simple bash script Assignment 1 discussion 1CS 311 Operating SystemsLecture 03.
Scripting Languages and C-Shell. What is a scripting language ? Script is a sequence of commands written as plain text and run by an interpreter (shell).
Tutorial 4 Decision Making with Control Structures and Statements Section A - Decision Making JavaScript Tutorial 4 -Decision Making with Control.
Guide To UNIX Using Linux Third Edition
Introduction to Unix (CA263) Introduction to Shell Script Programming By Tariq Ibn Aziz.
Shell Scripts 4 A shell usually interprets a single line of input, but we can also create a file containing a number of lines of commands to be interpreted.
Unix Shell Scripts. What are scripts ? Text files in certain format that are run by another program Examples: –Perl –Javascript –Shell scripts (we learn.
Bash Shell Scripting 10 Second Guide Common environment variables PATH - Sets the search path for any executable command. Similar to the PATH variable.
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.
1 Operating Systems Lecture 3 Shell Scripts. 2 Brief review of unix1.txt n Glob Construct (metacharacters) and other special characters F ?, *, [] F Ex.
Chapter 6: Shell Programming
Lecture Set 5 Control Structures Part D - Repetition with Loops.
Chapter 4: Decision Making with Control Structures and Statements JavaScript - Introductory.
Programming Fundamentals. Today’s lecture Decisions If else …… Switch Conditional Operators Logical Operators.
Writing C-shell scripts #!/bin/csh # Author: Ken Berman # Date: # Purpose: display command and parameters echo $0 echo $argv[*]
Shell Script Programming. 2 Using UNIX Shell Scripts Unlike high-level language programs, shell scripts do not have to be converted into machine language.
20-753: Fundamentals of Web Programming 1 Lecture 12: Javascript I Fundamentals of Web Programming Lecture 12: Introduction to Javascript.
1 Shell Scripting (C shell) SungHo Maeung 10/27/2000 Tutorial section Computer Science Lab.
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 Operations and Administration
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)
Copyright © 2010 Certification Partners, LLC -- All Rights Reserved Perl Specialist.
Control Structures By Shyam Gurram. Control Structure In this chapter we have two different types of structures. Conditional Structure Iterative Control.
sequence of execution of high-level statements
Controlling Execution Dong Shao, Nanjing Unviersity.
Chapter 3 Functions, Events, and Control Structures JavaScript, Third Edition.
Introduction to Unix – CS 21
LIN Unix Lecture 5 Unix Shell Scripts. LIN Command Coordination ; && || command1 ; command2 Interpretation: Do command 1. Then do command.
Perl Tutorial. Why PERL ??? Practical extraction and report language Similar to shell script but lot easier and more powerful Easy availablity All details.
Lecture 2 Conditional Statement. chcslonline.org Conditional Statements in PHP Conditional Statements are used for decision making. Different actions.
Copyright © 2003 ProsoftTraining. All rights reserved. Perl Fundamentals.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
1 ENERGY 211 / CME 211 Lecture 6 October 3, 2008.
CPS120 Introduction to Computer Science Iteration (Looping)
1 Unix/Linux commands and shell programming-Part 2 (Dr. Mohamed El Bachir Menai)
Just a Little PHP Programming PHP on the Server. Common Programming Language Features Comments Data Types Variable Declarations Expressions Flow of Control.
 Control Flow statements ◦ Selection statements ◦ Iteration statements ◦ Jump statements.
Lecture 7: Menus and getting input. switch Multiple-selection Statement switch Useful when a variable or expression is tested for all the values it can.
1 Writing Shell Scripts Professor Ching-Chi Hsu 1998 年 4 月.
Perl Chapter 3 Conditional statements. Control Expressions Control expressions – interpreted as T/F (evaluated as strings or numbers) – simple, relational,
Various 2. readonly readonly x=4 x=44 #this will give an error (like what in java?)
Batch Files Flow of Control to Strengthen Copyright © by Curt Hill.
Unit – 3 Control structures. Condition Statements 1.If.…..else :- Has someone ever told you, "if you work hard, then you will succeed"? And what happens.
1 Lecture 7 Introduction to Shell Scripts COP 3353 Introduction to UNIX.
Selection (if-then-else) Programming Has 3 Types of Control: Sequential (normal): Control of Execution Proceeds One after the Other Selection (if-then-else):
Information and Computer Sciences University of Hawaii, Manoa
Homework / Exams HW7 due today Exam #3 next class
CMIT100 Chapter 14 - Programming.
C-Shell with Functions
CSC 352– Unix Programming, Fall 2012
Loops in Java.
LINUX System : Lecture 5 (English-Only Lecture)
Selections, Scripting and Command Language
Scripts & Functions Scripts and functions are contained in .m-files
CSE 303 Concepts and Tools for Software Development
JavaScript: Control Statements.
ITM 352 Flow-Control: Loops
Agenda Control Flow Statements Purpose test statement
CSC215 Lecture Flow Control.
CSC215 Lecture Control Flow.
CSC 352– Unix Programming, Fall, 2011
Loops CGS3416 Spring 2019 Lecture 7.
CSC215 Lecture Control Flow.
SEEM 4540 Tutorial 4 Basic PHP based on w3Schools
Presentation transcript:

Lecture 5  More about Shell Script

Script Example  Task: Write a script that accepts an optional command line parameter – a directory name and prints the file system structure under this directory in the form of a tree. Without parameters, it should start in the current directory. Call this script dirtree.csh

Example Result of dirtree.csh % dirtree % dirtree||------bin | | | | garbage | | | | | |------file1 | | | |------examples | | | | | |------arg | | |------average.csh | | |------fileinfo.csh | | | |------hw6.sh | |------lab3

Script: dirtree.csh #!/bin/csh -f if($#argv == 0) then set thisdir="." set thisdir="."else set thisdir=$argv[1] set thisdir=$argv[1]endif if($?TREEPREFIX) then set prefix="$TREEPREFIX" set prefix="$TREEPREFIX"else set prefix="" set prefix=""endif echo "$prefix|" set filelist=`ls -A $thisdir` foreach file ($filelist) echo "${prefix}|------${file}" echo "${prefix}|------${file}" if(-d "$thisdir/$file") then if(-d "$thisdir/$file") then if($file == $filelist[$#filelist]) then setenv TREEPREFIX setenv TREEPREFIX "${prefix} " else else setenv TREEPREFIX setenv TREEPREFIX "${prefix}| " endif endif $0 "$thisdir/$file" $0 "$thisdir/$file" endif endifend echo "$prefix"

More on Conditionals  Recall the if command if ( expression ) then commandendif  expression must be an actual boolean expression -d directory -d directory $x > $y $x > $y

More on Conditionals  Problem: we want an expression like: “execute a command cmd and then do something based on the exit status of the command”?  Solution: store status of cmd in variable set exitStatus = cmd if ($exitStatus == 0) then … else if ($exitStatus == 1) then ……endif

The Status Variable  We don’t actually have to assign the exit status to a variable. The shell does this for us.  $status is the exit status of the last command executed cmd if ($status == 0) then … else if ($status == 1) then ……endif

A Shortcut  If we only care about whether cmd exited successfully or not, we don’t even need to use the $status variable if { ( cmd ) } then...else…endif

Evaluating Commands  Surround the command with { ( ) } if { ( cmd ) }  Evaluates to true iff exit status of cmd is 0

Script Exit Status  How do we return an exit status for our scripts?  Use the exit command exit 0, exit 1, etc. exit 0, exit 1, etc.  Convention is the exit status of 0 means normal exit and any other exit status means abnormal exit

Conditional Structures  We have already seen if. There is also switch. switch ( value ) case constant1: commands … breaksw case constant2: case constant2: breaksw breaksw ……………………. …………………….endsw

Notes on Switch  If a variable is used as the value, surround the variable with double quotes switch ( “$status” )  Fall through is allowed case 1: case 2: echo “1 or 2” break  Default case is allowed

More on Looping Structures  Loops can be nested  break stops execution of the current innermost loop  continue begins the next iteration of the current innermost loop  Another loop! repeat repeat

Repeat Command  repeat number command repeat 3 echo hello repeat 3 echo hello  repeat can be used to break out of more than one loop while ( $x > y ) … while ( $a > $b ) … if ( $c == 1 ) then repeat 2 break endifendend

Interrupt Handling  When a script is executing and an interrupt (Ctrl-c) occurs, the script exits  Use the onintr command to do something (like clean up temporary files) before exiting onintr label onintr label jump to label and execute commands therejump to label and execute commands there onintr – onintr – ignore interruptsignore interrupts

Onintr Example onintr cleanup script body goes here cleanup: onintr – clean up code goes here

Pros and Cons of Shell Scripting  Quick programming solutions for simple problems Shell is an interpreter (no compilation compared with C) Shell is an interpreter (no compilation compared with C)  Programming difficult things in shell is an abuse and ends up clumsy Compared with C, shell doesn’t allow subroutines and functions Compared with C, shell doesn’t allow subroutines and functions No complete data types No complete data types

Seeking the Right Solutions  With increasing complexity of problems, solutions are: shell script  Perl  C  Perl stands for Practical Extractions and Report Language Retain the immediateness of shell script while having the flexibility of C Retain the immediateness of shell script while having the flexibility of C Extremely good for text file handling Extremely good for text file handling  C Full fledged programming language Full fledged programming language

Recommended Reading  Chapter 10  Review  Something Fun!!  Something Fun!! 