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.

Slides:



Advertisements
Similar presentations
Introduction to Unix – CS 21 Lecture 11. Lecture Overview Shell Programming Variable Discussion Command line parameters Arithmetic Discussion Control.
Advertisements

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.
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.
Linux+ Guide to Linux Certification, Second Edition
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.
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.
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.
Introduction to Linux and Shell Scripting Jacob Chan.
Shell Programming, or Scripting Shirley Moore CPS 5401 Fall August 29,
UNIX command line. In this module you will learn: What is the computer shell What is the command line interface (or Terminal) What is the filesystem tree.
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.
1 Operating Systems Lecture 3 Shell Scripts. 2 Brief review of unix1.txt n Glob Construct (metacharacters) and other special characters F ?, *, [] F Ex.
8 Shell Programming Mauro Jaskelioff. Introduction Environment variables –How to use and assign them –Your PATH variable Introduction to shell programming.
Chapter Four UNIX File Processing. 2 Lesson A Extracting Information from Files.
Chapter 5 Bourne Shells Scripts By C. Shing ITEC Dept Radford University.
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.
System Administration Introduction to Unix Session 2 – Fri 02 Nov 2007 Reference:  chapter 1, The Unix Programming Environment, Kernighan & Pike, ISBN.
Shell Script Programming. 2 Using UNIX Shell Scripts Unlike high-level language programs, shell scripts do not have to be converted into machine language.
Additional UNIX Commands. 222 Lecture Overview  Multiple commands and job control  More useful UNIX utilities.
Introduction to Linux OS (IV) AUBG ICoSCIS Team Prof. Volin Karagiozov March, 09 – 10, 2013 SWU, Blagoevgrad.
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+ Guide to Linux Certification, Third Edition
Writing Shell Scripts ─ part 3 CSE 2031 Fall October 2015.
Linux Operations and Administration
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.
Scripting Languages Course 2 Diana Trandab ă ț Master in Computational Linguistics - 1 st year
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 Operating Systems Lecture 2 UNIX and Shell Scripts.
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.
©Colin Jamison 2004 Shell scripting in Linux Colin Jamison.
1 © 2000 John Urrutia. All rights reserved. Session 5 The Bourne Shell.
Lesson 3-Touring Utilities and System Features. Overview Employing fundamental utilities. Linux terminal sessions. Managing input and output. Using special.
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)
Agenda The Bourne Shell – Part II Special Characters Ambiguous File Reference Variable Names and Values User Created Variables Read-only Variables (Positional.
Introduction to Unix (CA263) Command File By Tariq Ibn Aziz.
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.
Lesson 6-Using Utilities to Accomplish Complex Tasks.
Agenda The Bourne Shell – Part I Redirection ( >, >>,
Various 2. readonly readonly x=4 x=44 #this will give an error (like what in java?)
Lesson 8-Specifying Instructions to the Shell. Overview An overview of shell. Execution of commands in a shell. Shell command-line expansion. Customizing.
1 Lecture 7 Introduction to Shell Scripts COP 3353 Introduction to UNIX.
 Prepared by: Eng. Maryam Adel Abdel-Hady
Linux Administration Working with the BASH Shell.
 Prepared by: Eng. Maryam Adel Abdel-Hady
1 Lecture 8 Shell Programming – Control Constructs COP 3353 Introduction to UNIX.
Bash Shell Scripting 10 Second Guide.
Lecture 7 Introduction to Shell Programming
Lecture 7 Introduction to Shell Scripts COP 3353 Introduction to UNIX.
Bash Introduction (adapted from chapters 1 and 2 of bash Cookbook by Albing, Vossing, & Newham) CPTE 440 John Beckett.
Agenda Bash Shell Scripting – Part II Logic statements Loop statements
The Linux Operating System
Shell Script Assignment 1.
John Carelli, Instructor Kutztown University
Chapter Four UNIX File Processing.
Chapter 5 Bourne Shells Scripts
CST8177 Scripting 2: What?.
Chapter 3 The UNIX Shells
Introduction to Bash Programming, part 3
Bash Scripting CS 580U - Fall 2018.
Essential Shell Programming
Presentation transcript:

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 file or a script file. Usually the terms command or script is used when the file contains a sequence of commands. The term shell program usually identifies a file containing a more complicated arrangements of commands, often using the shell's conditional commands and other advanced features. Three ways to execute a command file: 1. Redirection 2. Supply the script name as an argument to the sh command 3.

Executing a command file 1. Since the shell reads commands interactively, use redirection to get the shell to read the commands from a file: ie $sh < lsdir This is a bit clumsy and also does not allow you to supply command line arguments to the script which may be required in some cases. 2. The second method is to supply the script name as an argument to the sh command. This reflects the special capablity that is built into the shell. Ie $ sh lsdir One advantage is that you can supply arguments to the script ie $ sh lsdir /home/gb/exams The drawback of the two methods shown is that the script has to be explicitly run in order to execute the commands. You cannot avoid using the shell to execute a script but you can have the script executed automatically if you set the execute privileges to the script. When you create a script in text editor, the execute permission is turned off. This can be turned on to make the script text executable.

Executing scripts 3. Thus method 3 makes the script executable by changing the permission to the script file $ ls -l lsdir -rw-r--r-- etc $ chmod a+x lsdir -rwxr-xr-x - etc When you make the script executable, it must be located somewhere in the search path. Most search paths are constructed to look for programs in the current directory. However, if this is not the case the script will not execute. You can also pass arguments to the executable script.

Shells can store variables Shell can also use variables to store values: ie $ kc=goran $echo $kc goran (but no spaces are allowed) If spaces are needed use quotes $ kc="goran is here" $echo $kc goran is here Curly brackets are needed when to surround the variable when it is immediately followed by characters that are not part of the name. $ kc=goran $echo ${kc}Bez goranBez

Given an example Bourne Shell script – explain what is going on TMP=/tmp PIDLOG=${TMP}/pidlog$$ DATAFILE=${TMP}/date$$ DATEFILE=${TMP}/date$$ ERRLOG=${TMP}/ERRLOG #shell function to clean up and exit after an error errexit() {echo $1 date>> $ERRLOG rm –f $PIDLOG $DATAFILE exit } ok() {while true do read ans case $ans in [yY]*) return 0 ;; [nN]*) return 1;; *) echo please answer y or n ;; esac done } echo $$ > $PIDLOG date > $DATAFILE echo –n “Testing the errexit function” echo normal termination echo Please remove $PIDLOG and $DATAFILE

Example Shell Script This program calls two functions errexit() and ok(). Comments: #files used in the program are given here TMP=/tmp PIDLOG=${TMP}/pidlog$$ DATAFILE=${TMP}/data$$ DATEFILE=${TMP}/date$$ ERRLOG=${TMP}/ERRLOG #shell function to clean up and exit after an error, #this is the first function call. errexit() { echo $1 date>> $ERRLOG rm –f $PIDLOG $DATAFILE exit } Within the shell function the arguments are accessed using the notation $1, $2 etc.. Errexit is used to exit from the program when an error is encountered. In this event, errexit deletes temporary files, logs the error in a log file, and prints a message.

The other function call # read a y/n response, this is the second function call ok() { while true do read ans case $ans in [yY]*) return 0 ;; [nN]*) return 1;; *) echo please answer y or n ;; esac done } Function ok() reads a line from the standard input, and returns TRUE or FALSE, depending on user input y/n.

Continued echo $$ > $PIDLOG date > $DATAFILE echo –n “Testing the errexit function” echo normal termination echo Please remove $PIDLOG and $DATAFILE The body of the shell program asks if you want to test the errexit function, in which case user responds with y, conversely user responds with a n. Function run: The following are the possible results when the program runs, Two options user Y or n $ function_ssi Test errexit function [y/n] y Testing the errexit function $ function_ssi Test errexit function [y/n] n Normal Termination Please remove $PIDLOG and $DATAFILE