A Practical Guide to Fedora and Red Hat Enterprise Linux Unit 5: Scripting in Linux Chapter 27: Programming the Bourne Again Shell By Fred R. McClurg Linux.

Slides:



Advertisements
Similar presentations
Linux Shell Script. Shell script The first line is used to specify shell program #!/bin/sh Variables variable=“text” variable=0 variable=`program arguments`
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 33: - Shell Programming Chin-Chih Chang
Linux+ Guide to Linux Certification, Second Edition
Information Networking Security and Assurance Lab National Chung Cheng University 1 if condition Condition is defined as: "Condition is nothing but comparison.
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. Shell Scripts (1) u Basically, a shell script is a text file with Unix commands in it. u Shell scripts usually begin with a #! and.
Lab 8 Shell Script Reference:
Shell Programming, or Scripting Shirley Moore CPS 5401 Fall August 29,
Agenda Control Flow Statements Purpose test statement if / elif / else Statements for loops while vs. until statements case statement break vs. continue.
Introduction to Shell Script Programming
Week 7 Working with the BASH Shell. Objectives  Redirect the input and output of a command  Identify and manipulate common shell environment variables.
Linux Operating System
Introduction to UNIX / Linux - 11
Chapter 5 Bourne Shells Scripts By C. Shing ITEC Dept Radford University.
Bash shell programming Part II – Control statements Deniz Savas and Michael Griffiths November 2005 Corporate Information and Computing Services The University.
CST8177 bash Scripting Chapters 13 and 14 in Quigley's "UNIX Shells by Example"
July 17, 2003Serguei A. Mokhov, 1 Shells and Shell Scripts COMP 444/5201 Revision 1.3 January 25, 2005.
Shell Script Programming. 2 Using UNIX Shell Scripts Unlike high-level language programs, shell scripts do not have to be converted into machine language.
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.
Shell Scripting AFNOG IX Rabat, Morocco May 2008.
#!/bin/sh echo Hello World cat Firstshellscript.sh Firstshellscript.sh.
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.
CSC 352– Unix Programming, Spring 2015 March 2015 Shell Programming (Highlights only)
1 © 2001 John Urrutia. All rights reserved. Chapter 10 using the Bourne Again Shell.
Lecture 2: Advanced UNIX, shell scripts (ol)‏ Programming Tools And Environments.
Chapter 10: BASH Shell Scripting Fun with fi. In this chapter … Control structures File descriptors Variables.
(A Very Short) Introduction to Shell Scripts CSCI N321 – System and Network Administration Copyright © 2000, 2003 by Scott Orr and the Trustees of Indiana.
©Colin Jamison 2004 Shell scripting in Linux Colin Jamison.
CIT 140: Introduction to ITSlide #1 CIT 140: Introduction to IT Shell Programming.
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 © 2000 John Urrutia. All rights reserved. Session 5 The Bourne Shell.
Shell Script2 Reference: Linux Shell Scripting Tutorial v1.05r3 A Beginner's handbook
Lab 8 Shell Script Reference: Linux Shell Scripting Tutorial v1.05r3 A Beginner's handbook
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.
Shell script – part 2 CS 302. Special shell variable $0.. $9  Positional parameters or command line arguments  For example, a script myscript take 2.
CSCI 330 UNIX and Network Programming Unit X: Shell Scripts II.
Assigning Values 1. $ set One Two Three [Enter] $echo $1 $2 $3 [Enter] 2. $set `date` [Enter] $echo $1 $2 $3 [Enter] 3. $echo $1 $2 $3 $4 $5 $6 [Enter]
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.
1 UNIX Operating Systems II Part 2: Shell Scripting Instructor: Stan Isaacs.
Shell Scripting September 27, 2004 Class Meeting 6, Part II * Notes adapted by Lenwood Heath from previous work by other members of the CS faculty at Virginia.
Linux Administration Working with the BASH Shell.
Bash Shell Scripting 10 Second Guide.
Chapter 9 Shell Programming
Agenda Bash Shell Scripting – Part II Logic statements Loop statements
CSC 352– Unix Programming, Fall 2012
Shell Programming (ch 10)
Agenda Control Flow Statements Purpose test statement
Pepper (Help from Dr. Robert Siegfried)
CHAPTER 8 (skip , ) CHAPTER 10
Copyright © – Curt Hill Bash Flow of Control Copyright © – Curt Hill.
Presented by, Mr. Satish Pise
Chapter 5 The Bourne Shell
CSC 352– Unix Programming, Fall, 2011
Shell Control Structures
Chapter 5 Bourne Shells Scripts
Introduction to Bash Programming, part 3
Bash Scripting CS 580U - Fall 2018.
Basic shell scripting CS 2204 Class meeting 7
Review The Unix Shells Graham Glass and King Ables,
Presentation transcript:

