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.

Slides:



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

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 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
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.
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.
8 Shell Programming Mauro Jaskelioff. Introduction Environment variables –How to use and assign them –Your PATH variable Introduction to shell programming.
Chapter 6: Shell Programming
An Introduction to Unix Shell Scripting
Chap 3 – PHP Quick Start COMP RL Professor Mattos.
Bash shell programming Part II – Control statements Deniz Savas and Michael Griffiths November 2005 Corporate Information and Computing Services The University.
Linux+ Guide to Linux Certification, Third Edition
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.
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.
Shell Programming. An example u tr abcdefghijklmnopqrstuvwxyz \ thequickbrownfxjmpsvalzydg file2 –encrypts file1 into file2 u record this command with.
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.
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.
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 Lecture 9 Shell Programming – Command substitution Regular expressions and grep Use of exit, for loop and expr commands COP 3353 Introduction to UNIX.
1 © 2000 John Urrutia. All rights reserved. Session 5 The Bourne Shell.
CSCI 330 UNIX and Network Programming Unit IX: Shell Scripts.
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,
Agenda Positional Parameters / Continued... Command Substitution Bourne Shell / Bash Shell / Korn Shell Mathematical Expressions Bourne Shell / Bash Shell.
1 Unix/Linux commands and shell programming-Part 2 (Dr. Mohamed El Bachir Menai)
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.
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]
Compunet Corporation Introduction to Unix (CA263) Round and Round By Tariq Ibn Aziz Dammam Community College.
Chapter 5 The Bourne Shell Graham Glass and King Ables, UNIX for Programmers and Users, Third Edition, Pearson Prentice Hall, Notes by Michael Weeks.
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.
ULI101 Week 10. Lesson Overview ● Shell Start-up and Configuration Files ● Shell History ● Alias Statement ● Shell Variables ● Introduction to Shell Scripting.
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
Agenda Bash Shell Scripting – Part II Logic statements Loop statements
CSC 352– Unix Programming, Fall 2012
Shell Scripting March 1st, 2004 Class Meeting 7.
Agenda Control Flow Statements Purpose test statement
Pepper (Help from Dr. Robert Siegfried)
Linux Shell Script Programming
Shell Control Structures
Chapter 5 The Bourne Shell
CSC 352– Unix Programming, Fall, 2011
Shell Control Structures
Introduction to Bash Programming, part 3
Review The Unix Shells Graham Glass and King Ables,
Presentation transcript:

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 repetitive procedure 3

4

Parameters and Variables  A shell parameter is associated with a value that is accessible to the user. Shell variables Names consist of letters, digits and underscores By convention, environment variables use UPPERCASE Names must start with a letter or underscore User created variables (create and assign value) Keyword shell variables Have special meaning to the shell Being created and initialized by the startup file 5

Parameters and Variables Positional parameters Allow you to access command line arguments Special parameters Such as The name of last command The status of most recently executed command The number of command-line arguments 6

Shell variables To assign a value to a variable in the Bourne Again Shell, use the following syntax: VARIABLE=value myvar=abc Example person=alex echo person person echo $person alex 7

Shell variables The $VARIABLE syntax is a special case of the more general syntax ${VARIABLE}. Braces are necessary when concatenating a variable value with a string $ PREF=counter $ WAY=${PREF}clockwise $ FAKE=${PREF}feit $ echo $WAY $FAKE counterclockwise counterfeit 8

Shell variables Differences between “ ”, ‘ ’, \: $ echo $person alex $ echo "$person" alex $ echo '$person' $person $ echo \$person $person 9

Shell variables Read : Using read, scripts can accept input from the user and store that input in variables $ read program abc $ echo $program When you do not specify a variable to receive read’s input, bash puts the input into the variable named REPLY. $ read abc $ echo $REPLY 10

Some useful commands unset: Removes a Variable $ unset person readonly: Makes the Value of a Variable Permanent $ readonly person declare : Assign Attributes to Variables Examples : $ declare -x -r person3=helen $ declare +x person3 $ declare –x / declare -r 11

Array Variables The Bourne Again Shell supports one-dimensional array variables. The subscripts are integers with zero-based indexing (i.e., the first element of the array has the subscript 0). Syntax : name=(element1 element2...) $ NAMES=(max helen sam zach) Refer to a single element of an array as follows: $ echo ${NAMES[2]} 12

