135 -shel scripting
135 -shel scripting
135 -shel scripting
#!/bin/bash
greeting=Hello
name=Tux
Arithmetic Expressions
Below are the operators supported by bash for mathematical calculations:
OPERATOR USAGE
+ addition
- subtraction
* multiplication
/ division
** exponentiatio
n
% modulus
Example:
#!/bin/bash
var=$((3+9))
echo $var
example2
#!/bin/bash
#
#
clear
echo "Please calculate"
echo
read r
echo
var=$(($r))
echo "Answer is $var"
echo
Where scale defines the number of decimal places required in the output.
#!/bin/bash
var=$((a+b))
echo $var
Syntax:
if [ conditions ]
then
commands
fi
read x
read y
if [ $x -gt $y ]
then
echo X is greater than Y
elif [ $x -lt $y ]
then
echo X is less than Y
elif [ $x -eq $y ]
then
echo X is equal to Y
fi
Conditional Statements (Decision Making)
if...then...fi statements
if...then...else...fi statements
if..elif..else..fi
if..then..else..if..then..fi..fi.. (Nested Conditionals)
Syntax:
if [[ condition ]]
then
statement
elif [[ condition ]]; then
statement
else
do this by default
fi
To create meaningful comparisons, we can use AND -a and OR -o as well.
if [ $a -gt 40 -a $b -lt 6 ]
Example: Let's find the triangle type by reading the lengths of its sides.
read a
read b
read c
if [ $a == $b -a $b == $c -a $a == $c ]
then
echo EQUILATERAL
elif [ $a == $b -o $b == $c -o $a == $c ]
then
echo ISOSCELES
else
echo SCALENE
fi
#!/bin/bash
for i in {1..5}
do
echo $i
done
#!/bin/bash
While loop
While loops check for a condition and loop until the condition remains true. We need to provide a
counter statement that increments the counter to control loop execution.
In the example below, (( i += 1 )) is the counter statement that increments the value of i.
Example:
#!/bin/bash
i=1
while [[ $i -le 10 ]] ; do
echo "$i"
(( i += 1 ))
done
Reading files
We can read the file line by line and print the output on the screen.
#!/bin/bash
LINE=1
do
((LINE++))
Output:
If you need to include the output of a complex command in your script, you can write the statement
inside back ticks.
Syntax:
var= ` commands `
Example: Suppose we want to get the output of a list of mountpoints with tmpfs in their name. We
can craft a statement like this: df -h | grep tmpfs.
To include it in the bash script, we can enclose it in back ticks.
#!/bin/bash
echo $var
Output:
image-118
#!/bin/bash
for x in $@
do
done
image-155
How to Automate Scripts by Scheduling via cron Jobs
Cron is a job scheduling utility present in Unix like systems. You can schedule jobs to execute daily,
weekly, monthly or in a specific time of the day. Automation in Linux heavily relies on cron jobs.
* * * * * sh /path/to/script.sh
Using crontab
My scheduled scripts
My scheduled scripts
The find command helps to locate files based on certain patterns. As most of the scripts end with .sh,
we can use the find script like this:
Where,
. represents the current directory. You can change the path accordingly.
-type f indicates that the file type we are looking for is a text based file.
image-159
If you are interested to read about the find command in detail, check my