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.

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.
More about Shells1-1 More about Shell  Shells (sh, csh, ksh) are m Command interpreters Process the commands you enter m High-level programming languages.
CS Lecture 03 Outline Sed and awk from previous lecture Writing simple bash script Assignment 1 discussion 1CS 311 Operating SystemsLecture 03.
CS 497C – Introduction to UNIX Lecture 33: - Shell Programming Chin-Chih Chang
23-Jun-15Advanced Programming Spring 2002 bash Henning Schulzrinne Department of Computer Science Columbia University.
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.
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 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.
CSc 352 Shell Scripts Saumya Debray Dept. of Computer Science
Shell Programming 1. Understanding Unix shell programming language: A. It has features of high-level languages. B. Convenient to do the programming. C.
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.
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.
CTEC 1863 – Operating Systems Shell Scripting. CTEC F2 Overview How shell works Command line parameters –Shift command Variables –Including.
Second edition Your UNIX: The Ultimate Guide Das © 2006 The McGraw-Hill Companies, Inc. All rights reserved. UNIX – Shell Programming The activities of.
Shell Scripting Awk (part1) Awk Programming Language standard unix language that is geared for text processing and creating formatted reports but it.
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
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[*]
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.
Introduction to Linux OS (IV) AUBG ICoSCIS Team Prof. Volin Karagiozov March, 09 – 10, 2013 SWU, Blagoevgrad.
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.
Introduction to Bash Programming Ellen Zhang. Previous three classes What have we learnt so far ?
LINUX System : Lecture 6 Shell Programming
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.
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.
Shell Programming. Introducing UNIX Shells  Shell is also a programming language and provides various features like variables, branching, looping and.
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)
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.
Lecture 2: Advanced UNIX, shell scripts (ol)‏ Programming Tools And Environments.
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.
JavaScript, Fourth Edition
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.
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.
CS252: Systems Programming Ninghui Li Slides by Prof. Gustavo Rodriguez-Rivera Topic 7: Unix Tools and Shell Scripts.
Agenda Positional Parameters / Continued... Command Substitution Bourne Shell / Bash Shell / Korn Shell Mathematical Expressions Bourne Shell / Bash Shell.
1 Unix/Linux commands and shell programming-Part 2 (Dr. Mohamed El Bachir Menai)
Flow Control. The order in which commands execute in a shell script is called the flow of the script. When you change the commands that execute based.
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.
1 Writing Shell Scripts Professor Ching-Chi Hsu 1998 年 4 月.
CSCI 330 UNIX and Network Programming Unit X: Shell Scripts II.
1 © 2012 John Urrutia. All rights reserved. Chapter 09 The TC Shell.
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.
1 Lecture 8 Shell Programming – Control Constructs COP 3353 Introduction to UNIX.
Lecture 7 Introduction to Shell Scripts COP 3353 Introduction to UNIX.
CSC 352– Unix Programming, Fall 2012
LINUX System : Lecture 5 (English-Only Lecture)
Lecture 9 Shell Programming – Command substitution
Shell Programming.
Linux Shell Script Programming
CSC 352– Unix Programming, Fall, 2011
Introduction to Bash Programming, part 3
Review.
Presentation transcript:

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 –This file is a program known as a shell script –The program can also contain control structures (if-then, loops) –Shell scripts allow a sequence of commands to be executed automatically (e.g. installation of a program - see /user_client/sybase/install)

CSH Input Processing 4 CSH transforms each command before it is executed by applying the following steps: –History substitutions –Alias substitutions –Variable substitutions –Command substitutions –File name expansions

Shell Script Invocation 4 A shell script may be invoked either explicitly: –csh file [arg…] or sh file [arg …] 4 Or implicitly by giving its name on the command line. –In this case, the file must be made executable (chmod +rx scriptfile) 4 The shell script is interpreted by a subshell spawned by the user’s interactive shell

A Simple Example #this script consults an on-line telephone database grep -i $1 $HOME/phonenumbers 4 To make this script executable, chmod +rx 4 Now it can be run from the command line A comment A positional parameter An environment variable

Shell Script Basics 4 A shell script consists of a sequence of built-in and nonbuilt-in commands separated by ; or NEWLINE 4 Comments begin with a # 4 Script execution is aborted if a built-in command fails, goes to next command if a nonbuilt-in command fails. Remember - built-in commands (e.g. alias) are part of the shell, nonbuilt-in commands (e.g. ls) are separate executable programs.

Executable Text Files 4 When the shell executes a nonbuilt-in command (i.e. a file) it first determines what type of file the command is. –Executable binary files have a “magic number” (a few bytes of code) at the beginning. –If this magic number is not found, the file is an executable text file. The first few characters of the file tell which shell should process the file - #/bin/csh or #/bin/sh

