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.

Slides:



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

Lecture 5  More about Shell Script. Script Example  Task: Write a script that accepts an optional command line parameter – a directory name and prints.
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
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).
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.
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 Programming, or Scripting Shirley Moore CPS 5401 Fall August 29,
Lecture 3  Shell Variables  Shell Command History  Job / Process Control  Directory Control.
Agenda Control Flow Statements Purpose test statement if / elif / else Statements for loops while vs. until statements case statement break vs. continue.
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.
Introduction to Advanced UNIX March Kevin Keay.
Chapter 6: Shell Programming
An Introduction to Unix Shell Scripting
Chap 3 – PHP Quick Start COMP RL Professor Mattos.
Writing C-shell scripts #!/bin/csh # Author: Ken Berman # Date: # Purpose: display command and parameters echo $0 echo $argv[*]
July 17, 2003Serguei A. Mokhov, 1 Shells and Shell Scripts COMP 444/5201 Revision 1.3 January 25, 2005.
Shell Script Programming. 2 Using UNIX Shell Scripts Unlike high-level language programs, shell scripts do not have to be converted into machine language.
1 Shell Scripting (C shell) SungHo Maeung 10/27/2000 Tutorial section Computer Science Lab.
LINUX System : Lecture 6 Shell Programming
Writing Shell Scripts ─ part 3 CSE 2031 Fall October 2015.
Linux Operations and Administration
UNIX Shell Script (1) Dr. Tran, Van Hoai Faculty of Computer Science and Engineering HCMC Uni. of Technology
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.
1 System Administration Introduction to Scripting, Perl Session 3 – Sat 10 Nov 2007 References:  chapter 1, The Unix Programming Environment, Kernighan.
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.
Chapter 10: BASH Shell Scripting Fun with fi. In this chapter … Control structures File descriptors Variables.
Summer 2015 SILICON VALLEY UNIVERSITY CONFIDENTIAL 1 Introduction to UNIX / Linux - 13 Dr. Jerry Shiao, Silicon Valley University.
LIN Unix Lecture 7 Hana Filip. LIN Text Processing Command Line Utility Programs (cont.) sed LAST WEEK wc sort tr uniq awk TODAY join paste.
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.
1 Lecture 9 Shell Programming – Command substitution Regular expressions and grep Use of exit, for loop and expr commands COP 3353 Introduction to UNIX.
CSCI 330 UNIX and Network Programming Unit IX: Shell Scripts.
Executable scripts. So far We have made scripts echo hello #for example And called it hello.sh Run it as sh hello.sh This only works from current directory?
1 Unix/Linux commands and shell programming-Part 2 (Dr. Mohamed El Bachir Menai)
UNIX Shell Script (2) Dr. Tran, Van Hoai Faculty of Computer Science and Engineering HCMC Uni. of Technology
Sed. Class Issues vSphere Issues – root only until lab 3.
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.
Extending MATLAB Write your own scripts and/or functions Scripts and functions are plain text files with extension.m (m-files) To execute commands contained.
1 Writing Shell Scripts Professor Ching-Chi Hsu 1998 年 4 月.
Shell Scripting What is Shell Programming? Putting UNIX commands in a file Seldom used where speed is important Often used to manipulate files To create.
Summer 2015 SILICON VALLEY UNIVERSITY CONFIDENTIAL 1 Introduction to UNIX / Linux - 12 Dr. Jerry Shiao, Silicon Valley University.
CS 403: Programming Languages Lecture 20 Fall 2003 Department of Computer Science University of Alabama Joel Jones.
1 Lecture 7 Introduction to Shell Scripts COP 3353 Introduction to UNIX.
1 UNIX Operating Systems II Part 2: Shell Scripting Instructor: Stan Isaacs.
Shell Scripting September 27, 2004 Class Meeting 6, Part II * Notes adapted by Lenwood Heath from previous work by other members of the CS faculty at Virginia.
Linux Administration Working with the BASH Shell.
Lab 7 Shell Script Reference: Linux Shell Scripting Tutorial v1.05r3 A Beginner's handbook
Bash Scripting CIRC Summer School 2016 Baowei Liu CIRC Summer School 2016 Baowei Liu.
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.
CS 350 Lecture 3 UNIX/Linux Shells by İlker Korkmaz and Kaya Oğuz.
CIRC Winter Boot Camp 2017 Baowei Liu
Lecture 7 Introduction to Shell Programming
Lecture 7 Introduction to Shell Scripts COP 3353 Introduction to UNIX.
Agenda Bash Shell Scripting – Part II Logic statements Loop statements
Shell Scripting March 1st, 2004 Class Meeting 7.
LINUX System : Lecture 5 (English-Only Lecture)
Lecture 9 Shell Programming – Command substitution
Shell Script Assignment 1.
CSE 303 Concepts and Tools for Software Development
Agenda Control Flow Statements Purpose test statement
Shell Programming.
CSC 352– Unix Programming, Fall 2012
Introduction to Bash Programming, part 3
Basic shell scripting CS 2204 Class meeting 7
Presentation transcript:

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 Anything you can type on the command line Anything you can type on the command line Variables (including shell vars, env vars, command args…) and Expressions Variables (including shell vars, env vars, command args…) and Expressions Control statements (if, while, for) Control statements (if, while, for)

