CS 497C – Introduction to UNIX Lecture 32: - Shell Programming Chin-Chih Chang

Slides:



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

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 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.
CS 497C – Introduction to UNIX Lecture 33: - Shell Programming Chin-Chih Chang
CS 497C – Introduction to UNIX Lecture 36: - Customizing the Environment Chin-Chih Chang
CS 497C – Introduction to UNIX Lecture 34: - Shell Programming Chin-Chih Chang
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.
Guide To UNIX Using Linux Third Edition
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.
Shell Script Examples.
7/17/2009 rwjBROOKDALE COMMUNITY COLLEGE1 Unix Comp-145 C HAPTER 2.
Second edition Your UNIX: The Ultimate Guide Das © 2006 The McGraw-Hill Companies, Inc. All rights reserved. UNIX – Shell Programming The activities of.
Advanced Shell Programming. 2 Objectives Use techniques to ensure a script is employing the correct shell Set the default shell Configure Bash login and.
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.
8 Shell Programming Mauro Jaskelioff. Introduction Environment variables –How to use and assign them –Your PATH variable Introduction to shell programming.
Week 7 Working with the BASH Shell. Objectives  Redirect the input and output of a command  Identify and manipulate common shell environment variables.
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.
Introduction to Unix – CS 21 Lecture 9. Lecture Overview Shell description Shell choices History Aliases Topic review.
GNU Compiler Collection (GCC) and GNU C compiler (gcc) tools used to compile programs in Linux.
CS 6560 Operating System Design Lecture 3:Tour of GNU/Linux.
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.
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
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.
Shell Programming. Introducing UNIX Shells  Shell is also a programming language and provides various features like variables, branching, looping and.
LINUX programming 1. INDEX UNIT-III PPT SLIDES Srl. No. Module as per Session planner Lecture No. PPT Slide No. 1.Problem solving approaches in Unix,Using.
Linux+ Guide to Linux Certification Chapter Eight Working with the BASH Shell.
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.
LINUX programming UNIT-2 1. INDEX UNIT-IV PPT SLIDES Srl. No. Module as per Session planner Lecture No. PPT Slide No. 1.Working with Bourne shell L1 1-2.
©Colin Jamison 2004 Shell scripting in Linux Colin Jamison.
CIT 140: Introduction to ITSlide #1 CIT 140: Introduction to IT Shell Programming.
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.
Executable scripts. So far We have made scripts echo hello #for example And called it hello.sh Run it as sh hello.sh This only works from current directory?
Shell Programming Features “Full” programming language “Full” programming language Conditional statements Conditional statements Arithmetic, String, File,
Linux+ Guide to Linux Certification, Second Edition
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.
Agenda The Bourne Shell – Part I Redirection ( >, >>,
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.
Linux Administration Working with the BASH Shell.
INTRODUCTION TO SHELL SCRIPTING By Byamukama Frank
COMP075 OS2 Bash Scripting. Scripting? BASH provides access to OS functions, like any OS shell Also like any OS shell, BASH includes the ability to write.
Lecture 7 Introduction to Shell Programming
Lecture 7 Introduction to Shell Scripts COP 3353 Introduction to UNIX.
CSC 352– Unix Programming, Fall 2012
CAP652 Lecture - Shell Programming
The Linux Operating System
Writing Shell Scripts ─ part 3
John Carelli, Instructor Kutztown University
Linux Shell Script Programming
CSC 352– Unix Programming, Fall, 2011
Essential Shell Programming
Essential Shell Programming
Essential Shell Programming
Presentation transcript:

CS 497C – Introduction to UNIX Lecture 32: - Shell Programming Chin-Chih Chang

Shell Programming The shell is also a programming language that executes shell scripts in the interpretive mode – one line at a time. Shell scripts run slower than compiled languages like C, but for many jobs speed is no hurdle. Perl is more used as a language for the system administration. An UNIX system administrator is supposed to be an accomplished shell programmer.

Shell Variables A shell variable is assigned with the = symbol without using the $, but is evaluated by prefixing it with a $: fname=profile; echo $fname The unset statement removes a variable from the shell. Variables are concatenated by placing them side by side; no operators are needed: x=foo; y=.doc; z=$x$y; echo $z

Shell Variables Shell variables can be evaluated by using curly braces around the variable name. echo ${fname} These braces also enable concatenation of a variable and a string. echo ${fname}x or echo $fname”x” These concatenation features are useful when changing a file’s extension.

Shell Variables Using curly braces, the multiple command sequence can be written in this way: boldon=`tput smso` ; boldoff=`tput rmso` echo ${boldon}Come to the Web$boldoff A shell script is an ordinary file containing a set of commands, which is executed in an interpretive manner in a sub-shell. It is also known as a shell program or shell procedure.

Shell Scripts You can execute a shell script in two ways: –Make it executable with chmod before running it (chmod +x). –It can also be run with the sh command. Use # to comment lines in the shell script. Shell scripts are always run in a separate shell – a sub-shell. Consider the example on Page 537:

Shell Scripts # Sample shell script # Use # to comment lines echo "The date today is `date`" echo Your shell is $SHELL echo Your home directory is $HOME echo The processes running are shown below: ps You can specify the shell a script must use by placing the statement #/bin/sh in the first line of the script. ksh and bash are for korn and bash shells.

read: Making Scripts Interactive The read statement accepts input to a script from the keyboard. When you use a statement like read name the script pauses at that point to take input from the keyboard. See the example on Page 539. A script containing a read statement can be redirected to take input from a file. Consider a file list :

read: Making Scripts Interactive $ cat list director emp2.lst $ emp1.sh < list You can run a script noniteractively by specifying arguments in the command line. These arguments are accepted into the positional parameters $1, $2, and so on.

Positional Parameters The shell parameter $# stores the number of arguments and $* contains all the arguments. $0 contains the name of the script that is executed. Consider the script emp2.sh : #!/bin/sh echo "Program: $0" # $0 contains the program name echo "The number of arguments specified is $#“ # The number of arguments is stored in $* echo "The arguments are $*" grep "$1" $2 echo "\nJob Over“ $ emp2.sh “robert dylan” emp1.lst

Exit Status of a Command Once grep couldn’t locate a pattern, the command is said to fail. Every command or script script returns an exit status (or return value) on termination. This value is stored in the parameter $?. 0 signifies a true value. Any non-zero value points to failure. $ grep director emp.lst > /dev/null; echo $?

The Logical Operators && and || - Conditional Execution The && and || operators act as simple one- way conditionals. When && delimits two commands, the second command is executed only when the first command succeeds. $ grep ‘director’ emp1.lst && echo “pattern found in file” In the case of ||, the second command is executed only when the first command fails. $ grep ‘manager’ emp2.lst || echo “pattern found in file”

exit: Script Termination The exit statement terminates a script. It can be used with a number signifying the success or failure. #!/bin/sh echo "Program: $0" # $0 contains the program name echo "The number of arguments specified is $#“ echo "The arguments are $*" # exit also takes an argument grep "$1" $2 >patlist 2>/dev/null || exit 2 echo "Pattern found -- Contents shown below" cat patlist $ emp2a.sh manager emp3.lst