Positional Parameters 4 Parameters can be passed to the script from the command line. –Inside the script, these parameters may be referenced by using the positional parameters $0, $1, $2, etc. –$0 refers to the command name (the name of the shell script)

CSH Scripts 4 The CSH scripting language is based on the C language. –The positional parameters can alternatively be referred to as $argv[1], $argv[2], etc. –$argv[*] is a list of all of the arguments given. –$#argv is the number of arguments given. –$argv[0] is undefined. Why?

Control Flow in CSH Scripts –Rather than just executing a predetermined sequence of commands, a script can be made flexible by using the control flow constructs available: foreach if switch while goto break continue

Control Flow in CSH Scripts 4 The foreach command is used to execute a list of commands for each component of a list of words: foreach var (wordlist) commandlist end –Each time through the loop the variable var is assigned a word from wordlist, and the command list is executed

The foreach Command #!/bin/csh foreach x ( * ) echo Changing mode for $x chmod +rx $x end 4 Consider changing mode only for.exe files.

The if Command 4 Logical branching is provided by the if command: if (expr ) simple-command if ( expr ) then commandlist1 [else commandlist2] endif

The if Command ##check and set parameters if ( $#argv > 2 || $#argv < 1 ) then echo usage: “$0 [ from-file ] to-file” exit(1); else if ( $#argv == 2 ) then set from = $argv[1] set to = $argv[2] else set to = $argv[1] endif endir

The switch Command –A multiway alternative is provided by the switch command: switch ( str ) case pattern1: commandlist1 breaksw case pattern2: commandlist2 breaksw … default: commandlist endsw

The switch Command #!/bin/csh ## append $1 to $2 or standard input to $1 switch ( $#argv ) case 1: cat >> $argv[1] breaksw case 2: cat >> $argv[2] < $argv[1] breaksw default: echo 'usage: append [ from ] to' endsw

The while Command 4 The while command executes commands until an expression evaluates to zero: while ( expr ) commandlist end  Example set i = $#argv while ($i) echo i-- end

