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.
| #!/bin/bash # Calculate the sum of two integers with pre initialize values # in a shell script a=10 b=20 sum=$(( $a + $b )) echo $sum |
Command Line Arguments In this second example, shell script reads two numbers as command line parameters and perform the addition operation.
| #!/bin/bash # 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" |
Output:…
Read More 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.
| #!/bin/bash source other-file.sh |
Include Shell Script in Other Shell Script For this example, First, I have created a data.sh file with the following content.
| #!/bin/bash id=100 name="TecAdmin" |
Now create another file show.sh, which includes data.sh file.
| #!/bin/bash source data.sh echo "Welcome $name" echo "Your id is $id" |
…
Read More 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”…
Read More