0 ratings0% found this document useful (0 votes) 56 views7 pagesOS Lab. Exp.2
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content,
claim it here.
Available Formats
Download as PDF or read online on Scribd
Experiment 2
Shell Basic Operators and expressions
Objective
* Learning Shell basic operators
* Learning Shell Expressions for arithmetic and logical operations
Learning the assignment and read Statements
Introduction
Each shell supports various basic operations. There are five basic operations that one
must know to use the bash shell:
* Arithmetic Operators, such as +, -, *, /, % (mod)
+ Relational Operators, such as == for (equal), != for (not equal), >,
* Boolean Operators, such as:
1: for not
-0 (or | |): for OR-ing operation
-a (or &&): for AND-ing operation
* Bitwise Operators: These operators are used to perform bitwise operations (i.e.
perform bit by bit logical operations. Such as File Test Operators.
Shell Expressions
The expr command in Unix evaluates a given expression and displays its corresponding
output. It is used for:
~ Basic operations like addition, subtraction, multiplication, division, and modulus
on integers.
- Evaluating regular expressions, string operations like substring, length of strings
etc.
Syntax: expr expression
Where expression is equivalent to : op.1 operator op.2
a) Using expr for basic arithmetic operations
Table (1): Mathematical Operators with Integers
Operator] Description | Example Using echo statement Example Using echo
statement
+ Addition expr 11+ 3 echo “11+3"
- Subtraction expr 18 - 6 echo” 18-6”
=| Multiplication | expr 7 *3 echo “7 #3"
7 Division expr 17/3 echo “17/3 *
% Modulus ~_expr 17 % 3 echo “17%3"
++ | Exponentiation expr 3 "4 echo "17%3
1 "OSLab. Exp.2/ Prepared by Dr. Fatemah K. Al-Assfor & Or. Atheel Kadhum
2023Examples: print the following expressions to find their results:
expr 1+3
whats the difference between expr 1 + 3 and expr 1437
expr 2-1.
expr 10/2
expr 20% 3
Example: Perform the following example
a=4
b=5,
echo "a + b=${(a+b))"
echo "a - b = $((a-b))"
echo “a * b=$((a *b))”
echo "a/b = $((a/b))"
echo "a % b = ${(a % b))"
echo "++a = $((++a))"
echo "~-b = ${(--b))"
Example: Perform the following examples
echo"expr 3+ 11" —# It will printexpr3 + 11
echo'expr 3411’ —# It will print expr 3+ 11
echo ‘expr 3+ 11° # It will print the result 14
Note: The Back quote * * are used with the expr to find the results of an expression. The Back
quote is generally found on the key under tilde (~) of PC keyboard (above the TAB key).
Applications of expr
1. Find Matching (For String operations)
The (expr) can be used to find the partial matching and displays the number of
characters matched; note that it is work only from starting letter.
Example 1: length of a string
What is the result of the following condition?
2 OS Lab. Exp.2/ Prepared by Dr. Fatemah K. ALAssfor & Dr. Atheel Kadhum
20232. Find Substring
The (expr) can also be used to extract a portion of the string using the substr function.
The syntax of substr function is
Syntax: substr string position length
The position represents the character position in the string.
The length represents the number of characters to extract from the main string.
Example 1: Finding length of a string
expr substr Deep_learning 6 7. #Itis work only if there is no space key
learning
expr substr "My name is ROMY" 12 4
ROMY
3. Index of the substring
The position of a string in the main string can be found using the index function. The
syntax of index function is shown below:
Syntax : index string characters
If the characters string is found in the main string, then the index function displays the
position of the characters. Else, it displays 0. But, sometimes the result is O, in other versions
of ubuntu.
Example 1: Finding length of a substring:
expr index Computer_Engineering Department Engineer
10
Example 2: Finding length of a string
b="expr index LinuxHint t
echo $b
9
Example 3: To trim the substring from a string, select a substring beginning and end. For
instance, the following example, we try to trim the 2" word. Thus, the 6" character Is the
beginning of the substring, and the ending is the 10" character.
3 ‘OS Lab. Exp.2/ Prepared by Or. Fatemah K. Al-Assfor & Dr. Atheel KadhumThe Assignment Statement
The assignment statement can be used to perform basic operations directly. While
using let, we use variable names without the $ prefix .
Note: For basic arithmetic operators like, addition (+), subtraction (-), multiplication (*), division
(/), and modulus (%) ; these operators can be used in the expression with the let command.
Increment & Decrement Operations:
a) Post-increment(var++) / Post-decrement(var-) operator: This operator is used to
interpret the integer value then increase/decrease the integer variable by 1.
Increment Syntax let variable++
Decrement Syntax: _ let Variable- -
Example: Perform the following example
let “varl =2" “var2=1" “var3=varl+ var2"; echo $var3
3
let "a =25" "b=4" "c=a/b"; echo $c
6
let "x=4" "xe+" 5 echo $x
5
Example: Find the result of the following expression:
let "x=4" "y=xt++" ; echo $x $y
b) Pre-increment (++var) / Pre-decrement (-var) operator: This operator
increases/decreases the integer value by 1 and then interpret the integer variable.
Example: Perform the following example
let "z1=10" "z2=--21"; echo $21 $22
99
let "23-10" "z3--"; echo $23
Example: Find the result of the following expression:
Jet “zi=10" "z2=21--"j echo $21 $22
ind the result
4 (OS Lab. Exp.2/ Prepared by Or. Fatemah K.
2023x12
x46
echo $x
let x-
let x--
echo $x
Alternative Methods for Expressions:
a) Using the square brackets [ ] with the assignment statement to find the result of
an operation as follows:
Syntax
let var1 = $[ var2 operator var3 }
let r1=$[ $no1/3 ]
echo $r1
let r2=S[ $x%3 |
echo $12
a) Using the brackets (( )) with the assignment statement to find the result of an
operation as follows:
b)
Syntax let var1 = $(( var2 operator var3 ))
Example:
let r1=$(( $no1-3 })
echo $r1.
let r2=S(( $x /3 })
echo $12
Base Conversion be
The arithmetic operations discussed previously do not support floating-point values. To
find a floating-point result, the be can be used to find exact result as a floating point during
the mathematical operations. be has a wide range of options to perform floating point
‘operations.
Examples
- Using bc to find floating point result:
‘Assfor & Or. Atheel Kadhum
5 ” (OS Lab. €xp.2/ Prepared by‘echo "4 * 0.56" | be
2.24
echo "$no + 1.2525" | be’
echo $result
55.2525
- Decimal places scale with be:
Example: For the following example the scale=3; i.e. parameter sets the number of
decimal places to 3. Hence, the output of bc will be a number with 3 decimal places:
- Base conversion with be:
To convert from one base number system to another one, select the base of
conversion, then use be to convert the number to this system.
Example: Convert the number 8910 to binary, and vice versa.
x=89
echo "obase=2;$no" | be
1011001
no=1011001
echo "obase=10;iba!
89
Example: Calculating squares roots:
echo "sqrt(60)" | be
echo "sqrt(100)" | be #Square root
Bitwise shift left / Bitwise shift right:
This operator is used to shift the order of the bits either to the left or
let "m= 5 << 2"5 echo $m
20
Read Statement
The read statement is useful in scripts when reading or asking an input from user. This
read statement is used when a script needs to interact with a user for inputs to continue
the script. Read statement is used to input data from user.
6 (05 Lab. Exp.2/ Prepared by Or. Fatemah K. AL-Assfor & Or. Atheel Kadhum
2023,Syntax
read variable variable2 ... variable n
Example: Read a value from user input.
read ab # Input two values one for a and one for b then press enter to continue
49
echo “$a + $b*
Example: Read a value (2) and gives some info to what he has to give. For displaying some
information use -p option when reading value.
read -p "Please enter a number: "Z
please enter anumber: 25 #write7
echo $Z
echo "sart($Z)" | be #Square root
Exercise:
1-Perform all the examples, and write their results.
2- If and Y=9, Find the exact result for the following expressions:
«© Xmod Y-5
© 2x4 VV /2
© 2 /(%-2)>
3- Convert (227):0 to ( )2
4- Convert (11010101)2 to{ )s
5- Convert (195):0 to( )2to( his
6- Read three integer numbers then print the average result in real format with three
fraction positions.
7- Read two integer numbers a and b, then, Use the bitwise shift operations to
perform the following expression and find the exact result:
Z = (8a + 4b)/3
7 ” ‘OS Lab, Exp 2/ Prepared by Dr. Fatemah K. ArAssfor & Or, Atheel Kadhurn
2023