Script execution  Two ways to run a shell script: Launch a subshell with the script name as the argument. e.g. % csh my_script.sh Launch a subshell with the script name as the argument. e.g. % csh my_script.sh Specify which shell to use within the script Specify which shell to use within the script First line of script is as #!/usr/bin/cshFirst line of script is as #!/usr/bin/csh #!/usr/bin/csh –f to not read in.cshrc #!/usr/bin/csh –f to not read in.cshrc Make the script executable using chmodMake the script executable using chmod Make sure the PATH includes the current directoryMake sure the PATH includes the current directory Run directly from the command lineRun directly from the command line

Shell Script Example file./hello.sh: #!/usr/bin/csh -f echo Hello World % chmod +x./hello.sh %./hello.sh

Expressions  C Shell expressions are used or with (if/while) statements, where variable can be  Expression are formed by variables + operators operator: assigns the value of arithmetic expressions to a variable  Example % set n=2 a=$n + 2 a* = 2 Spaces must surround operators! Spaces must surround operators!

Operators  Arithmetic operators  Assignment Operators ( =, +=, -=…)  Comparison Operators (==, !=, <=…)  Bitwise and logic operators (!, &&, ||), (>>, >, <<)  Pattern matching (=~, !~)

File test operators (operator + filename) -d file: the file is a dir? -d file: the file is a dir? -e file: the file exists? -e file: the file exists? -f file: the file is a plain file? -f file: the file is a plain file? -o file: the user owns the file -o file: the user owns the file -r/w/x file: the user has read/write/execute permission -r/w/x file: the user has read/write/execute permission -z file: the file has 0 size -z file: the file has 0 size ! + any above: reverse the meaning ! + any above: reverse the meaning

Control Statements  if… then…[else…]endif if (condition) thenstatementselsestatementsendif  While…end Syntax: while (condition) statementsend  foreach...end foreach var (list) statementsend

Parameter Passing Review   Positional Parameters $0 – the currently executing script $n – the nth parameter $# -- the number of parameters   Argument Array $argv[n] – the nth parameter (n > 0) $#argv – the size of argv

Example 1  Task: Write a script that lists all its command line arguments prepended by arguments positional index  Ex: % arg.csh a1 a2 the output after running the script is: arg1 is a1 arg2 is a2

Script: arg_v1.csh #!/usr/bin/csh –f set n = 1 #positional index while ( $n <= $#argv ) echo “Arg $n is $argv[$n]” #increment the value of n++ end=================Notes:  # for comments  a space is required  Other ways to do it?

Script: arg_v2.csh #!/usr/bin/csh –f set n = 1 #positional index while ( $#argv > 0) echo “Arg $n is $1” n++ end

Example 2   Changing the access rights of the files in a directory recursively   It is simple but is very useful

