# 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"
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"
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.
Division can also be performed using expr
.
result=$(expr 15 / 3)
echo "15 / 3 equals $result"
For more complex mathematical calculations, the bc
command is a powerful tool. It supports floating-point arithmetic and mathematical functions.
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
echo "scale=2; 7 / 3" | bc
# Outputs: 2.33
The scale
parameter sets the number of decimal places.
Linux provides commands to perform numeric operations on data within files.
# Sum the values in the second column of a space-separated file
awk '{sum += $2} END {print sum}' data.txt
# 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.
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.
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
#!/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
#!/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
#!/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
#!/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
- 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.