Spring 23-24 Os Lab 8
Spring 23-24 Os Lab 8
Spring 23-24 Os Lab 8
❑ System Variables:
❑ Usually maintained by Operating systems
❑ Written in all capitals
❑ Examples:
❑ $BASH [knowing the bash location]
❑ $BASH_VERSION [knowing the bash version]
❑ $HOME [knowing the home directory]
❑ $PWD [present working directory]
Shell Variables (cont’d)
echo $Name
❑ Try
❑ echo My name is $Name
❑ echo “My name is ”$Name
❑ echo “This is ”$Name“ Who did this!”
Shell Variables Rules
❑ The shell does not care about types of variables; they may store
strings, integers, real numbers - anything you like.
❑ Loosely coupled
❑ Escape Characters
❑ \
❑ Example: Hello \”World\” to print Hello “World”
Reading User Input
❑ read command takes input from the keyboard
❑ Syntax
❑ read variablename [input will be saved in variablename]
❑ Using the taken input
❑ Use the variablename with $ sign like $variablename
❑ Example: The entered value is $variablename
❑ Multiple values input
❑ read variablename1 variablename2 …
❑ While giving multiple input use space to separate don’t press enter
❑ Taking input in same line (not in next line) // p flag
❑ read –p “Enter Variable” variablename (read –p comment variablename)
❑ Taking silent input like password
❑ read –s “Enter Variable” variablename [silent in new line] ///
❑ read –sp “Enter Variable” variablename [silent in same line]
Operators
❑ Arithmetic Operator
❑ BASH don’t have any mechanism to perform arithmetic operations
❑ It uses expr [external program] to perform
❑ expr only performs integer operations
❑ Floating value calculations are discussed later
****Most Important Things to Remember****
❑ There must be spaces between operators and expressions
2+2 is not correct it should be 2 + 2
❑ It should be written like `expr 2 + 2` or $(expr 2 + 2)
❑ ` This symbol is called backtick
Some Examples
❑ Assuming a=20 b=10
❑ Addition will be sum=$(expr $a + $b) or
sum=`expr $a + $b`
❑ Subtraction will be sub=$(expr $a - $b) or
sub=`expr $a - $b`
❑ Multiplication will be multi=$(expr $a \* $b) or
multi=`expr $a \* $b`
❑ Division will be div=$(expr $a / $b) or
div=`expr $a / $b`
❑ Modulus will be mod=$(expr $a % $b) or
mod=`expr $a % $b`
Operators
❑ Relational Operators
❑ Bourne Shell supports the following relational operators that are specific to
numeric values.
❑ Following operators will not work for strings.
*All relational operators must be inside square braces with spaces around them*
[ $a == $b ] [CORRECT]
[$a== $b] [WRONG]
[$a==$b] [WRONG]
Operators
❑ Relational operators
❑ -eq or == to check equality of 2 number [ $a -eq $b ]
❑ -ne or != to check inequality of 2 number [ $a -ne $b ]
❑ -gt or > to check left operand is greater or not [ $a -gt $b ]
❑ -lt or < to check left operand is greater or not [ $a -lt $b ]
❑ -ge or >= to check left operand is greater or equal [ $a -ge $b ]
❑ -le or <= to check left operand is lesser or equal [ $a -le $b ]
Operators
❑ String Operators
❑ = checks the equality [ $a = $b ]
❑ != checks the inequality [ $a != $b ]
❑ -z Checks if the given string operand size is zero; if it is zero
length, then it returns true. [ -z $a ]
❑ // -n Checks if the given string operand size is non-zero; if it is
nonzero length, then it returns true. [ -n $a ]
Floating Point Calculation
❑ Floating points can not be evaluated with expr it can be calculated
with a bc utility
❑ num1=20.5
❑ num2=10
❑ echo “$num1+$num2” | bc
❑ For storing values into a variable
❑ num3=$(bc <<< “$num1+num2”)
*****Remember the spaces and \* will not be applicable here*****
It uses simple equation strategies
Books
❑ Unix Shell Programming
❑ Written by Yashavant P. Kanetkar