(A Very Short) Introduction to Shell Scripts CSCI N321 – System and Network Administration Copyright © 2000, 2003 by Scott Orr and the Trustees of Indiana.

Slides:



Advertisements
Similar presentations
CIS 240 Introduction to UNIX Instructor: Sue Sampson.
Advertisements

Linux Shell Script. Shell script The first line is used to specify shell program #!/bin/sh Variables variable=“text” variable=0 variable=`program arguments`
Introduction to Unix – CS 21 Lecture 11. Lecture Overview Shell Programming Variable Discussion Command line parameters Arithmetic Discussion Control.
CSET4100 – Fall 2009 Perl Introduction Scalar Data, Operators & Control Blocks Acknowledgements: Slides adapted from NYU Computer Science course on UNIX.
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
Chapter 15 Mr. Mohammad Smirat SHELL PROGRAMMING (Bourne Shell)
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 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 Control Structures CSE 2031 Fall August 2015.
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.
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.
Chapter 5 Bourne Shells Scripts By C. Shing ITEC Dept Radford University.
An Introduction to Unix Shell Scripting
The if construct The general format of the if command is: Every command has exit status If the exit status is zero, then the commands that follow between.
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"
Linux+ Guide to Linux Certification, Third Edition
Linux Operations and Administration
CMPSC 60: Week 6 Discussion Originally Created By: Jason Wither Updated and Modified By: Ryan Dixon University of California Santa Barbara.
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 Programming. Introducing UNIX Shells  Shell is also a programming language and provides various features like variables, branching, looping and.
1 System Administration Introduction to Scripting, Perl Session 3 – Sat 10 Nov 2007 References:  chapter 1, The Unix Programming Environment, Kernighan.
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.
1. 2 What is a Shell Script? A Text File With Instructions Executable  Why shell script? Simply and quickly initiate a complex series of tasks or a.
Chapter 10: BASH Shell Scripting Fun with fi. In this chapter … Control structures File descriptors Variables.
1 P51UST: Unix and Software Tools Unix and Software Tools (P51UST) More Shell Programming Ruibin Bai (Room AB326) Division of Computer Science The University.
©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.
CSCI 330 UNIX and Network Programming Unit IX: Shell Scripts.
Shell Script2 Reference: Linux Shell Scripting Tutorial v1.05r3 A Beginner's handbook
Shell Programming Features “Full” programming language “Full” programming language Conditional statements Conditional statements Arithmetic, String, File,
Agenda Positional Parameters / Continued... Command Substitution Bourne Shell / Bash Shell / Korn Shell Mathematical Expressions Bourne Shell / Bash Shell.
UNIX Shell Script (2) Dr. Tran, Van Hoai Faculty of Computer Science and Engineering HCMC Uni. of Technology
Flow Control. The order in which commands execute in a shell script is called the flow of the script. When you change the commands that execute based.
Lab 8 Shell Script Reference: Linux Shell Scripting Tutorial v1.05r3 A Beginner's handbook
Shell script – part 2 CS 302. Special shell variable $0.. $9  Positional parameters or command line arguments  For example, a script myscript take 2.
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]
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.
By Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
Linux Administration Working with the BASH Shell.
Shell Control Structures CSE 2031 Fall June 2016.
1 Lecture 8 Shell Programming – Control Constructs COP 3353 Introduction to UNIX.
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
Shell Features CSCI N321 – System and Network Administration
CSC 352– Unix Programming, Spring 2016, Final Exam Guide
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
Command Substitution Command substitution is the mechanism by which the shell performs a given set of commands and then substitutes their output in the.
Copyright © – Curt Hill Bash Flow of Control Copyright © – Curt Hill.
Presented by, Mr. Satish Pise
Shell Control Structures
CSC 352– Unix Programming, Fall, 2011
Shell Control Structures
Chapter 5 Bourne Shells Scripts
Essential Shell Programming
Introduction to Bash Programming, part 3
Basic shell scripting CS 2204 Class meeting 7
Presentation transcript:

(A Very Short) Introduction to Shell Scripts CSCI N321 – System and Network Administration Copyright © 2000, 2003 by Scott Orr and the Trustees of Indiana University

