Shell Script to Add Two Integers Brief: This example will help you to understand to add two numbers in the bash script. This script takes the input of two numbers from the user and prints the sum of both numbers. This is an example script initializes two variables with numeric values. Then perform an addition operation on both values and store results in the third variable.
# Calculate the sum of two integers with pre initialize values
# in a shell script
sum=$(( $a + $b ))
Command Line Arguments In this second example, shell script reads two numbers as command line parameters and perform the addition operation.
# Calculate the sum via command line arguments
# $1 and $2 refers to the first and second argument passed as command line arguments
sum=$(( $1 + $2 ))
echo "Sum is: $sum"
Include Files in Bash Similar to other programming languages which allow to include other files to a file, Bash scripting also allows to include (source) another shell script file to script file. For example, to include config.sh script to current script file use following syntax, where config.sh is available in the same directory of the current script.
Include Shell Script in Other Shell Script For this example, First, I have created a data.sh file with the following content.
Now create another file show.sh, which includes data.sh file.
echo "Welcome $name"
echo "Your id is $id"
Bash String Comparisons Use double equals ( == ) operator to compare strings inside square brackets . Using this option you simply test if two given strings are equals or not inside bash shell scripts. Example Syntax: if [ “$str1” == “$str2” ] # True if equal if [ “$str1” != “$str2” ] # True if not equal Example: For example to compare two string are equal or not. if [ “hello” == “hello” ] ## True if [ “hello” == “hello1” ] ## False if [ “hello” != “hello”…