Locality of Variables You can have both global environment and local shell variables.  Global environment variables are set by your login shell and new programs and shells the environment of their parent shell.  Local shell variables are used only by that shell and are not passed on to other processes. A child process cannot pass a variable back to its parent process. 13

Locality of Variables Once you use the export builtin with a variable name as an argument, the shell places the value of the variable in the calling environment of child processes. This call by value gives each child process a copy of the variable for its own use 14

Locality of Variables Example $ cat > extest1 cheese=american echo "extest1 1: $cheese" bash./subtest echo "extest1 2: $cheese" $ cat > subtest echo "subtest 1: $cheese" cheese=swiss echo "subtest 2: $cheese" $ bash./extest1 extest1 1: american subtest 1: subtest 2: swiss extest1 2: american $ cat > extest2 export cheese=american echo "extest1 1: $cheese" bash./subtest echo "extest1 2: $cheese" $ bash./extest2 extest1 1: american subtest 1: american subtest 2: swiss extest1 2: american 15

Global variables Some global environment variables are, HOME Path to your home directory HOST The hostname of your system LOGNAME The name you login with PATH Paths to be searched for $ PATH=/usr/local/bin:/bin:/usr/bin:~/bin: $ PATH=$PATH:. 16

Alias An alias is a name that the shell translates into another name or (complex) command. They are typically placed in the ~/.bashrc startup files The syntax of the alias builtin is alias [name[=value]] Example $ alias ls='ls -F‘ Avoid alias substitution by preceding the aliased command with a backslash (\): $ \ls 17

Alias Remove an alias with the unalias built-in. $ unalias zap $ alias command list all aliases 18

Using a Default Value The :– modifier uses a default value in place of a null or unset variable while allowing a nonnull variable to represent itself : ${name:–default} Example: ls ${LTI:-/home/oslab} The := modifier uses assign a default value to a null variable : ${name:=default} Example: ls ${LTI:=/home/oslab} 19

Positional Parameters  The command name and arguments are the positional parameters. Because you can reference them by their position on the command line $0 : Name of the calling program $1 - $9 : Command-line Arguments The first argument is represented by $1 The second argument is represented by $2 And so on up to $9 The rest of arguments have to be shifted to be able to use $1- $9 parameters. For values of n over 9, the number must be enclosed within braces -> ${12} 20

Positional Parameters Examples $ cat > abc echo "The command used to run this script is $0" $ bash./abc The command used to run this script is./abc Example $ cat > display_5args echo “First 5 arguments are $1 $2 $3 $4 $5” $ bash./display_5args First 5 arguments are

Function Functions are defined similar to C and Java You can declare a shell function in the ~/.bash_profile startup file An example of a function function hello() { echo hello world! } Usage : $ hello 22

Function An example of a function function print () { echo $1 echo $2 } No parameter is defined in the deceleration of function Usage : $ print

Function Because functions run in the same environment as the shell that calls them, variables are implicitly shared by a shell and a function it calls. $ function nam () { > echo $myname > myname=zach > } $ myname=sam $ nam sam $ echo $myname zach 24

Locality of function variables The typeset built-in declares a variable to be local to the function it is defined in. $ function count_down () { > typeset count > count=$1 > while test $count -gt 0 > do > echo "$count..." > ((count=count-1)) > sleep 1 > done > echo "Blast Off." > } $ count=10 $ count_down Blast Off. $ echo $count 10 25

Shift  $1-$9 allows you to access 10 arguments How to access others?  Promote command-line arguments: shift Built-in command shift promotes each of the command- line arguments. The first argument ( which was $1) is discarded The second argument ( which was $2) becomes $1 The third becomes the second And so on Makes additional arguments available Repeatedly using shift is a convenient way to loop over all the command-line arguments 26

Shift  Example: $ cat > demo_shift #!/bin/tcsh echo $1 $2 $3 shift echo $1 $2 shift echo $1 $ bash./demo_shift $ bash./demo_shift

Special Parameters  The number of arguments: $# Return a decimal number Use the test to perform logical test on this number Example : $ cat > num_args echo "This script was called with $# arguments." $ bash./num_args sam max zach This script was called with 3 arguments. 28

Special Parameters  Exit status: $? When a process stops executing for any reason, it returns an exit status to its parent process. By convention, Nonzero represents a false value that the command failed. A zero value is true and means that the command was successful Example : $ ls xxx ls: xxx: No such file or directory $ echo $? 1 29

