1 Lecture 7 Introduction to Shell Scripts COP 3353 Introduction to UNIX.

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 497C – Introduction to UNIX Lecture 32: - Shell Programming Chin-Chih Chang
CS 497C – Introduction to UNIX Lecture 22: - The Shell Chin-Chih Chang
CS Lecture 03 Outline Sed and awk from previous lecture Writing simple bash script Assignment 1 discussion 1CS 311 Operating SystemsLecture 03.
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).
CSc 352 Shell Scripts Saumya Debray Dept. of Computer Science
Guide To UNIX Using Linux Third Edition
Introduction to Unix (CA263) Introduction to Shell Script Programming By Tariq Ibn Aziz.
Shell Programming 1. Understanding Unix shell programming language: A. It has features of high-level languages. B. Convenient to do the programming. C.
Bash Shell Scripting 10 Second Guide Common environment variables PATH - Sets the search path for any executable command. Similar to the PATH variable.
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 Programming, or Scripting Shirley Moore CPS 5401 Fall August 29,
Chapter 15 Introductory Bash Programming
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.
Writing Shell Scripts ─ part 1 CSE 2031 Fall September 2015.
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 Nine Advanced Shell Scripting1 System Programming Advanced Shell Scripting.
8 Shell Programming Mauro Jaskelioff. Introduction Environment variables –How to use and assign them –Your PATH variable Introduction to shell programming.
Chapter 6: Shell Programming
An Introduction to Unix Shell Scripting
Linux Shell Programming Tutorial 3 ENGR 3950U / CSCI 3020U Operating Systems Instructor: Dr. Kamran Sartipi.
July 17, 2003Serguei A. Mokhov, 1 Shells and Shell Scripts COMP 444/5201 Revision 1.3 January 25, 2005.
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.
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 ?
Essential Shell Programming by Prof. Shylaja S S Head of the Dept. Dept. of Information Science & Engineering, P.E.S Institute of Technology, Bangalore
LINUX System : Lecture 6 Shell Programming
Writing Shell Scripts ─ part 3 CSE 2031 Fall October 2015.
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.
Shell Programming Any command or a sequence of UNIX commands stored in a text file is called a shell program. It is common to call this file a command.
CS465 - UNIX The Bourne Shell.
1 Operating Systems Lecture 2 UNIX and Shell Scripts.
UNIX/LINUX SHELLS.  “A Unix shell is a command-line interpreter or shell that provides a traditional user interface for the Unix operating system and.
Introduction to Unix – CS 21
Searching and Sorting. Why Use Data Files? There are many cases where the input to the program may come from a data file.Using data files in your programs.
Writing Scripts Hadi Otrok COEN 346.
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.
CIT 140: Introduction to ITSlide #1 CIT 140: Introduction to IT Shell Programming.
1 Lecture 9 Shell Programming – Command substitution Regular expressions and grep Use of exit, for loop and expr commands COP 3353 Introduction to UNIX.
1 © 2000 John Urrutia. All rights reserved. Session 5 The Bourne Shell.
Chapter Six Introduction to Shell Script Programming.
CSCI 330 UNIX and Network Programming Unit IX: Shell Scripts.
Lesson 3-Touring Utilities and System Features. Overview Employing fundamental utilities. Linux terminal sessions. Managing input and output. Using special.
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 © 2012 John Urrutia. All rights reserved. Chapter 09 The TC Shell.
Chapter 15 Introductory Bourne Shell Programming.
1 UNIX Operating Systems II Part 2: Shell Scripting Instructor: Stan Isaacs.
Linux Administration Working with the BASH Shell.
INTRODUCTION TO SHELL SCRIPTING By Byamukama Frank
1 Lecture 8 Shell Programming – Control Constructs COP 3353 Introduction to UNIX.
CS 350 Lecture 3 UNIX/Linux Shells by İlker Korkmaz and Kaya Oğuz.
Department of Computer Engineering
Lecture 7 Introduction to Shell Programming
Lecture 7 Introduction to Shell Scripts COP 3353 Introduction to UNIX.
Writing Shell Scripts ─ part 1
Lecture 9 Shell Programming – Command substitution
Writing Shell Scripts ─ part 3
Writing Shell Scripts ─ part 3
Linux Shell Script Programming
Introduction to Bash Programming, part 3
Essential Shell Programming
Presentation transcript:

