Full Transcript

**University of Science and Technology** **Faculty of Computer Science and Information Technology** **Open Source Operating Systems** **Lab (9) Date 11-05-2024** Unix / Linux - Shell Decision Making ==================================== **Description:** The students take from this exercise the...

**University of Science and Technology** **Faculty of Computer Science and Information Technology** **Open Source Operating Systems** **Lab (9) Date 11-05-2024** Unix / Linux - Shell Decision Making ==================================== **Description:** The students take from this exercise the various operators supported by the shell, and how to make use of conditional statements that allow program to make correct decisions and perform the right actions. **Lab Learning Outcomes:** - Shell operators - The two decision-making statements: - The **if\...else** statement - The **case\...esac** statement **Lab Instructions:** The lab instructor has to follow the steps below to demonstrate to the students the following:. 1. **Shell operators:** There are various operators supported by each shell. We will discuss in detail about Bourne shell (default shell).We will discuss the following operators − - Arithmetic Operators - Relational Operators - Boolean Operators - String Operators - File Test Operators Bourne shell didn\'t originally have any mechanism to perform simple arithmetic operations but it uses external programs, either **awk** or **expr**. The following example shows how to add two numbers − \#!/bin/sh val=\`expr 2 + 2\` echo \"Total value : \$val\" The above script will generate the following result − **Total value : 4** 1. **Arithmetic Operators:** The following arithmetic operators are supported by Bourne Shell. Assume variable **a** holds 10 and variable **b** holds 20 then − **[Show Examples](https://www.tutorialspoint.com/unix/unix-arithmetic-operators.htm):** **Operator** **Description** **Example** --------------------- ----------------------------------------------------------------------- ---------------------------------------- \+ (Addition) Adds values on either side of the operator \`expr \$a + \$b\` will give 30 \- (Subtraction) Subtracts right hand operand from left hand operand \`expr \$a - \$b\` will give -10 \* (Multiplication) Multiplies values on either side of the operator \`expr \$a \\\* \$b\` will give 200 / (Division) Divides left hand operand by right hand operand \`expr \$b / \$a\` will give 2 \% (Modulus) Divides left hand operand by right hand operand and returns remainder \`expr \$b % \$a\` will give 0 = (Assignment) Assigns right operand in left operand a = \$b would assign value of b into a == (Equality) Compares two numbers, if both are same then returns true. \[ \$a == \$b \] would return false. != (Not Equality) Compares two numbers, if both are different then returns true. \[ \$a != \$b \] would return true. It is very important to understand that all the conditional expressions should be inside square braces with spaces around them, for example **\[ \$a == \$b \]** is correct whereas, **\[\$a==\$b\]** is incorrect. All the arithmetical calculations are done using long integers. 2. **Relational Operators:** Bourne Shell supports the following relational operators that are specific to numeric values. These operators do not work for string values unless their value is numeric. For example, following operators will work to check a relation between 10 and 20 as well as in between \"10\" and \"20\" but not in between \"ten\" and \"twenty\". Assume variable **a** holds 10 and variable **b** holds 20 then − **[Show Examples](https://www.tutorialspoint.com/unix/unix-relational-operators.htm):** **Operator** **Description** **Example** -------------- -------------------------------------------------------------------------------------------------------------------------------------- -------------------------------- **-eq** Checks if the value of two operands are equal or not; if yes, then the condition becomes true. \[ \$a -eq \$b \] is not true. **-ne** Checks if the value of two operands are equal or not; if values are not equal, then the condition becomes true. \[ \$a -ne \$b \] is true. **-gt** Checks if the value of left operand is greater than the value of right operand; if yes, then the condition becomes true. \[ \$a -gt \$b \] is not true. **-lt** Checks if the value of left operand is less than the value of right operand; if yes, then the condition becomes true. \[ \$a -lt \$b \] is true. **-ge** Checks if the value of left operand is greater than or equal to the value of right operand; if yes, then the condition becomes true. \[ \$a -ge \$b \] is not true. **-le** Checks if the value of left operand is less than or equal to the value of right operand; if yes, then the condition becomes true. \[ \$a -le \$b \] is true. It is very important to understand that all the conditional expressions should be placed inside square braces with spaces around them. For example, **\[ \$a \

Use Quizgecko on...
Browser
Browser