Numerical Computations –Notice the ’ in front of the i-- this allows a variable to take on a numeric value (otherwise script variables are var = var[n] = expr x = argv[$j] = $j + x += i++

Other Control Flow Constructs –The goto command ( goto word ) provides an unconditional branch to a label of the form word: –The break command provides a means to exit the nearest enclosing while or foreach loop. –The continue command is similar to the break but transfers control to the next iteration rather than breaking out of the loop.

Expressions –Expressions in csh are similar to those in C: Logical operators are the same (!, ||, &&) Relational operators are the same with the addition of =~ for string match and !~ for string mismatch Binary arithmetic operators are the same Bitwise logical operators are the same Parentheses are used to affect the order of evaluation Logical constants (0 for false, 1 for true) are the same

Expressions CSH adds file queries to test the status of a file –-r file (Is readable by the user) –-x file (Is executable by the user) –-o file (Is owned by the user) –-f file (Is an ordinary file) –-w file (Is writable by the user) –-e file (Exists) –-z file (Is of zero size) –-d file (Is a directory) We can test whether a command has succeeded in a logical expression by enclosing the command in braces ({ and }): if ({ command1 } && { command2 } || { command3 } then

Variables –There are four kinds of variables: 1. Positional parameters (e.g. $1 and $argv[2]) 2. Special variables such as noglob and nonomatch 3. Environment variables such as DISPLAY and TERM 4. User-defined variables –Variables are set using the set command: set dir = $cwd set list = (a b c k = $j + 1 set y = “$list” A multivalued variable Value of k is a string Use doublequotes for a multivalued variable

Variables  To select a portion of a multivalued variable, use $var[ selector ] where selector is an integer index, a range (e.g. 2- 4) or all of the values (*).  $#var or ${#var} gives the number of strings in a multivalued variable.

Variable Modifiers –The value of a variable can be modified before it is used in an expression or command by using a variable modifier at the end of a variable. :h removes a trailing pathname component :t removes all leading pathname components :r removes a file extension (e.g..xxx) :e removes the root name, leaving the extension :gh, :gr, :gt, :ge globally modify all strings of a multivalued variable :q quotes the substituted words, preventing further substitution :x is like :q but breaks into words at whitespace

Variable Modifiers #!/bin/csh set files = * foreach file ( $files ) echo $file if ( $file:e == for ) then mv $file {$file:r}.f77 endif end

Special Substitutions  To examine whether a variable is set or not use $?var or ${?var}. The string 1 is substituted is var is set, 0 if it is not. if ( ! $?term ) then set term = ‘tset - -m dialup:vt100’ endif 4 The special variable $$ substitutes the process number of the shell executing the script.

Special Substitutions #!/bin/csh ## Reminder service using calendar and mail set tfile = /tmp/remind_$$ ## temporary file calendar > $tfile ## consult calendar file if ( ! -z $tfile ) then ## send msg if necessary cat $tfile | /usr/ucb/mail -s \ "Reminder-calendar" $USER endif rm -f $tfile

Input and Output  The command echo words is used to display zero or more words to the standard output. –To prevent a newline after the last word, use echo -n words.  To read user input, the metavariable $< is used. A line from the standard input is read and returned as the value of the metavariable $< without variable- or file- name substitution.

The Here Document 4 It is possible to include input that is normally entered interactively inside of a script. This type of input is known as a here document and has the following form: command <<word zero or more lines of input included here word

The Here Document ## script name: timestamp ## usage: timestamp file cat >> $1 <<here ******************** RECEIVED by $user on `hostname` `date` here

The findcmd Script #!/bin/csh ## this procedure finds where given command is on search path ## the pathname for the command ## is displayed as soon as it is found ## otherwise a message to the contrary is displayed ## This script simulates the UNIX command "which" set cmd = $1 foreach dir ( $path ) if ( -e $dir/$cmd ) then echo FOUND: $dir/$cmd exit(0) endif end echo $cmd not on $path

The append Script #!/bin/csh ## append $1 to $2 or standard input to $1 switch ( $#argv ) case 1: cat >> $argv[1] breaksw case 2: cat >> $argv[2] < $argv[1] breaksw default: echo 'usage: append [ from ] to' endsw

The clean Script ## csh script: clean ## helps to rm unwanted files from a directory if ($#argv != 1) then echo usage: $0 directory; exit(1) endif set dir = $1 if (! -d $dir || ! -w $dir ) then echo $dir not a writable directory echo usage: $0 directory; exit(1) endif chdir $dir set files = *

The clean Script foreach file ($files) if (! -f $file ) then continue ## treat only ordinary files endif echo " " ## gives a blank line /bin/ls -l $file while (1) ## infinite loop echo -n "***** Delete $file or not?? [y, n, m, t, ! or q]" : set c = $< ## obtain user input switch ( $c ) case y: ## yes -- remove file if (! -w $file) then echo $file write-protected else rm -f $file if (-e $file) then

The clean Script echo cannot delete $file else echo "***** $file deleted" endif break ## to handle next file case n: ## no, don't remove file echo "***** $file not deleted" break ## to handle next file case m: ## display file with more more $file; continue ## back to while loop case t: ## show tail of file tail $file; continue case \!: ## shell escape echo command:

The clean Script eval $< ## in this command the var $file can be used continue case q: ## quit from clean exit(0) default: ## help for user echo "clean commands: followed by RETURN\ y yes delete file\ n no, don't delete file, skip to next file\ m display file with more first\ t display tail of file first\ \! shell escape\ q quit, exit from clean" endsw end ## of while end ## of foreach

The ccp Script #!/bin/csh ## csh script : ccp --- conditional copy ## usage: ccp from to [ file... ] ## where: `from' the source directory ## `to' the destination directory ## [file... ] an optional list of files to be copied, ## otherwise, all files in `from' will be processed if ($#argv < 2) then echo usage: ccp from to "[ file... ]"; exit(1) else if (! -d $1 || ! -d $2) then echo usage: ccp from to "[ file... ]"; exit(1) endif

The ccp Script set dir = `pwd`; chdir $2; set to = `pwd` chdir $dir; chdir $1; ## now in from-dir if ($#argv == 2) then set files = * else set files = ( $argv[3-] ) endif foreach file ($files) if ( -d $file ) continue ## skip directories if (! -e $to/$file) then echo $to/$file is a new file cp $file $to; continue endif # if file in $from is more recent then cp find $file -newer $to/$file -exec cp $file $to \; end

The total Script #!/bin/csh ## csh script: total ## --- compute total disk space in bytes for a directory hierarchy ## a recursive script if ( $#argv == 1 && -d $1) then set x = `/bin/ls -l -d $1` else echo usage : $0 directory; exit(1) endif set count = $x[4] ## initialize byte count

The total Script foreach file ( $1/* $1/.* ) if ( -f $file ) then set x = `/bin/ls -l count = $count + $x[4] else if ( $file:t == "." || $file:t == ".." ) then continue else if ( -d $file ) then set y = `$0 $file` ## recursive count = $count + $y else echo $file not included in the total >>! /tmp/total.file endif end echo $count