1 Shell Scripting (C shell) SungHo Maeung 10/27/2000 Tutorial section Computer Science Lab.

Slides:



Advertisements
Similar presentations
Shell Scripting Printing; Loops and Logic printf: formatted print printf is a more standardized method to print text to the screen. It can be used instead.
Advertisements

CIS 240 Introduction to UNIX Instructor: Sue Sampson.
Shell Programming Software Tools. Slide 2 Shells l A shell can be used in one of two ways: n A command interpreter, used interactively n A programming.
TDC368 UNIX and Network Programming Camelia Zlatea, PhD Week 9:  Introduction to UNIX Shells  C-Shell.
CS 497C – Introduction to UNIX Lecture 34: - Shell Programming Chin-Chih Chang
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.
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 Control Structures CSE 2031 Fall August 2015.
Chapter 15 Introductory Bash 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.
Introduction to Advanced UNIX March Kevin Keay.
Introduction to UNIX / Linux - 11
Chapter 6: Shell Programming
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.
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.
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
CS465 - UNIX The Bourne Shell.
Shell Programming. 222 Lecture Overview  Shell variables  Shell scripts  Control flow and Boolean operators  Shell programming tips  Shell programming.
Shell Programming. Introducing UNIX Shells  Shell is also a programming language and provides various features like variables, branching, looping and.
Shell Script Yingying Wang. Basic Commands Good resources Google is your friend
1. 2 What is a Shell Script? A Text File With Instructions Executable  Why shell script? Simply and quickly initiate a complex series of tasks or a.
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.
Shells. Variables MESSAGE="HELLO WORLD" echo $MESSAGE MESSAGE="HELLO WORLD $LOGNAME" echo $MESSAGE MESSAGE="HELLO WORLD $USER" echo $MESSAGE.
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.
Using UNIX Shell Scripts Michael Griffiths Corporate Information and Computing Services The University of Sheffield
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.
CSCI 330 UNIX and Network Programming Unit IX: Shell Scripts.
CISC3130 Spring 2013 Fordham Univ. 1 Bash Scripting: control structures.
Shell Programming Features “Full” programming language “Full” programming language Conditional statements Conditional statements Arithmetic, String, File,
Shell Control Statements and More
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
Computer Programming for Biologists Class 4 Nov 14 th, 2014 Karsten Hokamp
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 月.
Assigning Values 1. $ set One Two Three [Enter] $echo $1 $2 $3 [Enter] 2. $set `date` [Enter] $echo $1 $2 $3 [Enter] 3. $echo $1 $2 $3 $4 $5 $6 [Enter]
Compunet Corporation Introduction to Unix (CA263) Round and Round By Tariq Ibn Aziz Dammam Community College.
1 © 2012 John Urrutia. All rights reserved. Chapter 09 The TC Shell.
Summer 2015 SILICON VALLEY UNIVERSITY CONFIDENTIAL 1 Introduction to UNIX / Linux - 12 Dr. Jerry Shiao, Silicon Valley University.
1 Lecture 7 Introduction to Shell Scripts COP 3353 Introduction to UNIX.
Chapter 15 Introductory Bourne Shell Programming.
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.
ULI101 Week 10. Lesson Overview ● Shell Start-up and Configuration Files ● Shell History ● Alias Statement ● Shell Variables ● Introduction to Shell Scripting.
Bash Shell Scripting 10 Second Guide.
Homework / Exams HW7 due today Exam #3 next class
Lecture 7 Introduction to Shell Programming
Lecture 7 Introduction to Shell Scripts COP 3353 Introduction to UNIX.
Advanced C Shell Programming
LINUX System : Lecture 5 (English-Only Lecture)
Shell Programming (ch 10)
Introduction to Advanced UNIX
Shell Programming.
Shell Control Structures
Introduction to Bash Programming, part 3
Presentation transcript:

1 Shell Scripting (C shell) SungHo Maeung 10/27/2000 Tutorial section Computer Science Lab

2 Contents C shell –First Script –Getting User Input –Command Line Arguments –Conditionals and File Testing –Switch & Loops Programming with C Shell –mailingList Summary & Resources

Computer Science Lab3 C Shell #! /bin/csh set name = hello -> echo $name $array = ( element1 elemsnt2 ) $array[1], $array[2]

Computer Science Lab4 C Shell – First Script ${LOGNAME} –Print user’s name `unix command` –Enclosed in back quotes –Performed command substitution –Ex) `date` uname –n –Display the machine name ps –ef | grep “^ *$LOGNAME” –User’s processes are printed chmond –u+x greetme

Computer Science Lab5 C shell Getting User Input $< – user input (std) without newline $array = ($name) –An array is created Arithmetic ( operators integer +, -, /, *, %, > +=, -=, *=, /=, ++, -- sum = 4+6 –Space is required

Computer Science Lab6 Cshell Debugging scripts Echo (-x) and Verbose (-v) – csh –x scriptname (command ) Display each line after variable substitution and before execution – set echo, unset ( in the file )

Computer Science Lab7 Cshell command line argument $0 - The name of the script $1, $2, $3,..${10} –Parameters are referenced by number $* - All positional parameters $argv[1], $argv[2] –The first argument, second … $argv[*] or $argv – all arguments $#argv – The number of arguments (size) $argv[$#argv] – The last argument

Computer Science Lab8 Cshell Conditional constructs ==, !=,, >=, <=, ! =~, !~ –String matches ex) $ans =~ [Yy]* ||, && –Logical operator

Computer Science Lab9 Cshell if examples If( expression ) then If( expression ) then Command Commandelse endif Ex) if ( $#argv != 1) then echo “$0 requires an argument” exit 1 endif

Computer Science Lab10 Cshell if expression set name = $< If( “$name” != “” ) then grep “$name” data file endif If( $answer =~ [Yy]* ) then mail bob < message else mail jon < datafile endif set x = 1, y = 2, z = 3 If( ( “ $x ” && “ $y ” ) || ! “ $z ” ) then echo TRUE If( $#argv == 0 ) exit 1

Computer Science Lab11 Cshell if command If { (command) } then command endif If { ( who | grep $1 >& /dev/null ) } then echo $1 is logged on endif startover: set grade = $ 100 ) then echo “Illegal grade” goto startover endif

Computer Science Lab12 Cshell if file testing if( ! – e $file ) : file exists if( -d $file ) : a direcotry file if( -f $file ) : a plain file if( ! – z $file ) : zero length if( -r $file && -w $file): readable and writable if( -x $file ): executable

Computer Science Lab13 Cshell Switch switch () case : breaksw default: breaksw endsw set color = $< switch ( “ $color ” ) case bl*: echo I feel $color breaksw default: echo $color not one of them breaksw endsw

Computer Science Lab14 Cshell for foreach variable (wordlist) commands end foreach variable (wordlist) commands end –foreach person ( bob sam sue fred) mail $person < letter end –foreach person (`cat maillist`) mail $person <<EOF Hi $person EOF end

Computer Science Lab15 Cshell while while end set num = 0 while ($num < 10 ) echo num++ end repeate repeate takes two argument( number, command) –Ex) %repeate 3 echo hello hello hello hello

Computer Science Lab16 Cshell While shift shift command –Shifts the argv array by one word Ex) While( $#argv) echo $argv shift end %loop a b c a b c b c c

Computer Science Lab17 Cshell While break && continue command set answer = $< while( “ $answer ” !~ [Mm]* ) echo “ Wrong\! Try again. ” set answer = $< if( “ $answer ” =~ [Mm]*) break; end

Computer Science Lab18 Programming with C shell mailingList –data Hello XXX, – friends ( s )

Computer Science Lab19 Resources Tutorial sites