Simple Solution #!/bin/csh -f foreach file (`ls`) if ( -d $file ) then chmod –R 750 $file else chmod 750 $file endif end

A Solution Using Arguments #!/bin/csh -f if ($#argv == 0) then echo "Please give a permission" exit 1 endif foreach file (`ls`) if ( -d $file ) then chmod -R $1 $file else chmod $1 $file endif end

A Solution Using while loop #!/bin/csh -f if ($#argv == 0) then echo "Please give a permission" exit 1 endif set file_set = `ls` set n = 1 while($n <= $#file_set) set file = $file_set[$n] echo "The name of file is : $file." if ( -d $file ) then chmod -R $1 $file else chmod $1 $file n ++ end

Another Solution #!/bin/csh -f if ($#argv == 0) then echo "Please give a permission" exit 1 endif set files_set = `ls` foreach file (files_set) if ( -d $file ) then chmod -R $1 $file else chmod $1 $file endif end Doesn’t Work!!

A Corrected Solution   #!/bin/csh -f   if ($#argv == 0) then   echo "Please give a permission"   exit 1   endif   set files_set = `ls`   foreach file ($files_set[*])   if ( -d $file ) then   chmod -R $1 $file   else   chmod $1 $file   endif   end

Example 3  Task: Write a script that prints similar info. as ls –l, but in more user-friendly way.  Ex: % fileinfo.csh bob (can take multiple args) (Assume: ls –l bob => -rwsr-xr-x bill ….) the output after running the script is: bob is a regular file you own the file you have read permission you have write permission you have execute permission

Script: fileinfo.csh #!/usr/bin/csh -f set n = 1 while ($n <= $#argv) if(-d $argv[$n]) then if(-d $argv[$n]) then echo "$argv[$n] is a directory" echo "$argv[$n] is a directory" endif endif if(-f $argv[$n]) then if(-f $argv[$n]) then echo "$argv[$n] is a regular file" echo "$argv[$n] is a regular file" endif endif if(-o $argv[$n]) then if(-o $argv[$n]) then echo "You own the file" echo "You own the file" else else echo "You do not own the file" echo "You do not own the file" endif endif if(-r $argv[$n]) then if(-r $argv[$n]) then echo "You have read permission" echo "You have read permission" endif endif if(-w $argv[$n]) then if(-w $argv[$n]) then echo "You have write permission" echo "You have write permission" endif endif if(-x $argv[$n]) then if(-x $argv[$n]) then echo "You have execute permission" echo "You have execute permission" endif n++end

Example 4  Task: Write a script called average.csh that reads a list of integers on stdin and prints how many numbers were read, their sum and integer average. Note: should handle the case there are not numbers read and not produce “division by 0” error Note: should handle the case there are not numbers read and not produce “division by 0” error  Ex: % average.csh <<. > 10 > 20 > 30 >. 3 numbers have been read the sum is 60 Integer average was 20

Script : average.csh #!/usr/bin/csh -f set sum = 0 set count = 0 set avg = 0 set num = $< while(($num !~ [a-z]*) && ($num != sum += count++ set num = $< set num = $<end echo "the total numbers are $count" echo "the sum is $sum" if($count >0) avg += avg += avg /= avg /= $count echo "Integer average is $avg" echo "Integer average is $avg"else echo "Integer average is 0" echo "Integer average is 0"endif

Debugging C Shell Script  C Shell has two command line options to help to debug scripts by echoing each line of the script before actually executing it. -v (verbose): echoes each line even before performing variable substitution -v (verbose): echoes each line even before performing variable substitution -x: echoes each line after all substitution has been performed just before executing the actual commands -x: echoes each line after all substitution has been performed just before executing the actual commands  How to use it: % csh –xv script.csh % csh –xv script.csh #!/bin/csh –vx #!/bin/csh –vx  Or manually set echo points to avoid verbosity

Arguments to set   set echo Display each lines after variable substitution and before execution   set verbose Display each line of script before execution, just as you typed it

Reading Assignment   Reading Chapter 10