Special Parameters  Value of Command-line arguments: $* and $* and represent all the command_line arguments ( not just the first nine) $* : treats the entire list of arguments as a single argument : produce a list of separate arguments (Only bash/ksh/sh) Example : $ cat > display_all echo All arguments are $* $ bash./display_all a b c d e f g h i j k l m n o p All arguments are a b c d e f g h i j k l m n o p 30

Special Parameters  The PID: $$ $echo $$  $! : The value of the PID number of the last process that you ran in the background is stored in $! 31

32

Expression The expr command: VALUE=`expr VALUE \* 10 + NEW` The let command $ let "VALUE=VALUE * 10 + NEW“ $ let "VALUE=VALUE *10+NEW (without space) Bash accepts ((expression)) as a synonym for let "expression", obviating the need for both quotation marks and dollar signs: ((VALUE=VALUE * 10 + NEW)) 33

Notes on expr Why do we need the expr command ??? E.g: $ file=1+2 $echo $file 1+2 NOTE: 1+2 is copied as it is into val and not the result of the expression, to get the result, we need expr. $ file=`expr 1 + 2` or $ let “file=1 + 2” 34

Notes on expr expr supports the following operators: arithmetic operators: +,-,*,/,% comparison operators: =, > boolean/logical operators: &, |,! parentheses: (, ) precedence is the same as C, Java Use [[expr]] when using expression as a condition if [[ 30 < $age && $age < 60 ]] 35

String Pattern Matching The syntax for these operators is ${varname op pattern} Example : $ SOURCEFILE=/usr/local/src/prog.c $ echo ${SOURCEFILE#/*/} local/src/prog.c $ echo ${SOURCEFILE##/*/} prog.c $ echo ${SOURCEFILE%/*} /usr/local/src $ echo ${SOURCEFILE%/*} 36

String operators # Removes minimal matching prefixes ## Removes maximal matching prefixes % Removes minimal matching suffixes % Removes maximal matching suffixes 37

38

Control statements The three most common types of control statements: conditionals: if/then/else, case,... loop statements: while, for, until, do,... branch statements: subroutine calls (good programming practice), goto (usage not recommended). 39

Conditionals Conditionals are used to test something. In Java or C, they test whether a Boolean variable is true or false. In a Bourne shell script, the only thing you can test is whether or not a command is “successful”. 40

Conditionals Every well behaved command returns back a return code. 0 if it was successful Non-zero if it was unsuccessful (actually ) This is different from C. 41

The if command Simple form: if decision_command then command_set fi 42

Example #! /bin/bash if grep unix myfile >/dev/null then echo "It's there" fi grep returns 0 if it finds something returns non-zero otherwise redirect to /dev/null so that "intermediate" results do not get printed 43

The test command Use for checking validity. Three kinds: Check on files. Check on strings. Check on integers 44

Notes on test Testing on files. test -e file: does file exist? test –f file: does file exist and is not a directory? test -d file: does file exist and is a directory? test –x file: does file exist and is executable? test –r file: does file exist and is readable ? test –w file: does file exist and is writable ? 45

Notes on test Testing on files. test –s file: does file exist and is longer than 0 bytes? test file1 -nt file2: does if file1 is newer than (according to modification time) file2? test file1 -ot file2: does file1 is older than file2 46

Notes on test Testing on strings. test –z string: is string of length 0 (is empty)? test –n string: is string is not empty? test string1 = string2: does string1 equal string2? test string1 != string2: not equal? 47

Example #! /bin/bash if test -z $REMOTEHOST then : else DISPLAY="$REMOTEHOST“ echo $DISPLAY fi NOTE: This example tests to see if the value of REMOTEHOST is a string of length > 0 or not, and then sets the DISPLAY to the appropriate value. 48

Notes on test Testing on integers. test int1 –eq int2: is int1 equal to int2 ? test int1 –ne int2: is int1 not equal to int2 ? test int1 –lt int2: is int1 less than to int2 ? test int1 –gt int2: is int1 greater than to int2 ? test int1 –le int2: is int1 less than or equal to int2 ? test int1 –ge int2: is int1 greater than or equal to int2 ? 49

Example #!/bin/bash smallest=10000 for i in do if test $i -lt $smallest then smallest=$i fi done echo $smallest NOTE: This program calculates the smallest among the numbers 5, 8, 19, 8, 3. 50

Notes on test The test command has an alias ‘ [] ’. Each bracket must be surrounded by spaces #!/bin/bash smallest=10000 for i in do if [ $i -lt $smallest ] then smallest=$i fi done echo $smallest 51

Logical Evaluation (Conditional Expressions) The syntax of a conditional expression is [[ expression ]] where expression is a Boolean (logical) expression. You must precede a variable name with a dollar sign ($) within expression. if [[ $age>30 && $age<60 ]]; then AND and OR test : uses –a as a Boolean AND operator, and –o as OR operator [[ expression ]] : uses && as a Boolean AND operator, and || as OR operator 52

The elif command Simple form: if decision_command then command_set elif decision_command then command_set … else command_set fi 53

Using elif with if #! /bin/bash if grep "UNIX" myfile >/dev/null then echo UNIX occurs in myfile elif grep “DOS” myfile > /dev/null then echo DOS appears in myfile not UNIX else echo nobody is here in myfile fi 54

for loops for loops allow the repetition of a command for a specific set of values. Syntax: for var in value1 value2... do command_set done command_set is executed with each value of var (value1, value2,...) in sequence 55

for loops Other syntax: for var in {START..END} do command_set done for var in {START..END..INCREMENT} do command_set done for (( expr1; expr2; expr3 )) do command_set done 56

Notes on for Example: Listing all files in a directory. for i in * do if [ -d "$i" ] then echo "$i" fi done NOTE: * is a wild card that stands for all files in the current directory, and for will go through each value in *, which is all the files and $i has the filename. 57

Notes on for Examples #!/bin/bash for i in {1..5} do echo "Welcome $i times" done #!/bin/bash for i in { } do echo "Welcome $i times" done #!/bin/bash for (( c=1; c<=5; c++ )) do echo "Welcome $c times" done #!/bin/bash for (( ; ; )) do echo "infinite loops [ hit CTRL+C to stop]" done 58

Example – count executables in directory #!/bin/bash count=0 for i in *; do if test –x $i then count=`expr $count + 1` fi done echo Total of $count files executable NOTE: expr $count + 1 serves the purpose of count++ 59

While loop Syntax while test-command do commands done 60

While loop #! /bin/bash i=1 sum=0 while [ $i -le 100 ] do sum=`expr $sum + $i` i=`expr $i + 1` done echo The sum is $sum. NOTE: The value of i is tested in the while to see if it is less than or equal to 100. Example 61

until Syntax number=0 until test-command; do commands done 62

until Example #!/bin/bash number=0 until [ $number -ge 10 ]; do echo "Number = $number" number=$((number + 1)) done 63

case Syntax case word in patterns ) commands ;; pattern ) command esac 64

