What is Bash Shell Scripting?

Slides:



Advertisements
Similar presentations
CST8177 awk. The awk program is not named after the sea-bird (that's auk), nor is it a cry from a parrot (awwwk!). It's the initials of the authors, Aho,
Advertisements

CIS 240 Introduction to UNIX Instructor: Sue Sampson.
Introduction to Unix – CS 21 Lecture 11. Lecture Overview Shell Programming Variable Discussion Command line parameters Arithmetic Discussion Control.
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 Lecture 03 Outline Sed and awk from previous lecture Writing simple bash script Assignment 1 discussion 1CS 311 Operating SystemsLecture 03.
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.
Shell Script Examples.
Shell Control Structures CSE 2031 Fall August 2015.
Second edition Your UNIX: The Ultimate Guide Das © 2006 The McGraw-Hill Companies, Inc. All rights reserved. UNIX – Shell Programming The activities of.
Shell Programming, or Scripting Shirley Moore CPS 5401 Fall August 29,
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.
8 Shell Programming Mauro Jaskelioff. Introduction Environment variables –How to use and assign them –Your PATH variable Introduction to shell programming.
Week 7 Working with the BASH Shell. Objectives  Redirect the input and output of a command  Identify and manipulate common shell environment variables.
An Introduction to Unix Shell Scripting
Shell Scripting Todd Kelley CST8207 – Todd Kelley1.
CST8177 bash Scripting Chapters 13 and 14 in Quigley's "UNIX Shells by Example"
Additional UNIX Commands. 222 Lecture Overview  Multiple commands and job control  More useful UNIX utilities.
Introduction to Linux OS (IV) AUBG ICoSCIS Team Prof. Volin Karagiozov March, 09 – 10, 2013 SWU, Blagoevgrad.
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.
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.
#!/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.
1 System Administration Introduction to Scripting, Perl Session 3 – Sat 10 Nov 2007 References:  chapter 1, The Unix Programming Environment, Kernighan.
Linux+ Guide to Linux Certification Chapter Eight Working with the BASH Shell.
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.
(A Very Short) Introduction to Shell Scripts CSCI N321 – System and Network Administration Copyright © 2000, 2003 by Scott Orr and the Trustees of Indiana.
Writing Scripts Hadi Otrok COEN 346.
Shell Programming Learning Objectives: 1. To understand the some basic utilities of UNIX File 2. To compare UNIX shell and popular shell 3. To learn the.
©Colin Jamison 2004 Shell scripting in Linux Colin Jamison.
1 Lecture 9 Shell Programming – Command substitution Regular expressions and grep Use of exit, for loop and expr commands COP 3353 Introduction to UNIX.
CSCI 330 UNIX and Network Programming Unit IX: Shell Scripts.
Agenda Positional Parameters / Continued... Command Substitution Bourne Shell / Bash Shell / Korn Shell Mathematical Expressions Bourne Shell / Bash Shell.
Sed. Class Issues vSphere Issues – root only until lab 3.
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.
Compunet Corporation Introduction to Unix (CA263) Round and Round By Tariq Ibn Aziz Dammam Community College.
By Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
1 UNIX Operating Systems II Part 2: Shell Scripting Instructor: Stan Isaacs.
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.
Computer Programming Batch & Shell Programming 9 th Lecture 김현철 컴퓨터공학부 서울대학교 2009 년 가을학기.
Bash Shell Scripting 10 Second Guide.
Shell Control Structures
CSC 352– Unix Programming, Spring 2016, Final Exam Guide
Bash Introduction (adapted from chapters 1 and 2 of bash Cookbook by Albing, Vossing, & Newham) CPTE 440 John Beckett.
Agenda Bash Shell Scripting – Part II Logic statements Loop statements
CSC 352– Unix Programming, Fall 2012
Shell Scripting March 1st, 2004 Class Meeting 7.
Lecture 9 Shell Programming – Command substitution
Unix Scripting Session 4 March 27, 2008.
LING 408/508: Computational Techniques for Linguists
Agenda Functions: What are functions? / Purpose of Functions
Shell Programming.
CSE 303 Concepts and Tools for Software Development
Linux Shell Script Programming
Shell Control Structures
CSC 352– Unix Programming, Fall, 2011
Shell Control Structures
CST8177 Scripting 2: What?.
Introduction to Bash Programming, part 3
Bash Scripting CS 580U - Fall 2018.
Basic shell scripting CS 2204 Class meeting 7
Presentation transcript:

What is Bash Shell Scripting? A shell script is a script written for the shell, or command line interpreter, of an operating system. The shell is often considered a simple domain-specific programming language.  Typical operations performed by shell scripts include file manipulation, program execution, and printing text.

What is Bash Shell Scripting? Bash Shell Script is an interpreted language. This means that the shell analyzes each statement in the language one line at a time, then executes it. This differs from languages such as C, in which programs are compiled into executable files. Interpreted languages are generally easier to debug and modify; however, they usually take much longer to execute than compiled programs.

About My Shell Script My program is a simple network monitoring script that acts as wrapper for the ping command. It takes and IP address or multiple IP addresses as arguments, creates a log file of ping statistics, and outputs the connection status of the host. I intended to use it for lab 4, to monitor the time it took for dynamic routing to reroute network traffic when a node is taken down.

Special Shell Variables $0 The name of the program is assigned here. $1 - $9 The arguments typed on the command line are assigned here. ${10} Any argument after $9 must be accessed using curly braces.

Special Shell Variables $# Number of arguments passed to the program or number of parameters set by executing the set statement. $* Collectively references all positional parameters as $1, $2, … $@ Same as $* except when double quoted; collectively references all positional parameters as “$1”, “$2”, …

Special Shell Variables $? Exit status of the last command not executed in the background. $! The process ID number of the last program sent to the background for execution. $$ The process ID number of the program being executed.

Special Shell Variables (( )) Arithmetic operator; parses faster, only accepts numeric input. [ ] Idiomatic operator; shell built in, older and slower, accepts alpha-numeric input.

Let’s have a look at my original code. Version 1.0 Let’s have a look at my original code.

Improvements Version 1.0 Version 1.1 #!/bin/bash # # Monitor: a script to monitor the connection # status of one or more IP addresses # Author Sean Callahan & Solomon Bundy IP= COUNT="-c 1" INTERVAL="-i 1" EMSG="[-i interval] [-c count] [-b run in bg] [--help] <IPaddress> <IPaddress>" if [ "$#" -eq 0 ] #Test for no args then echo "$EMSG" exit 0 fi TEST=$(echo "$@" | grep ^--help$) #Test for --help if [ "$?" -eq 0 ] exit 1 #!/bin/bash # # Monitor: a script to monitor the connection # status of one or more IP addresses # Author Sean Callahan & Solomon Bundy _ip= _count="-c 1" _interval="-i 1" _emsg="[-i interval] [-c count] [--help] <IPaddress> <IPaddress>" if (("$#"==0)) #Test for no args then echo "$_emsg" exit 0 fi echo "$@" | grep -q '^--help$' #Test for --help if (("$?"==0)) exit 1

More Improvements Version 1.0 Version 1.1 while [ "$#" -gt 0 ] #Start main loop do TEST2=$(echo "$1" | grep ^-c$) #grep for option -c if [ "$?" -eq 0 ] then COUNT="$1 $2" #Assign positional parameter $1 and $2 to COUNT shift else TEST3=$(echo "$1" | grep ^-c[0-9]) #grep for option -c w/ space COUNT="$1" #Assign positional parameter $1 to COUNT fi while (("$#">0)) #Start main loop do echo "$1" | grep -q '^-c$‘ #grep for option -c if (("$?"==0)) then _count="$1 $2“ #Assign positional parameter $1 and $2 to _count shift else echo "$1" | grep -q '^-c[0-9]' #grep for option -c w/ space _count="$1“ #Assign positional parameter $1 to _count fi

More Improvements Version 1.0 Version 1.1 set "$@" #Set all args (only IP addresses should be left at this point) echo "$1" | grep -E -o -q '(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)‘ #Above line greps for a valid IP address if [ "$?" -ne 0 ] then echo "INVALID IP SKIPPING..." shift continue else IP="$1" fi set "$@” #Set all args (only IP addresses should be left at this point) echo "$1" | grep -E -o -q '(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)‘ #Above line greps for a valid IP address if (("$?"!=0)) then echo "INVALID IP SKIPPING..." shift continue else _ip="$1" fi

GREP Options echo "$1" | grep -E -o -q '(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\. -E Extend regular expression. -o Only matching -q Quiet mode

One Regular Expression To Rule Them All echo "$1" | grep -E -o -q '(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\. The | is the alternation operator. Since the alternation operator has the lowest precedence of all, we use the round brackets to group the alternatives together. The ? makes the preceding item optional. The \ is an escape character. The expression will first test 250 -255. If this fails, it will look for the next set of numbers, 200 -249. If this fails, it will look for 100 – 199, then 0-99. If successful, it will return 0, and test the next set of numbers in the expression. If nothing is found, it will return 1, and stop.

More Improvements Version 1.0 Version 1.1 ping "$COUNT" "$INTERVAL" "$IP" 1> $IP.log #Create a log file of ping output echo "Monitoring "$IP" " PING=$(ping "$COUNT" "$INTERVAL" "$IP" | grep 'received' | awk -F',' '{ print $2 }' | awk '{ print $1 }') #Check ping status if [ "$PING" -eq 0 ] then echo "Host : $IP is down (ping failed) at $(date)" else echo "Host : $IP is up (ping succeeded at $(date)" fi done exit 0 echo $(date) >> "$_ip".log ping "-W 3" "$_count" "$_interval" "$_ip" >> "$_ip".log #Create a log file of ping output echo " " >> "$_ip".log echo "Monitoring "$_ip" “ _result=$(ping "-W 3" "$_count" "$_interval" "$_ip" | grep 'received' | awk -F',' '{ print $2 }' | awk '{ print $1 }') #Check ping status if (("$_result"==0)) then echo "Host : "$_ip" is down (ping failed) at $(date)" else echo "Host : "$_ip" is up (ping succeeded) at $(date)" fi done exit 0

What’s Next? Utilize the /etc/hosts file to allow users to type in host names as well as IP addresses. Separate the regular expression and ping code into their own loops, so that the program won’t scan for all of the options every time it loops. Include an option for the program to run silently in the background, and only bring itself into the foreground when a ping is successful.