Shell Scripting – Putting it All Together. Agenda Escaping Characters Wildcards Redirecting Output Chaining and Conditional Chaining Unnamed and Named.

Slides:



Advertisements
Similar presentations
A batch file is a file that contains a number of DOS commands, each of which could be run individually from the command prompt. By putting them into a.
Advertisements

P1PMF Split1 QBASIC. P1PMF Split2QBasic Command Prompt Will launch the emulator DOS operating system? Press Alt + Enter to display the widescreen.
Utilizing the GDB debugger to analyze programs Background and application.
1 © 2001 John Urrutia. All rights reserved. Chapter 5 The Shell Overview.
FILE TRANSFER PROTOCOL Short for File Transfer Protocol, the protocol for exchanging files over the Internet. FTP works in the same way as HTTP for transferring.
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.
Linux+ Guide to Linux Certification, Second Edition
Shell Basics CS465 - Unix. Shell Basics Shells provide: –Command interpretation –Multiple commands on a single line –Expansion of wildcard filenames –Redirection.
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. 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.
Ch 111 Chapter 11 Advanced Batch Files. Ch 112 Overview This chapter focuses on batch file commands that allow you to:  write sophisticated batch files.
Programming Batch Files Aim: To introduce the concept of Batch processing and programming techniques. Lesson Outcomes  The need for Batch Processing.
Chapter Seven Advanced Shell Programming. 2 Lesson A Developing a Fully Featured Program.
Advanced Shell Programming. 2 Objectives Use techniques to ensure a script is employing the correct shell Set the default shell Configure Bash login and.
Lesson 7-Creating and Changing Directories. Overview Using directories to create order. Managing files in directories. Using pathnames to manage files.
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.
Ch 21 Command Syntax Using the DIR Command with Parameters and Wildcards.
Week 7 Working with the BASH Shell. Objectives  Redirect the input and output of a command  Identify and manipulate common shell environment variables.
PMS /134/182 HEX 0886B6 PMS /39/80 HEX 5E2750 PMS /168/180 HEX 00A8B4 PMS /190/40 HEX 66CC33 By Adrian Gardener Date 9 July 2012.
Agenda User Profile File (.profile) –Keyword Shell Variables Linux (Unix) filters –Purpose –Commands: grep, sort, awk cut, tr, wc, spell.
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.
Builtins, namespaces, functions. There are objects that are predefined in Python Python built-ins When you use something without defining it, it means.
®® Microsoft Windows 7 for Power Users Tutorial 13 Using the Command-Line Environment.
Shell Scripting Introduction. Agenda What is Shell Scripting? Why use Shell Scripting? Writing and Running a Shell Script Basic Commands -ECHO - REM.
1 Functions 1 Parameter, 1 Return-Value 1. The problem 2. Recall the layout 3. Create the definition 4. "Flow" of data 5. Testing 6. Projects 1 and 2.
The Shell Chapter 7. Overview The Command Line Standard IO Redirection Pipes Running a Program in the Background Killing (a process!)
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
Linux Operations and Administration
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.
Agenda Link of the week Use of Virtual Machine Review week one lab assignment This week’s expected outcomes Review next lab assignments Break Out Problems.
Copyright © 2010 Certification Partners, LLC -- All Rights Reserved Perl Specialist.
Unix/Linux cs3353. The Shell The shell is a program that acts as the interface between the user and the kernel. –The shell is fully programmable and will.
Chapter 10: BASH Shell Scripting Fun with fi. In this chapter … Control structures File descriptors Variables.
1 PL\SQL Dev Templates. 2 TEMPLATE DEFINITION Whenever you create a new program unit, its initial contents are based upon a template which contains pre-defined.
5 1 Data Files CGI/Perl Programming By Diane Zak.
Ch 91 Pipes, Filters and Redirection. Ch 92 Overview Will use redirection to redirect standard input and standard output.
Lesson 4-Mastering the Visual Editor. Overview Introducing the visual editor. Working in an existing file with vi. Understanding the visual editor. Navigating.
Writing Scripts Hadi Otrok COEN 346.
LIN Unix Lecture 5 Unix Shell Scripts. LIN Command Coordination ; && || command1 ; command2 Interpretation: Do command 1. Then do command.
Copyright © 2003 ProsoftTraining. All rights reserved. Perl Fundamentals.
Chapter Six Introduction to Shell Script Programming.
Week 9 - Nov 7, Week 9 Agenda I/O redirection I/O redirection pipe pipe tee tee.
Lesson 3-Touring Utilities and System Features. Overview Employing fundamental utilities. Linux terminal sessions. Managing input and output. Using special.
File and Folder CLI Commands 12/24/ Agenda Overview of OS functions and the SHELL Internal v External Commands Command History Making & Modifying.
Agenda Positional Parameters / Continued... Command Substitution Bourne Shell / Bash Shell / Korn Shell Mathematical Expressions Bourne Shell / Bash Shell.
Agenda The Bourne Shell – Part II Special Characters Ambiguous File Reference Variable Names and Values User Created Variables Read-only Variables (Positional.
© 2015 by McGraw-Hill Education. This proprietary material solely for authorized instructor use. Not authorized for sale or distribution in any manner.
Batch Files Weaker form of UNIX shell scripts. Introduction UNIX may use one of several shells The shell is the command interpreter It interacts with.
Command Prompt Chapter 9 Pipes, Filters, and Redirection ©Richard Goldman 11/30/2000 Revised 10/16/2001.
Linux+ Guide to Linux Certification, Second Edition
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
© Prepared By: Razif Razali 1 TMK 265: UNIX SYSTEM CHAPTER FOUR – QUOTING IN DETAIL.
Lesson 6-Using Utilities to Accomplish Complex Tasks.
Batch Files Flow of Control to Strengthen Copyright © by Curt Hill.
Lesson 8-Specifying Instructions to the Shell. Overview An overview of shell. Execution of commands in a shell. Shell command-line expansion. Customizing.
Batch Files More flow of control Copyright © by Curt Hill.
Linux Administration Working with the BASH Shell.
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
1 Lecture 8 Shell Programming – Control Constructs COP 3353 Introduction to UNIX.
Linux Shell Script Programming
CST8177 Scripting 2: What?.
Presentation transcript:

Shell Scripting – Putting it All Together

Agenda Escaping Characters Wildcards Redirecting Output Chaining and Conditional Chaining Unnamed and Named Replacement Parameters IF statement IF ELSE statement Menus Subroutines and Procedures

Escaping Characters The “&” and “|” are special characters to the command interpreter If you wish to use these characters in a text string, they must be “escaped” by using the “^” (caret symbol) For example you want to echo the phrase “Yes & No”

Caret Symbol Input & Output By using the ^ symbol the Shell does not interpret the & symbol as a chaining command and thus displays the & as a string

Wildcards Most commands allow you to use wildcard characters to handle more than one file at a time. Asterisk (*) – substitutes multiple characters Question Mark (?) – substitutes only 1 character

Wildcards Example BUDGET.JANBUDGET.FEBBUDGET.MAR BANK.DOCREPORT12.DOCREPORT2.DOC C:\>dir *.* C:\>dir budget.* C:\>dir b* C:\>dir *.doc C:\>dir budget.?a? C:\>dir report?.doc

Redirecting Output You can modify where the output of a command goes By default the output of a commands is generally displayed on the screen You can redirect the output to a file (>, >>) You can redirect the output to be used as input for another command (|)

Redirecting Output to a File > - when you redirect output to an existing file, the redirected output replaces the original file >> - you can add, or append to the end of an existing file If the file doesn’t exist, it will be created in either case

Examples File1.txt This is file 1. File2.txt This is file 2. C:\>type file1.txt >> file2.txt C:\>type file1.txt > file2.txt C:\>type file1.txt > file3.txt

Connecting Commands with a Pipe You can redirect the output of one command to be the input of another. The two commands become connected using a pipe (|). You pipe the output of one command and use it as the input for a filter command. C:\>dir \DOS | more C:\>dir | sort

Chaining and Conditional Chaining & - chains two commands together ECHO Unsuccessful & GOTO :EOF && - executes the second command ONLY if the first command was successful COPY *.txt && ECHO Operation Successful || - executes the second command ONLY if the first command was unsuccessful COPY *.txt || ECHO Operation Unsuccessful

Replaceable Parameters A replaceable parameter is a “placeholder” for information entered on the command line and inserted by the command interpreter into the script during run time 2 types: Unnamed Replacement Parameters Use %1, %2, %3, %4, %5, %6, %7, %8, %9 Named Replacement Parameters Use a named variable, either e.g. %path% -- a predefined environment variable, or SET [variable]=[value] -- user defined variable

Unnamed Replacement Parameters Names are “hardwired” in the script – no flexibility No user input before script runs – no dynamic changes Changes to script must be retyped – thus, error prone

Unnamed Replacement Parameters Statements are not “hardwired” – gives flexibility User input on the CLI – allows dynamic changes No retyping of code – thus, less error prone

Unnamed Replacement Parameters

Using Parameters Requires… Error Entrapment Good programming tests for the existence of parameters and displays a message Documentation and messages Good programming provides the user with correct command syntax

Error Entrapment and Documentation

Named Replacement Parameters System-wide variables used to store information Two types Environment variables Can only be added, deleted or modified by members of the administrators group user defined Can be added, deleted and modified by members of the users group

Environment Variables The environment is an area of memory which the operating system uses as a scratch pad Stores “mission critical” information such as: COMPUTERNAME USERNAME USERPROFILE OS PATH TEMP and TMP + MANY MORE SEE SET

Predefined Environment Variables

User Defined Variables SET [variable]=[string]  used to create a variable SET /P [variable]=prompt[string] Used to capture user input SET /A [variable]=[value] Used to do arithmetic All information stored as text strings, even numbers

Named Replacement Parameters

GOTO command Use with caution if the label is before the script, you can create an endless loop (unless there is a control to bypass the GOTO) for example

GOTO Command On the other hand, if the label is after the GOTO statement, you can skip commands and jump to a new section Execution of the script will continue to the end and you cannot go back to the unexecuted commands unless you use another GOTO

GOTO Command Jumps to :Copy and executes the remaining commands

4 Types of Conditional Tests IF [NOT]“string1” == “string2” [command] compares two strings for same value IF [NOT] ERRORLEVEL 1 [command] Commands return all values 1 AND above 0 Success 1 Failure 2 execution failure4 math failure IF [NOT]“%ERRORLEVEL%” == “2” [command] Tests for a specific value ONLY IF [NOT] EXIST filename [command] Checks to see if a file is located in that directory To check a directory, add “\.” after the directory name IF [NOT] DEFINED [variable] [command] Tests for the existence of a variable

IF and GOTO Example

IF ELSE

Menu Displaying choices to the user and controlling user selection Branch using a user defined variable Using IF or IF /I to compare strings Branch using ERRORLEVEL Trap the actual key on the keyboard

Branching – Using variable

Branching – Using Error Level based on ASCII valuesASCII

Subroutines and Procedures Normally the “command interpreter” executes scripts line by line starting at the beginning of a file To change the order of execution we use subroutines and procedures Using Subroutines and Procedures helps to organize your code for easier reading and creates “building blocks” of code which can be easily reused

Subroutines Are created with the GOTO command The GOTO command creates an unconditional branch to a label A label identifies a location in your script A label is preceded with a colon, ie. :Start The Shell then executes the commands after the label

Procedures Are created using the CALL command External Internal The difference between a subroutine and a procedure is how the Shell behaves With procedures execution continues at the designated label, proceeds to the end of the file and then returns to the line following the CALL statement

External Procedures 1 2

Internal Procedures MyScript.cmd :start cmd1 CALL :Copy :Copy cmd1 cmd2 :Make cmd1 :EOF 1 2 GOTO :EOF