©Colin Jamison 2004 Shell scripting in Linux Colin Jamison.

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.
CS Lecture 03 Outline Sed and awk from previous lecture Writing simple bash script Assignment 1 discussion 1CS 311 Operating SystemsLecture 03.
Shell Programming Software Tools. Slide 2 Shells l A shell can be used in one of two ways: n A command interpreter, used interactively n A programming.
CS 497C – Introduction to UNIX Lecture 33: - Shell Programming Chin-Chih Chang
Linux+ Guide to Linux Certification, Second Edition
23-Jun-15Advanced Programming Spring 2002 bash Henning Schulzrinne Department of Computer Science Columbia University.
Bash, part 2 Prof. Chris GauthierDickey COMP Unix Tools.
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.
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:
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.
Chapter 5 Bourne Shells Scripts By C. Shing ITEC Dept Radford University.
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.
Linux+ Guide to Linux Certification, Third Edition
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.
CS465 - UNIX The Bourne 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.
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.
Shell Programming. Creating Shell Scripts: Some Basic Principles A script name is arbitrary. Choose names that make it easy to quickly identify file function.
LINUX programming UNIT-2 1. INDEX UNIT-IV PPT SLIDES Srl. No. Module as per Session planner Lecture No. PPT Slide No. 1.Working with Bourne shell L1 1-2.
Chapter 10: BASH Shell Scripting Fun with fi. In this chapter … Control structures File descriptors Variables.
Shells. Variables MESSAGE="HELLO WORLD" echo $MESSAGE MESSAGE="HELLO WORLD $LOGNAME" echo $MESSAGE MESSAGE="HELLO WORLD $USER" echo $MESSAGE.
(A Very Short) Introduction to Shell Scripts CSCI N321 – System and Network Administration Copyright © 2000, 2003 by Scott Orr and the Trustees of Indiana.
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.
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
CISC3130 Spring 2013 Fordham Univ. 1 Bash Scripting: control structures.
Shell Programming Features “Full” programming language “Full” programming language Conditional statements Conditional statements Arithmetic, String, File,
More Shell Programming. Slide 2 Control Flow  The shell allows several control flow statements:  if  while  for.
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
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.
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.
#!/bin/sh 2002/07/26 Jaeho Shin. Interface between user and the operating system sh, bash, csh, tcsh, ksh, … What is a “Shell”? OSuser Shell.
By Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
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.
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.
Agenda Bash Shell Scripting – Part II Logic statements Loop statements
CSC 352– Unix Programming, Fall 2012
Shell Scripting March 1st, 2004 Class Meeting 7.
Pepper (Help from Dr. Robert Siegfried)
Command Substitution Command substitution is the mechanism by which the shell performs a given set of commands and then substitutes their output in the.
Linux Shell Script Programming
Presented by, Mr. Satish Pise
Shell Control Structures
Chapter 5 The Bourne Shell
CSC 352– Unix Programming, Fall, 2011
Shell Control Structures
Introduction to Bash Programming, part 3
Bash Scripting CS 580U - Fall 2018.
Basic shell scripting CS 2204 Class meeting 7
Presentation transcript:

©Colin Jamison 2004 Shell scripting in Linux Colin Jamison

©Colin Jamison 2004 Shells A shell is an interface between the user and the Unix system, which allows the user to enter commands which the operating system executes There are a variety of shells available e.g. sh, csh, ksh, bash, etc. We shall use bash

©Colin Jamison 2004 Interactive Shell Use The shell can be used to directly input and run shell programming code It automatically performs wildcard expansions [set] allows any number of single characters to be checked [^set] negates the set of characters to be checked Brace expansion {} possible in bash

©Colin Jamison 2004 Creating a Script First line of the script starts with #!/bin/sh On all other lines, comments start with # and continue to the end of the line Environment variables for the shell script are the same as those for the user who runs the script

