Essential Shell Programming

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.
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.
CS 497C – Introduction to UNIX Lecture 33: - Shell Programming Chin-Chih Chang
CS 497C – Introduction to UNIX Lecture 34: - Shell Programming Chin-Chih Chang
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.
Shell Script Examples.
Introduction to Shell Script Programming
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 5 Bourne Shells Scripts By C. Shing ITEC Dept Radford University.
Unix Talk #2 (sed). 2 You have learned…  Regular expressions, grep, & egrep  grep & egrep are tools used to search for text in a file  AWK -- powerful.
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.
Chapter 4 UNIX Common Shells Commands By C. Shing ITEC Dept Radford University.
Shell Script Programming. 2 Using UNIX Shell Scripts Unlike high-level language programs, shell scripts do not have to be converted into machine language.
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
Shell Programming. Introducing UNIX Shells  Shell is also a programming language and provides various features like variables, branching, looping and.
Shell Programming. Creating Shell Scripts: Some Basic Principles A script name is arbitrary. Choose names that make it easy to quickly identify file function.
©Colin Jamison 2004 Shell scripting in Linux Colin Jamison.
Xuan Guo Chapter 5 The Bourne Shell Graham Glass and King Ables, UNIX for Programmers and Users, Third Edition, Pearson Prentice Hall, Notes by Michael.
1 Lecture 9 Shell Programming – Command substitution Regular expressions and grep Use of exit, for loop and expr commands COP 3353 Introduction to UNIX.
Chapter Six Introduction to Shell Script Programming.
CSCI 330 UNIX and Network Programming Unit IX: Shell Scripts.
CISC3130 Spring 2013 Fordham Univ. 1 Bash Scripting: control structures.
Awk- An Advanced Filter by Prof. Shylaja S S Head of the Dept. Dept. of Information Science & Engineering, P.E.S Institute of Technology, Bangalore
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)
CSCI 330 UNIX and Network Programming Unit X: Shell Scripts II.
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.
Chapter 5 The Bourne Shell Graham Glass and King Ables, UNIX for Programmers and Users, Third Edition, Pearson Prentice Hall, Notes by Michael Weeks.
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.
Awk- An Advanced Filter by Prof. Shylaja S S Head of the Dept. Dept. of Information Science & Engineering, P.E.S Institute of Technology, Bangalore
1 UNIX Operating Systems II Part 2: Shell Scripting Instructor: Stan Isaacs.
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.
Bash Shell Scripting 10 Second Guide.
Shell Control Structures
Lecture 7 Introduction to Shell Programming
Lecture 7 Introduction to Shell Scripts COP 3353 Introduction to UNIX.
Lecture 9 Shell Programming – Command substitution
Shell Script Assignment 1.
Writing Shell Scripts ─ part 3
Writing Shell Scripts ─ part 3
Essential Shell Programming
CSC 352– Unix Programming, Spring 2016
UNIX Reference Sheets CSE 2031 Fall 2010.
awk- An Advanced Filter
Linux Shell Script Programming
Shell Control Structures
Chapter 5 The Bourne Shell
Shell Control Structures
Essential Shell Programming
Chapter 5 Bourne Shells Scripts
Essential Shell Programming
awk- An Advanced Filter
Introduction to Bash Programming, part 3
Review.
Essential Shell Programming
Presentation transcript:

Essential Shell Programming by Prof. Shylaja S S Head of the Dept. Dept. of Information Science & Engineering, P.E.S Institute of Technology, Bangalore-560085 shylaja.sharath@pes.edu

Session Objective We will be learning the following constructs case expr Calling script with different names Loops

The case Conditional Conditional statement offered by the shell The statement matches an expression for more than one alternative, and uses a compact construct to permit multiway branching. also handles string tests, but in a more efficient manner than if.

The case Conditional Syntax: case expression in Pattern1) commands1 ;; … esac

The case Conditional Example: #! /bin/sh # echo “ Menu\n 1. List of files\n 2. Processes of user\n 3. Today’s Date 4. Users of system\n 5.Quit\n Enter your option: \c”

The case Conditional read choice case “$choice” in 1) ls –l;; 2) ps –f ;; 3) date ;; 4) who ;; 5) exit ;; *) echo “Invalid option” esac

The case Conditional Output $ menu.sh Menu 1. List of files 2. Processes of user 3. Today’s Date 4. Users of system 5. Quit

The case Conditional Enter your option: 3 Mon Oct 8 08:02:45 IST 2007

The case Conditional Matching Multiple Patterns: case can also specify the same action for more than one pattern . For instance to test a user response for both y and Y (or n and N). Example: Echo “Do you wish to continue? [y/n]: \c” Read ans Case “$ans” in

The case Conditional Y | y );; N | n) exit ;; esac

The case Conditional Wild-Cards: case uses them: case has a superb string matching feature that uses wild-cards. It uses the filename matching metacharacters *, ? and character class (to match only strings and not files in the current directory).

