Second edition Your UNIX: The Ultimate Guide Das © 2006 The McGraw-Hill Companies, Inc. All rights reserved. UNIX – Shell Programming The activities of.

Slides:



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

Second edition Your UNIX: The Ultimate Guide Das © 2006 The McGraw-Hill Companies, Inc. All rights reserved. UNIX – The Process The Process A process is.
CHAPTER 2 THE UNIX SHELLS by U ğ ur Halıcı. layers in a unix system 1 Users Standard utility programs (shell, editors, compilers, etc.) Standard utility.
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.
CS 497C – Introduction to UNIX Lecture 33: - Shell Programming Chin-Chih Chang
Linux+ Guide to Linux Certification, Second Edition
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
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.
CTEC 1863 – Operating Systems Shell Scripting. CTEC F2 Overview How shell works Command line parameters –Shift command Variables –Including.
Shell Programming, or Scripting Shirley Moore CPS 5401 Fall August 29,
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.
1 Operating Systems Lecture 3 Shell Scripts. 2 Brief review of unix1.txt n Glob Construct (metacharacters) and other special characters F ?, *, [] F Ex.
Week 7 Working with the BASH Shell. Objectives  Redirect the input and output of a command  Identify and manipulate common shell environment variables.
Second edition Your UNIX: The Ultimate Guide Das © 2006 The McGraw-Hill Companies, Inc. All rights reserved. UNIX – The Shell The Shell The agency that.
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.
Shell Scripting Todd Kelley CST8207 – Todd Kelley1.
Shell Script Programming. 2 Using UNIX Shell Scripts Unlike high-level language programs, shell scripts do not have to be converted into machine language.
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
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+ Guide to Linux Certification Chapter Eight Working with the BASH Shell.
Shell Programming. Creating Shell Scripts: Some Basic Principles A script name is arbitrary. Choose names that make it easy to quickly identify file function.
Guide to Linux Installation and Administration, 2e1 Chapter 11 Using Advanced Administration Techniques.
Writing Scripts Hadi Otrok COEN 346.
©Colin Jamison 2004 Shell scripting in Linux Colin Jamison.
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.
Agenda The Bourne Shell – Part II Special Characters Ambiguous File Reference Variable Names and Values User Created Variables Read-only Variables (Positional.
Batch Files Weaker form of UNIX shell scripts Copyright © by Curt Hill.
Linux+ Guide to Linux Certification, Second Edition
Agenda The Bourne Shell – Part I Redirection ( >, >>,
Chapter 5 The Bourne Shell Graham Glass and King Ables, UNIX for Programmers and Users, Third Edition, Pearson Prentice Hall, Notes by Michael Weeks.
1 Week 8 Creating Simple Shell Scripts. 2 Chapter Objectives  In this chapter, you will :  Learn how to create Shell Scripts  Commenting / Making Portable.
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.
Linux Administration Working with the BASH Shell.
CS 350 Lecture 3 UNIX/Linux Shells by İlker Korkmaz and Kaya Oğuz.
Bash Shell Scripting 10 Second Guide.
Shell Features CSCI N321 – System and Network Administration
Bash Introduction (adapted from chapters 1 and 2 of bash Cookbook by Albing, Vossing, & Newham) CPTE 440 John Beckett.
Chapter 9 Shell Programming
What is Bash Shell Scripting?
Shell Programming.
Linux Shell Script Programming
Essential Shell Programming
CST8177 Scripting 2: What?.
Chapter 3 The UNIX Shells
Essential Shell Programming
Introduction to Bash Programming, part 3
Essential Shell Programming
Presentation transcript:

second edition Your UNIX: The Ultimate Guide Das © 2006 The McGraw-Hill Companies, Inc. All rights reserved. UNIX – Shell Programming The activities of the shell are not restricted to command interpretation alone. shell has a whole set of internal commands that can be strung together as a language most of the constructs are borrowed from C, but there are syntactical differences shell programming is powerful because the external UNIX commands blend easily with the shell’s internal constructs in shell scripts

second edition Your UNIX: The Ultimate Guide Das © 2006 The McGraw-Hill Companies, Inc. All rights reserved. UNIX – Shell Programming We will examine the programming features of the lowest common denominator of all shells the Bourne shell (/bin/sh) and its derivatives – the Korn shell (/bin/ksh) and Bash (/bin/bash). The C shell uses totally different programming constructs

second edition Your UNIX: The Ultimate Guide Das © 2006 The McGraw-Hill Companies, Inc. All rights reserved. UNIX – Shell Programming Shell Scripts when a group of commands have to be executed regularly, they should be stored in a file, and the file executed as a shell script or a shell program. it is not mandatory using the.sh extension for shell scripts; makes them easy to match with the wild cards. shell script needs to have execute permission when invoked by its name.

second edition Your UNIX: The Ultimate Guide Das © 2006 The McGraw-Hill Companies, Inc. All rights reserved. UNIX – Shell Programming Shell Scripts it is not compiled to a separate executable file as a C program is it runs in interpretive mode and in a separate child process the calling process (often the login shell) forks a sub-shell which reads the script file and loads each statement into memory when it is to be executed shell programs are slower than compiled programs shell scripts are not recommended for number crunching, but to automate routine tasks often scheduled to run non-interactively with cron (cron daemon)

second edition Your UNIX: The Ultimate Guide Das © 2006 The McGraw-Hill Companies, Inc. All rights reserved. UNIX – Shell Programming Shell Scripts System administrative tasks are often best handled by shell scripts the reason why UNIX system administrator must be an accomplished shell programmer.

second edition Your UNIX: The Ultimate Guide Das © 2006 The McGraw-Hill Companies, Inc. All rights reserved. UNIX – Shell Programming Shell Scripts The She-Bang Line the first line of the script (#!bin/bash) contains a string beginning with #! this is not a comment line it is called the interpreter line, hash-bang, or she-bang line when the shell executes, the login shell reads this line first to determine the pathname of the program to be used for running the script

second edition Your UNIX: The Ultimate Guide Das © 2006 The McGraw-Hill Companies, Inc. All rights reserved. UNIX – Shell Programming Shell Scripts The She-Bang Line the login shell spawns a Bourne sub-shell which actually executes each statement in sequence (in interpretive mode) failure to provide the she-bang line, the login shell will spawn a child of its own type to run the script—which may not be the shell you want you can also explicitly spawn a shell of your choice by running the program representing the shell with script name as argument: sh script.sh

second edition Your UNIX: The Ultimate Guide Das © 2006 The McGraw-Hill Companies, Inc. All rights reserved. UNIX – Shell Programming Shell Scripts The She-Bang Line when using the shell with the script name as argument, the Bourne sub-shell opens the file but ignores the interpreter line

second edition Your UNIX: The Ultimate Guide Das © 2006 The McGraw-Hill Companies, Inc. All rights reserved. UNIX – Shell Programming Shell Scripts read: Making Scripts Interactive The read statement is the shell’s interactive tool for taking input from the user, making script interactive read is used with one or more variables that are assigned by keyboard input. The statement read name No $ here will pause the script to take input from the standard input a single read statement can be used with one or more variables read pname flname

second edition Your UNIX: The Ultimate Guide Das © 2006 The McGraw-Hill Companies, Inc. All rights reserved. UNIX – Shell Programming Shell Scripts #!/bin/sh echo "Enter the pattern to be searched: \c" read pname echo "Enter the file to be used: \c" read flname echo "Searching for $pname from file $flname" grep "$pname" $flname echo "Selected lines shown above"

second edition Your UNIX: The Ultimate Guide Das © 2006 The McGraw-Hill Companies, Inc. All rights reserved. UNIX – Shell Programming Shell Scripts {apache:~} emp1.sh Enter the pattern to be searched: director Enter the file to be used: shortlist Searching for director from file shortlist 9876: bill johnson :director :production:03/12/50: :john woodcock :director :personnel :05/11/47: Selected lines shown above

second edition Your UNIX: The Ultimate Guide Das © 2006 The McGraw-Hill Companies, Inc. All rights reserved. UNIX – Shell Programming Shell Scripts Using Command Line Arguments Scripts not using read can run non-interactively and be used with redirection and pipelines such scripts take their input from command line arguments assigned to certain special “variables”—positional parameters the first argument is available in $1, the second $2, and so on in addition to the positional parameters, there are a few other special parameters used by the shell

second edition Your UNIX: The Ultimate Guide Das © 2006 The McGraw-Hill Companies, Inc. All rights reserved. UNIX – Shell Programming Shell Scripts Using Command Line Arguments $* -- Stores the complete set of positional parameters as a single string $# -- It is set to the number of arguments specified; lets you design scripts that check whether the right number of arguments have been entered $0 – Holds the script filename itself.

second edition Your UNIX: The Ultimate Guide Das © 2006 The McGraw-Hill Companies, Inc. All rights reserved. UNIX – Shell Programming Shell Scripts Using Command Line Arguments #!/bin/sh echo "Program: $0" echo "The number of arguments specified is $#" echo "The argumetns are $*" grep $1 $2 echo "\nJob Over"

second edition Your UNIX: The Ultimate Guide Das © 2006 The McGraw-Hill Companies, Inc. All rights reserved. UNIX – Shell Programming Shell Scripts Using Command Line Arguments {apache:~} emp2.sh director shortlist Program:./emp2.sh The number of arguments specified is 2 The argumetns are director shortlist 9876: bill johnson :director :production:03/12/50: :john woodcock :director :personnel :05/11/47:120000

second edition Your UNIX: The Ultimate Guide Das © 2006 The McGraw-Hill Companies, Inc. All rights reserved. UNIX – Shell Programming Shell Scripts Using Command Line Arguments $1, $2…Positional parameters representing arguments $#Number of arguments $0Name of executed command $*Complete set of positional parameters in a string quoted string is treated as a separate arg $?Exit status of the last command $$PID of current shell $!PID of last background job

second edition Your UNIX: The Ultimate Guide Das © 2006 The McGraw-Hill Companies, Inc. All rights reserved. UNIX – Shell Programming Shell Scripts exit and $? : Exit status of a Command All programs and shell scripts return a value called the exit status to the caller, often the shell The shell waits for a command to complete execution and then picks up this value from the process table exit 0 – True, everything went fine exit 1 – False; something went wrong

second edition Your UNIX: The Ultimate Guide Das © 2006 The McGraw-Hill Companies, Inc. All rights reserved. UNIX – Shell Programming Shell Scripts exit and $? : Exit status of a Command A program is designed to return a true exit status when it runs successfully and false otherwise what constitutes success or failure is determined by the designer of the porgram for example, when grep could not locate a pattern; we said that the command failed; meaning the designer of grep made the program returns false exit status on failing to locate a pattern

second edition Your UNIX: The Ultimate Guide Das © 2006 The McGraw-Hill Companies, Inc. All rights reserved. UNIX – Shell Programming Shell Scripts exit and $? : Exit status of a Command A program is designed to return a true exit status when it runs successfully and false otherwise what constitutes success or failure is determined by the designer of the porgram for example, when grep could not locate a pattern; we said that the command failed; meaning the designer of grep made the program returns false exit status on failing to locate a pattern

second edition Your UNIX: The Ultimate Guide Das © 2006 The McGraw-Hill Companies, Inc. All rights reserved. UNIX – Shell Programming Shell Scripts exit and $? : Exit status of a Command A program is designed to return a true exit status when it runs successfully and false otherwise the pattern $? stores the exit status of the last command; the value of zero (0) indicates success and non-zero when the command fails if no exit is specified, then $? is set to to zero (true)

second edition Your UNIX: The Ultimate Guide Das © 2006 The McGraw-Hill Companies, Inc. All rights reserved. UNIX – Shell Programming Shell Scripts exit and $? : Exit status of a Command /dev/null  the null device that discards all data written to it {apache:~} grep director emp.lst > /dev/null; echo $? grep: can't open emp.lst 2 {apache:~} grep director shortlist > /dev/null; echo $? 0 {apache:~} grep manager shortlist > /dev/null; echo $? 1

second edition Your UNIX: The Ultimate Guide Das © 2006 The McGraw-Hill Companies, Inc. All rights reserved. UNIX – Shell Programming Shell Scripts exit and $? : Exit status of a Command The if and while constructs implicitly check $? to control the flow of execution. it is good programming practice to place exit statements with meaningful exit values at appropriate points in script for example, if an important file doesn’t exist or can’t be read, there is not point in continuing with script execution use exit 1 at that point so the next program knows that the previous program failed

second edition Your UNIX: The Ultimate Guide Das © 2006 The McGraw-Hill Companies, Inc. All rights reserved. UNIX – Shell Programming Shell Scripts The Logical Operators && and || -- Conditional Execution cmd1 && cmd2cmd2 executed if cmd1 succeeds cmd1 || cmd2cmd2 executed if cmd1 fails When && is used to delimit two commands, cmd2 is executed only when cmd1 succeeds. {apache:~} grep director shortlist > /dev/null && echo "pattern found in file" pattern found in file

second edition Your UNIX: The Ultimate Guide Das © 2006 The McGraw-Hill Companies, Inc. All rights reserved. UNIX – Shell Programming Shell Scripts The Logical Operators && and || -- Conditional Execution cmd1 && cmd2cmd2 executed if cmd1 succeeds cmd1 || cmd2cmd2 executed if cmd1 fails The || operators does the opposite; the second command is executed only when the first fails: {apache:~} grep manager shortlist > /dev/null || echo "pattern found in file" pattern found in file

second edition Your UNIX: The Ultimate Guide Das © 2006 The McGraw-Hill Companies, Inc. All rights reserved. UNIX – Shell Programming Shell Scripts The Logical Operators && and || -- Conditional Execution To display a message before invoking exit, you need to group commands, but remember to use only curly braces this will execute the enclosed commands in the current shell: grep joker /etc/passwd || { echo "Pattern not found" ; exit 0; } Use of paranthesis would not terminte the script. If the {} is executed at the shell prompt, you would be logged out. The logical operators are recommended for making simple decisions; otherwise use the if statement.