Open navigation menu
Close suggestions
Search
Search
en
Change Language
Upload
Sign in
Sign in
Download free for days
0 ratings
0% found this document useful (0 votes)
46 views
7 pages
OS Lab. Exp.2
Uploaded by
Xtr Gbu
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
Download
Save
Save OS Lab. Exp.2 For Later
0%
0% found this document useful, undefined
0%
, undefined
Embed
Share
Print
Report
0 ratings
0% found this document useful (0 votes)
46 views
7 pages
OS Lab. Exp.2
Uploaded by
Xtr Gbu
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
Carousel Previous
Carousel Next
Download
Save
Save OS Lab. Exp.2 For Later
0%
0% found this document useful, undefined
0%
, undefined
Embed
Share
Print
Report
Download now
Download
You are on page 1
/ 7
Search
Fullscreen
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
You might also like
Shell Programming
PDF
No ratings yet
Shell Programming
21 pages
FALLSEM2019-20 CSE2005 ELA VL2019201002569 Reference Material I 25-Jul-2019 Shell Programming
PDF
No ratings yet
FALLSEM2019-20 CSE2005 ELA VL2019201002569 Reference Material I 25-Jul-2019 Shell Programming
27 pages
Shell Scripting
PDF
No ratings yet
Shell Scripting
27 pages
Shell Programming
PDF
No ratings yet
Shell Programming
29 pages
Shell Scripting
PDF
No ratings yet
Shell Scripting
23 pages
Unit 2
PDF
No ratings yet
Unit 2
83 pages
Shell Unit 3
PDF
No ratings yet
Shell Unit 3
42 pages
Unit-II DevOps - Shell Scripting
PDF
No ratings yet
Unit-II DevOps - Shell Scripting
83 pages
Shell
PDF
No ratings yet
Shell
50 pages
Unix Program
PDF
100% (1)
Unix Program
49 pages
Os Lab 05
PDF
No ratings yet
Os Lab 05
36 pages
Linux Prac
PDF
No ratings yet
Linux Prac
60 pages
VTA Training Course: Topics
PDF
No ratings yet
VTA Training Course: Topics
25 pages
Os Record Exp2-6 Senior
PDF
No ratings yet
Os Record Exp2-6 Senior
48 pages
Os Record Exp2-6 Mythra
PDF
No ratings yet
Os Record Exp2-6 Mythra
46 pages
Shell OSL Lab
PDF
No ratings yet
Shell OSL Lab
43 pages
Arithmetic in Bash PDF
PDF
No ratings yet
Arithmetic in Bash PDF
27 pages
Spring 23-24 Os Lab 8
PDF
No ratings yet
Spring 23-24 Os Lab 8
16 pages
LSP Lab Solutions
PDF
No ratings yet
LSP Lab Solutions
36 pages
Shellprog
PDF
No ratings yet
Shellprog
23 pages
Chapter 9. Shell Scripting Basics
PDF
No ratings yet
Chapter 9. Shell Scripting Basics
28 pages
Default Folder
PDF
No ratings yet
Default Folder
22 pages
122-Linux Shell Scripting
PDF
No ratings yet
122-Linux Shell Scripting
30 pages
Unix Het
PDF
No ratings yet
Unix Het
18 pages
Laboratory Lecture 3
PDF
No ratings yet
Laboratory Lecture 3
26 pages
Operating System Part3
PDF
No ratings yet
Operating System Part3
28 pages
OS Lab Ex 1
PDF
No ratings yet
OS Lab Ex 1
12 pages
Lab 3 07032023 112254am 11102023 090408am
PDF
No ratings yet
Lab 3 07032023 112254am 11102023 090408am
11 pages
Bash Data Handling: COMP2101 Winter 2019
PDF
No ratings yet
Bash Data Handling: COMP2101 Winter 2019
25 pages
Basic Operators in Shell Scripting
PDF
No ratings yet
Basic Operators in Shell Scripting
14 pages
06 Linux Shell Programming
PDF
No ratings yet
06 Linux Shell Programming
59 pages
Shell Programs
PDF
No ratings yet
Shell Programs
25 pages
Shell Scripting: Presented by - Mukesh Halwan Premkanth Mengani
PDF
No ratings yet
Shell Scripting: Presented by - Mukesh Halwan Premkanth Mengani
25 pages
135 - Shel Scripting
PDF
No ratings yet
135 - Shel Scripting
8 pages
Bash Scripting
PDF
No ratings yet
Bash Scripting
13 pages
ZSH 11 Arithmetic Evaluation
PDF
No ratings yet
ZSH 11 Arithmetic Evaluation
6 pages
Script.: Shell Scripts
PDF
No ratings yet
Script.: Shell Scripts
20 pages
Unix Ques 3
PDF
No ratings yet
Unix Ques 3
11 pages
Shell 1.1 Shell Scripts:: Script
PDF
No ratings yet
Shell 1.1 Shell Scripts:: Script
19 pages
Shellscripts Sujata
PDF
No ratings yet
Shellscripts Sujata
14 pages
5 Useful Ways To Do Arithmetic in Linux Terminal
PDF
No ratings yet
5 Useful Ways To Do Arithmetic in Linux Terminal
5 pages
Shell Scripting If Else
PDF
No ratings yet
Shell Scripting If Else
9 pages
L1 Unix General Purpose Commands
PDF
No ratings yet
L1 Unix General Purpose Commands
8 pages
Experiment No. 09: Echo "Enter Some Text " Read Text Echo "You Entered: $text"
PDF
No ratings yet
Experiment No. 09: Echo "Enter Some Text " Read Text Echo "You Entered: $text"
11 pages
Arithmetic Operators: #!/bin/sh
PDF
No ratings yet
Arithmetic Operators: #!/bin/sh
2 pages
Lab3 OSG
PDF
No ratings yet
Lab3 OSG
6 pages
Linux Expr Command Tutorial For Beginners With Examples
PDF
No ratings yet
Linux Expr Command Tutorial For Beginners With Examples
4 pages
Ass 03
PDF
No ratings yet
Ass 03
7 pages
Unix Basic Operators
PDF
No ratings yet
Unix Basic Operators
4 pages
CS1101: Lecture 9: The Shell As A Programming Language
PDF
No ratings yet
CS1101: Lecture 9: The Shell As A Programming Language
5 pages
Intro To Bash Scripting
PDF
No ratings yet
Intro To Bash Scripting
4 pages
Ex: 2 Shell Programming Objectives
PDF
No ratings yet
Ex: 2 Shell Programming Objectives
13 pages
Shell Programs
PDF
No ratings yet
Shell Programs
4 pages
Simple C Programs
PDF
No ratings yet
Simple C Programs
10 pages
Shell Scripting Examples
PDF
100% (1)
Shell Scripting Examples
19 pages
Lab Instructions: B. 11 Bourne Shell Scripts
PDF
No ratings yet
Lab Instructions: B. 11 Bourne Shell Scripts
3 pages
AWK Cheat Sheets: Command Short Description
PDF
100% (7)
AWK Cheat Sheets: Command Short Description
7 pages
Shell Programming
PDF
No ratings yet
Shell Programming
16 pages