Writing C-shell scripts #!/bin/csh # Author: Ken Berman # Date: 8-8-2002 # Purpose: display command and parameters echo $0 echo $argv[*]

Slides:



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

CS Lecture 03 Outline Sed and awk from previous lecture Writing simple bash script Assignment 1 discussion 1CS 311 Operating SystemsLecture 03.
Integer variables #!/bin/csh # sum of numbers from $argv[1] to $argv[2] set low = $argv[1] set high = $argv[2] set sum = 0 set current = $low while ( $current.
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.
Further Shell Scripting Michael Griffiths Corporate Information and Computing Services The University of Sheffield
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.
CSCI 330 T HE UNIX S YSTEM C Shell Programming. S TEPS TO C REATE S HELL P ROGRAMS Specify shell to execute program Script must begin with #! (pronounced.
Using Unix Shell Scripts to Manage Large Data
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 Control Structures CSE 2031 Fall August 2015.
CTEC 1863 – Operating Systems Shell Scripting. CTEC F2 Overview How shell works Command line parameters –Shift command Variables –Including.
Shell Programming, or Scripting Shirley Moore CPS 5401 Fall August 29,
Week 7 Working with the BASH Shell. Objectives  Redirect the input and output of a command  Identify and manipulate common shell environment variables.
Introduction to Advanced UNIX March Kevin Keay.
LIN 6932 Unix Lecture 6 Hana Filip. LIN 6932 HW6 - Part II solutions posted on my website see syllabus.
Chapter 6: Shell Programming
An Introduction to Unix Shell Scripting
Chap 3 – PHP Quick Start COMP RL Professor Mattos.
CIS 218 Advanced UNIX1 CIS 218 – Advanced UNIX (g)awk.
1 UNIX essentials (hands-on) the directory tree running programs the shell → command line processing → special characters → command types → shell variables.
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.
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
Linux+ Guide to Linux Certification, Third Edition
UNIX Shell Script (1) Dr. Tran, Van Hoai Faculty of Computer Science and Engineering HCMC Uni. of Technology
Shell Programming. 222 Lecture Overview  Shell variables  Shell scripts  Control flow and Boolean operators  Shell programming tips  Shell programming.
CSC 352– Unix Programming, Spring 2015 March 2015 Shell Programming (Highlights only)
1 © 2001 John Urrutia. All rights reserved. Chapter 10 using the Bourne Again Shell.
Shell Programming. Creating Shell Scripts: Some Basic Principles A script name is arbitrary. Choose names that make it easy to quickly identify file function.
Awk Dr. Tran, Van Hoai Faculty of Computer Science and Engineering HCMC Uni. of Technology
Summer 2015 SILICON VALLEY UNIVERSITY CONFIDENTIAL 1 Introduction to UNIX / Linux - 13 Dr. Jerry Shiao, Silicon Valley University.
LIN Unix Lecture 5 Unix Shell Scripts. LIN Command Coordination ; && || command1 ; command2 Interpretation: Do command 1. Then do command.
1 © 2000 John Urrutia. All rights reserved. Session 5 The Bourne Shell.
CSCI 330 UNIX and Network Programming Unit IX: Shell Scripts.
Shell Programming Features “Full” programming language “Full” programming language Conditional statements Conditional statements Arithmetic, String, File,
1 Unix/Linux commands and shell programming-Part 2 (Dr. Mohamed El Bachir Menai)
Linux+ Guide to Linux Certification, Second Edition
1 Writing Shell Scripts Professor Ching-Chi Hsu 1998 年 4 月.
ORAFACT Text Processing. ORAFACT Searching Inside Files grep - searches for patterns within files grep [options] [[-e] pattern] filename [...] -n shows.
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.
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.
By Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
1 UNIX Operating Systems II Part 2: Shell Scripting Instructor: Stan Isaacs.
Shell Control Structures CSE 2031 Fall June 2016.
Foreground and background processes
Homework / Exams HW7 due today Exam #3 next class
Shell Control Structures
Lecture 7 Introduction to Shell Programming
Lecture 7 Introduction to Shell Scripts COP 3353 Introduction to UNIX.
Using Unix Shell Scripts to Manage Large Data
CSC 352– Unix Programming, Fall 2012
LINUX System : Lecture 5 (English-Only Lecture)
Lecture 9 Shell Programming – Command substitution
Shell Script Assignment 1.
Introduction to Advanced UNIX
Shell Programming.
Shell Control Structures
CSC 352– Unix Programming, Fall, 2011
Shell Control Structures
Essential Shell Programming
Introduction to Bash Programming, part 3
Review The Unix Shells Graham Glass and King Ables,
Presentation transcript:

Writing C-shell scripts #!/bin/csh # Author: Ken Berman # Date: # Purpose: display command and parameters echo $0 echo $argv[*]

Commands and Expressions Commands: –Sequence of UNIX commands, separated by ';' or on different lines –Typically returns value via stdout Expression –Logical expression similar to C language –Returns Boolean value (or integer)

Control structures for csh if ( expression ) simple command if ( expression ) then commands endif if ( expression ) then commands else commands endif

Control structures continued switch( testcase ) case pattern1 : commands breaksw case pattern2 : commands breaksw default: commands endsw

Control structures continued while ( expression ) commands end foreach var ( wordlist ) commands End

Variables String variables set name = value Integer name = value

Examples of variables % set name = Fred % echo name name % echo $name Fred % set name #not the same as % unset name % set colors = ( red green blue) % echo $colors[1] red

Variables continued % echo $colors red green blue % echo $colors[1-2] red green % echo $colors[4] Subscript out of range

Parameters for calling a script Parameters to a script are positional parameters $argv[1], $argv[2],… same as $1, $2,… $#argv number of arguments $argv[*] word list of all arguments $0 name of command (i.e. filename of script) $argv[0] is not defined

Different ways to display all parameters #!/bin/csh echo $argv[*] while ( $#argv > 0 ) echo $argv[1] shift end

Display parameters in reverse order set i = $#argv while ( $i ) echo i-- end

Logical expressions if ($#argv == 0) echo "No arguments given" Logical expressions can be formed with == equal != not equal =~ string match !~ string nonmatch && and || or ! not Expressions have to evaluate to an integer or simple string

Hints for hw3 Create a shell vector variable containing usernames from first column of who (can use awk to do this). For each username use grep –c to count number of occurrences of username and apply sed to delete those usernames that occur less then n times. Use if-else control structure to check whether there are two arguments and whether flag –t has been used Use awk to output results in a table

Logical expressions involving files -d filenamefile is a directory -e "file exists -f "file is an ordinary file -o " user owns file -r "user has read access -w " user has write access -x"user has execute access -z"file is 0 bytes long Example: if (-e $file && -f $file ) then …

Finds a given command on the search path. The pathname found or a failure message is displayed. Simulates the command "which" #!/bin/csh set cmd = $1 foreach dir($path) if (-e $dir/$cmd) then echo FOUND: $dir/$cmd exit(0) endif end echo $cmd NOT on $path