Section Overview Comments and Script Headers Variables Flow Control Loops

References Linux System Administration Chapter 14 CQU COIT System Administration Course Textbook   Chapter 9 Chapter 9 Lectures   2002 # #8

Why Learn Scripting? /bin/sh always there Understand/Modify Existing System Scripts RC Startup Scripts Daemon Startup Scripts Automate repetitive tasks Great for small tools

Writing/Debugging Scripts Know how to do job manually first Write in small (logical) parts Test each part before continuing Print modified variables Print command lines to be executed Never work on production data Comment code!!!

Starting Scripts Passed as input to a shell Example: /bin/bash mysript Run directly from the command line First line of script (i.e: #!/bin/bash ) Script must have execute bits set $PATH considerations

Commenting code Anything following ‘ # ’ is considered a comment and ignored Script Header Tells what the script does Common Entries: Author Date written Program name Command line usage Purpose Files used Notes/features Revision History

Argument Variables VariablePurpose $0 Name of the program $1 - $9 Command line arguments $# Number of Arguments $* All arguments as 1 word Each argument is a word

Other Special Variables VariablePurpose $$ PID number of script $! PID number of parent process $? Exit status of script (Checked by parent)

Basic Input read variable [ < filename ] Examples: read ANS read LINE < file (only reads first line)

Basic output echo [ -n ] [ -e ] “string” -n : No new line -e : Allow backslash options Example: echo –n “Enter your name: “

Backslash Options CharacterPurpose \aAlert (bell) \bBackspace \cDon’t display trailing newline \nNewline \rCarriage return \tHorizontal Tab \vVertical Tab \\Backslash \######: ascii value of character

Expression Evaluation expr command Math expressions ( +, -, *, /, % ) String expressions (match, substring, etc.) Logic expressions ( &, |, =, etc.) Example: COUNT=`expr $COUNT + 1`

Flow Control Need to determine which commands to run based on one or more conditions Useful to catch errors Provides flexibility in code Classic if-then-else model

If-Then condition Basic decision construct if condition then commands fi

String Tests Expression True if… -z string Length of string is 0 -n string Length of string is not 0 string1 = string2 If the 2 strings are identical string1 != string2 If the 2 strings are different string If string is not NULL

Numeric Tests Expression True if… int1 –eq int2 int1 equals int2 int1 –ne int2 int1 and int2 not equal int1 –gt int2 int1 > int2 int1 –ge int2 int1 >= int2 int1 –lt int2 int1 < int2 int1 –le int2 int1 <= int2

File Tests Expression True if… -r file File exists and readable -w file File exists and writable -x file File exists and executable -f file File exists and is a regular file -d file File exists and is a directory -h file File exists and is a symbolic link -p file File exists and is a named pipe

File Tests (Con’t.) Expression True if… -c file File exists and is a character device -b file File exists and is a block device -u file File exists and is SUID -g file File exists and is SGID -k file File exists and sticky bit set -s file File exists and size > 0 bytes

Logic Operators ExpressionPurpose ! Negates the expression -a AND -o OR ( expression ) Groups the expression

If-Then-Else condition if condition then commands else commands fi

If-Then-Else If condition if condition then commands elif condition then commands fi

Case condition Perform a range of tests on a variable case variable in pattern1) commands ;; pattern2) commands ;; esac *) matches all other cases

Loops Need to run block of code more than once Modes Repeat a set number of times Repeat while a condition is true Repeat until a condition is true

for loop Repeat for each variable in list for variable in variable_list do commands done

while loop Repeat until condition is false while [ condition ] do commands done

Reading Files Read a file one line at a time while read LINE do echo $LINE done < $FILENAME

do-until loop Repeat until condition is true until [ condition ] do commands done

Escaping out of loops Sometimes you need to exit loop early Error conditions Special conditions break command Example: while [ 1 ] do echo –n “Done? “ read ANS if [ $ANS = “y” ] then break fi done

Other scripting languages Perl Very popular among sysadmins Many modules available TCL/TK Expect Python