1 Lecture 7 Introduction to Shell Scripts COP 3353 Introduction to UNIX

2 What is a shell script? An executable file containing –Unix shell commands –Programming control constructs (if, then, while, until, case, for, break, continue, while, …)‏ – basic programming capabilities (assignments, variables, arguments, expressions, …)‏ The file entries are the script The file is interpreted rather than compiled and executed –The first line of the script indicates which shell is used to interpret the script

3 Simple script (egshell.sh)‏ #!/bin/sh #this is the script in file egshell.sh cal date who | grep liu exit The “#!” is used to indicate that what follows is the shell used to interpret the script The “exit” command immediately quits the shell script (by default it will also quit at the end of the file)‏

4 Executing shell scripts sh myscript #uses Bourne shell tcsh myscript#uses t-cshell Note that the above explicitly invoke the appropriate shell with the file containing the commands as a parameter. The file does not need to be executable. You can also make the file executable and then simple run as a command –chmod 755 myscript (or chmod +x myscript)‏ –myscript

5 Shell scripts Advantages –Can quickly setup a sequence of commands to avoid a repetitive task –Can make several programs work together Disadvantages –Little support for large and complicated programming semantics –Shell scripts need to be interpreted hence are slower programs Which shell to use? –csh shell and tcsh shell are recommended for use at the command line –sh (Bourne) shell and bash shell are recommended for writing shell scripts –Examples will generally use the Bourne shell

6 Printing a line to standard output Use the echo command to print a line to stdout Form of command: echo Examples echo “Hello World” echo “hello” “world” #two values echo hello #need not always use quotes echo “please enter your name”

7 Shell Environment Variables These are variables provided as part of the shell’s operational environment They exist at startup but can be changed Examples are: USER, HOME, PATH, SHELL, HOSTNAME The “setenv” command (in tcsh) is used to set these, for example, by: setenv PATH $PATH:/home/here/bin (this sets the PATH variable so that it’s current value is appended by :/home/here/bin)‏ –Note that setenv is how tcsh sets the environment variables

8 User defined variables You can also specify variables yourself and these can also be used inside a script In tcsh, the “set” command is used to set a variable to a string value Form: set = Examples: set alpha = “any string” set beta = 3 set mypath = /home/special/public_html Once a variable has been defined, it’s value can be used by “dereferencing” it with $. ls –al $mypath Note that using setenv or set without any parameters simply displays the current settings

9 Shell variables (Bourne shell)‏ Note that for all shells, variables need not be declared explicitly, but simply used For the Bourne shell, the use is as follows (note that there should be no blanks before and after the equals sign and no need for the set command. Form: = Example alpha=“hello world” beta=45 echo $alpha $beta “third argument” Note that $alpha is the value of the variable alpha

10 Reading values into shell variables The read statement is used to read a line of standard input, split the line into fields of one or more strings, and assign those strings to shell variables. Any strings not assigned are assigned to the last variable. Form: read … Examples read num read field1 field2 rest read field1 field2 < ifile.txt

11 Shell arguments Arguments on the command line can be passed to a shell script, just as you can pass command line arguments to a program $1, $2, …, $9 are used to refer to up to nine command line arguments (similar to C’s argv[1], argv[2], …, argv[9]). Note that $0 contains the name of the script (argv[0])‏ Example: shprog.sh john 40 shprog.sh bob 45 “new york”

12 Example using shell arguments Script: #!/bin/sh #script name is greet.sh #friendly display of today’s date echo “Hello” $1 $2 “pleased to meet you” echo “The date is” date exit Usage: greet.sh john smith