©Colin Jamison 2004 Run a Script To make the script executable chmod +x scriptname {Remove write permissions for other users} To run script type./scriptname

©Colin Jamison 2004 Syntax - Variables Variables are created when we first use them By default all variables are stored as strings To get at the contents of a variable precede it by $ Quotes: Variable name in double quotes - replaces variable with its value Variable name in single quotes - prints variable name literally

©Colin Jamison 2004 Variables - Example #!/bin/sh var=“Hello” echo $var echo “$var” echo ‘$var’ echo \$var exit 0 Output: Hello $var

©Colin Jamison 2004 Environment Variables $HOME - users home directory $PATH - colon separated list of directories $0- shell script name $#- number of parameters passed $$- PID of the shell script $IFS - Input field separator (usually whitespace)

©Colin Jamison 2004 Parameter Variables $1, $2,... - parameters passed to the script $* - list of all parameters separated by IFS - list of all parameters stored as separate fields

©Colin Jamison 2004 Syntax - Booleans If [ -f myC.c ] then … fi Note the space between each part of the conditional statement - this is required!

©Colin Jamison 2004 String Comparison “$string1” = “$string2” True if equal “$string1” != “$string2” True if not equal -n “$string” True if string is not null -z “$string” True if string is null

©Colin Jamison 2004 Arithmetic Comparison expression1 operator expression2 operator -eq equal to operator -ne not equal operator -gt greater than operator -ge greater than or equal to operator -lt less than operator -le less than or equal to ! expression True if expression false and vice versa

©Colin Jamison 2004 File Conditionals -d fileTrue if file a directory -e fileTrue if the file exists -f fileTrue if the file is a regular file -r fileTrue if the file is readable -s fileTrue if the file size is non-zero -w fileTrue if the file is writeable -x fileTrue if the file is executable

©Colin Jamison 2004 Syntax - Program Control (1) if [ condition ] then statements else statements fi elif allows more conditional tests (instead of else) for variable in values do statements done

©Colin Jamison 2004 Syntax - Program Control (2) while [ condition ] do statements done until [ condition ] do statements done

©Colin Jamison 2004 Syntax - Program Control (3) case variable in pattern [ | pattern] …) statements;; … esac

©Colin Jamison 2004 Syntax - Lists The AND list [ statement1 ] && [ statement2 ] && … Fails at the first false statement The Or list [ statement1 ] || [ statement2 ] || … Passes at the first true statement

©Colin Jamison 2004 Syntax - Functions function_name () { statements } You must always define a function before invoking it Return a numerical value using the return command Declare local variable/s by using the local keyword

©Colin Jamison 2004 Syntax - Built-in Commands (1) breakcan be used to break out of an enclosing for, while or until loop the : command: is an alias for true and useful for the conditional setting of variables continuemakes the enclosing for, while or until loop continue at the next iteration the. commandexecutes the command in the current shell echooutputs a string to standard output evalallows the evaluation of arguments execnormally used for replacing the current shell with a different program

©Colin Jamison 2004 Syntax - Built-in Commands (2) exit ncauses the script to exit with exit code n exportmakes the variable named as its parameter available in sub-shells expruse $((…)) printfuse in preference to echo in generating formatted output returncauses a function to return setsets the parameter variables for the shell shiftmoves all the parameter variables down by one e.g. $2 becomes $1

©Colin Jamison 2004 Syntax - Built-in Commands (3) trapused to specify the action to take on the receipt of signals unsetremoves variables or functions from the environment

©Colin Jamison 2004 Command Execution Arithmetic expansion x=$(($y+1)) Parameter expansion e.g. variable called counter - $file_number_${counter} other parameter expansions/substitutions are possible e.g. ${param:-default} ${#param} ${param#word} ${param##word} ${param%word} ${param%word}

©Colin Jamison 2004 Here Documents A special way of passing input to a command from a shell script command <<unique_descriptor statements unique_descriptor

©Colin Jamison 2004 Questions ?