The case Conditional Example: Case “$ans” in [Yy] [eE]* );; Matches YES, yes, Yes, yEs, etc [Nn] [oO]) exit ;;Matches no, NO, No, nO *) echo “Invalid Response” esac

expr: Computation and String Handling The Bourne shell uses expr command to perform computations and string manipulation. expr can perform the four basic arithmetic operations (+, -, *, /), as well as modulus (%) functions.

expr contd.. Computation: Example1 :$ x=3 y=5 $ expr $x+$y 8  Output Example 2:$ expr 3 \* 5 15  Output Note: \ is used to prevent the shell from interpreting * as metacharacter

expr contd.. expr is also used with command substitution to assign a variable. Example: $x=5 $x=`expr $x+1` $echo $x 6 Output

expr contd.. String Handling For manipulating strings, expr uses two expressions separated by a colon (:). expr can perform the following three functions: Determine the length of the string. Extract the substring. Locate the position of a character in a string.

expr contd.. Length of the string: The regular expression .* is used to print the number of characters matching the pattern . Example: $expr “abcdefg” : ‘.*’ (o/p  7) Extracting a substring: expr can extract a string enclosed by the escape characters \ (and \). Example:$ st=2007 $ expr “$st” :’..\(..\)’ (o/p  07)

expr contd.. Locating position of a character: expr can return the location of the first occurrence of a character inside a string. Example: $ stg = abcdefgh ; expr “$stg” : ‘[^d]*d’ (o/p 4)

$0: Calling a Script by Different Names There are a number of UNIX commands that can be used to call a file by different names and doing different things depending on the name by which it is called. Similarly $0 can also be used to call a script by different names.

Contd.. Example: #! /bin/sh lastfile=`ls –t *.c | head –n 1` command=$0 exe=`expr $lastfile: ‘\(.*\).c’`

Contd.. case $command in *runc) $exe ;; *vic) vi $lastfile ;; *comc) cc –o $exe $lastfile && Echo “$lastfile compiled successfully” ;; esac

Contd.. After this create the following three links: ln comc.sh comc ln comc.sh runc ln comc.sh vic Output: $ comc hello.c compiled successfully.

While: Looping To carry out a set of instruction repeatedly shell offers three features namely while,for and until. Syntax: while condition is true do Commands done

Contd.. Example: #! /bin/usr ans=y while [“$ans”=”y”] do echo “Enter the code and description : \c” > /dev/tty read code description

Contd.. echo “$code $description” >>newlist echo “Enter any more [Y/N]” read any case $any in Y* | y* ) answer =y;; N* | n*) answer = n;; *) answer=y;; esac done

Contd.. Output: Enter the code and description : 03 analgestics Enter any more [Y/N] :y Enter the code and description : 04 antibiotics Enter any more [Y/N] : n

Contd.. Output: $ cat newlist 03 | analgestics 04 | antibiotics newlist opened only once with done> newlist All command output inside loop will go to newlist. To avoid this use /dev/tty

While: Looping Input: Enter the code and description : 03 analgestics Enter any more [Y/N] :y Enter the code and description : 04 antibiotics Enter any more [Y/N] : [Enter] Enter the code and description : 05 OTC drugs Enter any more [Y/N] : n

While: Looping Output: $ cat newlist 03 | analgestics 04 | antibiotics 05 | OTC drugs

for: Looping with a List for is also a repetitive structure. Syntax: for variable in list do Commands done Note: list here comprises a series of character strings. Each string is assigned to variable specified.

Contd.. Example: $ for file in ch1 ch2; do > cp $file ${file}.bak > echo $file copied to $file.bak done Output: ch1 copied to ch1.bak ch2 copied to ch2.bak

Contd.. Sources of list: 1. List from variables: Series of variables are evaluated by the shell before executing the loop Example: $ for var in $PATH $HOME; >do echo “$var” ; done Output: /bin:/usr/bin;/home/local/bin; /home/user1

Contd.. 2. List from command substitution: Command substitution is used for creating a list. This is used when list is large. Example: $ for var in `cat clist` ;do > echo ‘$var’ ;done 3. List from wildcards: Here the shell interprets the wildcards as filenames.

Contd.. Example: for file in *.htm *.html ; do sed ‘s/strong/STRONG/g s/img src/IMG SRC/g’ $file > $$ mv $$ $file done 4. List from positional parameters:

Contd.. Example: emp.sh #! /bin/sh for pattern in “$@”; do grep “$pattern” emp.lst || echo “Pattern $pattern not found” Done Output: $ emp.sh 9876 “Rohit” 9876 Jai Sharma Director Productions 2356 Rohit Director Sales

Conclusion In this session we have learnt Decision making structures Branching using case Repetitive structures for and while. Expression Evaluation Using scripts for achieving different tasks depending on by what name it is invoked