Shell programming Introduction to Operating Systems
Environment variables Predened variables $* all parameters $# number of parameters $? result of last command $$ process identier $i parameter number i $0 name of the script Positional variables $0, $1, $2,..., $9 $0 name of the script, $1 rst parameter, $2 second,... shift (useful when there are more than 10 parameters) Local variables variable=value (value is a string. NOT a number!) unset variable (variable is removed) Examples message= Hi, how are you Hi, how are you text counter=100 100 text message=hi_how_are_you Hi_how_are_you text Introduction to Operating Systems Shell programming 2
Commands echo writes in the standard output the text passed as argument Examples: echo hello # writes hello echo $x # writes the value of the variable x Special characters (used with the -e option): \c No return at the end \n New line \f New page \t Tabulator \r writes Return (returning to the start of the line) read allows reading values from the keyboard, assigning the values to the variables passed as parameters Example: echo e Insert the price: \c read price Introduction to Operating Systems Shell programming 3
Commands expr Used for basic arithmetic operations, by default writes the result in the standard output The following operators are possible: + Addition example: expr 3 + 2 - Subtraction example: expr 5-2 * Multiplication example: expr 6 \* 4 / Integer division example: expr 18 / 7 % Rest of integer division example: expr 18 % 7 The * character being special (meta-character), it is necessary to put the \ character before in order not to be considered as special Operator priority: %, *, /, +, - Examples: expr 3 + 4 # 7 will be written in the standard output x=`expr 3 + 4` # accents are necessary to evaluate as an operation expr `expr $x + $y` / `expr $a + $b` Introduction to Operating Systems Shell programming 4
Commands test Condition-expression: Parameters can be variables, constants, strings or les / directories. Syntax: test arg1 -option arg2 # [ ] can be used instead of test Options: Numbers Strings Files / Directories -eq equal = equal -a exists -ne not equal!= not equal -s exists, not empty -gt greater than -z null string -f regular le -ge greater than or equal -n not null string -d directory -lt less than -le less than or equal Condition-expressions can be combined via logic operators: -a AND -o OR -r read permission -w write permission -x exec. permission Introduction to Operating Systems Shell programming 5
if Conditional statement Syntax: a) if condition instructions b) if condition else Conditional statement: if instructions1 instructions2 condition: program / command TRUE when result is 0 FALSE when result is not 0 c) if condition1 instructions1 elif condition2 instructions2 Introduction to Operating Systems Shell programming 6
Conditional statement: if Examples: if test $n -gt 10 echo $n is greater than 10 if [ $# -lt 1 ] # error echo error: at least two parameters are needed exit 1 else # correct. Introduction to Operating Systems Shell programming 7
Conditional statement: case case multiple option sequence Syntax: a) case expression in pattern1) instructions1 ;; pattern2) instructions2 ;; pattern3) instructions3 ;;... *) instructions esac Introduction to Operating Systems Shell programming 8
Conditional statement: case Example: read var1 case $var1 in A) echo A has been chosen ;; B) echo B has been chosen ;; C) echo C has been chosen ;;... *) echo Invalid option esac Introduction to Operating Systems Shell programming 9
Iterative statement: while Set of instruction than will be repeated while the condition holds while Syntax: while condition instructions condition: it is a program TRUE when result is 0 FALSE when result is not 0 Introduction to Operating Systems Shell programming 10
Set of instruction than will be repeated until the condition holds until Iterative statement: until Syntax: until condition instructions condition: it is a program TRUE when result is 0 FALSE when result is not 0 Introduction to Operating Systems Shell programming 11
for Iterative statement: for Set of instructions that will be executed for each of the values passed Syntax: for variable in value_list instructions Introduction to Operating Systems Shell programming 12
Examples: Iterative statement: for for i in $* echo ===== $i parameter ===== for i in Ane Kol Miren echo Hello $i for number in 1 2 3 4 5 6 7 8 9 10 echo Message: $number Introduction to Operating Systems Shell programming 13
1.- Write the name of the script echo $0 Examples 2.- Write the number of users that are currently connected echo number of users: `who wc -l` 3.- Check if a given user (whose name is passed as parameter) is connected or not if who grep $1 >/dev/null echo $1 is connected Introduction to Operating Systems Shell programming 14
4.- Echo of the script name and its parameters echo $0 for i in $* echo $i Examples 5.- Write the rst 10 lines of les in the current directory whose name ends by.h, putting a header before each le for i in *.h echo ===== $i le ===== head $i Introduction to Operating Systems Shell programming 15
Examples 6.- Similar to the previous exercise, bat endings are passed as parameters and verifying that les are regular. It veries also that there is at least one parameter if [ $# -lt 1 ] echo "error: no parameters" exit 1 for type in $* for i in *.$type if [ -f $i ] echo "===== $i le =====" head $i Introduction to Operating Systems Shell programming 16
Examples 7.- Read a number, and if it is greater than 10 write a message and loop wn until 10 echo e Give me a number:\c read n if test $n -gt 10 #also: if [ $n gt 10 ] echo It is greater than 10" while [ $n ge 10 ] echo $n n=`expr $n 1` Introduction to Operating Systems Shell programming 17
Examples 8.- Write the last 10 lines of the les passed as parameters, putting a header before. It veries the existence of parameters, as well as the existence of the les passed if [ $# -lt 1 ] echo "error: parameters are required" exit 1 for i in $* if [ -f $i ] echo "===== $i le =====" tail $i Introduction to Operating Systems Shell programming 18
Examples 9.- Analyze the current directory and its subdirectories (only one level wn), writing the names of the les and directories encountered for i in * if [ -d $i ] echo Directory: $i/" for j in $i/* if [ -d $j ] echo " Sub-directory: $j/" if [ -f $j ] echo " Sub-le: $j" if [ -f $i ] echo "File: $i" Introduction to Operating Systems Shell programming 19