Skip to content

Latest commit

 

History

History
199 lines (135 loc) · 4.49 KB

01.if_elif_else_statements.md

File metadata and controls

199 lines (135 loc) · 4.49 KB

if-elif-else Statements Linux:

# Linux Shell Scripting Tutorial

## Numeric Operations in Linux

In Linux, the command-line interface provides powerful tools for performing numeric operations. Whether you need to perform basic arithmetic, more advanced calculations, or manipulate numerical data in files, Linux has the tools to get the job done.

### 1. Basic Arithmetic Operations

#### 1.1. Addition

To add two numbers, you can use the `expr` command or the `$(( ))` construct.

```bash
result=$(expr 5 + 3)
echo "5 + 3 equals $result"
# or
result=$((5 + 3))
echo "5 + 3 equals $result"

1.2. Subtraction

Similarly, subtraction can be done using expr or $(( )).

result=$(expr 8 - 2)
echo "8 - 2 equals $result"
# or
result=$((8 - 2))
echo "8 - 2 equals $result"

1.3. Multiplication

Multiplication is achieved using the expr command.

result=$(expr 4 \* 6)
echo "4 * 6 equals $result"

Note: The * needs to be escaped with a backslash (\) to prevent it from being interpreted as a wildcard character.

1.4. Division

Division can also be performed using expr.

result=$(expr 15 / 3)
echo "15 / 3 equals $result"

2. Advanced Arithmetic with bc

For more complex mathematical calculations, the bc command is a powerful tool. It supports floating-point arithmetic and mathematical functions.

2.1. Installation

If not installed, you can install bc using the package manager for your distribution.

# For Debian/Ubuntu
sudo apt-get install bc

# For Red Hat/Fedora
sudo dnf install bc

2.2. Example Usage

echo "scale=2; 7 / 3" | bc
# Outputs: 2.33

The scale parameter sets the number of decimal places.

3. Numeric Manipulation in Files

Linux provides commands to perform numeric operations on data within files.

3.1. awk for Numeric Operations in Files

# Sum the values in the second column of a space-separated file
awk '{sum += $2} END {print sum}' data.txt

3.2. sed for Basic Arithmetic in Files

# Multiply each number in a file by 2
sed 's/[0-9]\+/\n&\n/2;s/\n//;s/\([0-9]\+\)\(.*\)\([0-9]\+\)/\1*\3/' data.txt

Adjust the commands according to your specific needs and file formats.

Conditional Statements in Linux Shell Scripting

In Linux shell scripting, if-elif-else statements are used to make decisions based on certain conditions. These statements allow you to control the flow of your script based on whether a given condition is true or false.

1. Basic Syntax

The basic syntax of an if-elif-else statement in bash is as follows:

if [ condition ]; then
    # code to be executed if the condition is true
elif [ another_condition ]; then
    # code to be executed if the another_condition is true
else
    # code to be executed if none of the conditions are true
fi

2. Examples

2.1. Simple Numeric Comparison

#!/bin/bash

number=5

if [ $number -eq 5 ]; then
    echo "The number is 5."
elif [ $number -gt 5 ]; then
    echo "The number is greater than 5."
else
    echo "The number is less than 5."
fi

2.2. String Comparison

#!/bin/bash

word="Linux"

if [ "$word" == "Linux" ]; then
    echo "The word is Linux."
elif [ "$word" == "Unix" ]; then
    echo "The word is Unix."
else
    echo "The word is neither Linux nor Unix."
fi

2.3. File Existence Check

#!/bin/bash

file_path="/path/to/somefile.txt"

if [ -e "$file_path" ]; then
    echo "The file exists."
else
    echo "The file does not exist."
fi

2.4. Logical AND and OR

#!/bin/bash

age=25

if [ $age -ge 18 ] && [ $age -le 30 ]; then
    echo "You are between 18 and 30 years old."
elif [ $age -lt 18 ] || [ $age -gt 30 ]; then
    echo "You are either under 18 or over 30 years old."
else
    echo "Invalid age."
fi

3. Notes

  • Ensure proper spacing and quoting in conditions to avoid syntax errors.
  • Use appropriate comparison operators (-eq, -ne, -lt, -le, -gt, -ge) for numeric comparisons.
  • For string comparisons, use ==.
  • File-related conditions, such as -e for existence, can be useful in script logic.

These examples demonstrate the basic usage of if-elif-else statements in Linux shell scripting, along with numeric operations. You can modify and expand upon these templates based on your specific requirements.