A Practical Guide to Fedora and Red Hat Enterprise Linux Unit 5: Scripting in Linux Chapter 27: Programming the Bourne Again Shell By Fred R. McClurg Linux Operating System © Copyright 2012, All Rights Reserved

Executing Scripts Must be executable. Example: chmod a+x script Pathname of shell or interpreter on first line. Example: #!/bin/bash Specify relative or full pathname Or if directory is on $PATH, specify script name. Example: $PATH=$PATH:$HOME/bin

False True if... then: Flow Diagram ifif commands

if... then: Control Structure Definition: Evaluates a logical condition and controls whether or not statements are executed Syntax: if test ; then commands fi

if... then: Display first arg Purpose: Display first command line argument if specified Example script “argDisplay”: if test "$1" != "" then echo "First arg: $1" fi

if... then: Input prompt Purpose: Guess the “mystery” word when prompted Example script “mysteryWord”: target="secret" echo -n "Guess word: " read guess if ["$guess" = "$target"] ; then echo "That's right!" fi

False True if... then... else: Flow Diagram ifif commandscommandscommandscommands elsebranch ifbranch

if... then... else: Structure Definition: Evaluates a condition. If condition is true, control follows one branch. If the condition is not true (else), control follows a different branch. Syntax: if test ; then commands else commands fi

False True False if... elif... else: Flow Diagram commandscommands commandscommands elseelse elifelif ifif commandscommands elif branch if branch else fallback

if... elif... else: Example Purpose: File type via command-line argument Example “whatisit”: if [ -h "$1" ] ; then echo "Link: $1" elif [ -f "$1" ] ; then echo "File: $1" elif [ -d "$1" ] ; then echo "Directory: $1" else echo "Not link, file or dir" fi

Debugging Shell Scripts Description: Echo every script line before being executed Example: #!/bin/bash -x or bash -x script [args]

False True for... in: Flow Diagram for in commands true

for... in: Looping Structures Description: Structure that performs given number of iterations on a statement or statements Syntax: for idx in argList ; do commands done

for... in: Looping Example Purpose: Display all command-line arguments for arg in $* ; do echo "Arg: $arg" done

for: Looping Structures Description: Structure that iterates on the command line arguments Syntax: for arg ; do commands done

for: Looping Structure Purpose: Display all command-line arguments for arg ; do echo "Arg: $arg" done

False True while: Flow Diagram whilewhile commands while true

while: Looping Structures Description: Structure that performs iterations while a condition is true (i.e. repeat while true) Syntax: while test ; do commands done

while: Looping Example Purpose: Count to ten num=1 while [ $num -lt 11 ]; do echo $num ((num += 1)) done

True False until: Flow Diagram untiluntil commands until false

until: Looping Structures Description: Structure that performs iterations while a condition is false (i.e. repeat until true) Syntax: until test ; do commands done

until: Looping Example Purpose: Guess a number secret=4 guess="" echo "Number between 1 & 10" until [ "$guess" = "$secret" ] ; do echo -n "Your guess: " read guess done echo "You guessed it!"

break: Looping Interrupt Description: Terminates the current iteration and breaks out of the loop Example: for idx in {0..9} ; do if [ $idx = 4 ] ; then break fi echo $idx done

continue: Looping Interrupt Description: Terminates the current loop and continues the next iteration Example: for idx in {0..9} ; do if [ $idx = 4 ] ; then continue fi echo $idx done

False True case: Flow Diagram casecase patt1)patt1) *)*) commandscommands commandscommands case fallback

case: Structure Description: Multiple branching mechanism similar to if... elif... else Syntax: case test in pattern1) command1 ;; patternN) commandN ;; *) fallbackCmd ;; esac

case: Example echo -n "Where do you want to go? " read room case "$room" in "cave") echo "It is dark!" ;; "hill") echo "Tough climb!" ;; "cliff") echo "I’m falling!" ;; *) echo "Can’t go there!" ;; esac

Special Parameters Parameter Description $$ Process Id (PID) of the shell $# Command line arg count $0 Name of the script $1-$9 Command line arguments $* All the command line args

<<: The “here” document Purpose: Allows the redirection of input from within the shell Example: cat file one two EOF

Ctrl+D: End of File Signal Purpose: 1.Terminates a shell 2.Provides end of file signal to stdin Example: cat > file one two Ctrl+D