case Example #!/bin/bash echo -n "Type a digit or a letter > " read character case $character in [a-z] | [A-Z] ) echo "You typed the letter $character" ;; [0-9] ) echo "You typed the digit $character" ;; * ) echo "You did not type a letter or a digit" esac 65

break Syntax break or break n 66

break Example #!/bin/bash a=0 while [ $a -lt 10 ] do echo $a if [ $a -eq 5 ] then break fi a=`expr $a + 1` done 67

break Example #!/bin/bash for var1 in do for var2 in 0 5 do if [ $var1 -eq 2 -a $var2 -eq 0 ] then break 2 else echo "$var1 $var2" fi done 68

continue Syntax continue Or continue n 69

continue Example #!/bin/bash NUMS=" " for NUM in $NUMS do Q=`expr $NUM % 2` if [ $Q -eq 0 ] then echo "Number is an even number!!" continue fi echo "Found odd number" done 70

71

Using colon in shell scripts Sometimes, we do not want a statement to do anything. In that case, use a colon ‘ : ’ if grep UNIX myfile > /dev/null then : fi Does not do anything when UNIX is found in myfile. 72

Using semicolon in shell scripts Because a semicolon (;) ends a command just as a NEWLINE does, you can place then on the same line as if by preceding it with a semicolon Example if test-command ; then commands else commands fi 73

Debugging Shell Scripts You can use the shell’s –x option to help debug a script. This option causes the shell to display each command before it runs the command Example Bash –x./if2 74

Enable shell options You can set the an option of the shell running the script by running the set command You can set the an option of the shell running the script by putting the set command at the top of the script: Example : set –x : turn debugging on set +x : turn debugging off 75

Shell parameters  Shell parameters HOME PATH SHELL $0 : name of calling program $n : n’th argument $*: total argument total argument $# : number of argument $? : Exit status $$ : process ID of current command 76