Example1:
This shows the usage of single quote and double quote.
#!/bin/bash
test="bla bla bla bla";
echo 'I read a sentence like $test inside double quotes';
echo "I read a sentence like $test inside double quotes";
apple='$test'; #Single quotes takes it as a literals
banana=\$test; #Backslash does the same thing
MYIP=`ifconfig eth0 | grep inet | cut -d: -f2 | cut -d" " -f1`;
echo "My IP address is $MYIP";
Example2:
This shows the if condition operation.
#!/bin/bash
fruit1="apple";
fruit2="banana";
if [ $fruit1 != $fruit2 ]
then
echo "$fruit1 is not equal to $fruit2";
else
echo "$fruit1 is equal to $fruit2";
fi
# -n is for non empty & -z is for zero length
if [ -n $fruit1 ]
then
echo "Fruit1 is not the empty string";
else
echo "Fruit1 is an empty string";
fi
Example3:
#!/bin/bash
# -eq equal, -ne notequal
# -gt (greater than), -ge (greater than or = 2)
# -lt (less that), -le (less than or = 2)
num1=5;
num2=10;
num3=15;
if [ $num1 -ge $num2 ]
then
echo "$num1 is greater than or equal to $num2";
else
echo "$num1 is less than $num2";
fi
let num4=$num1+$num2;
if [ $num4 -eq $num3 ]
then
echo "$num4 is equal to $num3";
else
echo "$num4 is not equal to $num3";
fi
Example4:
#!/bin/bash
# -r is read permission set, -w for write, -x for execute
# -d (is the file a directory), -f for file, -s non-empty file
dir1=/home/bala;
file1=/tmp/script.sh;
if [ -d $file1 ]
then
echo "$file1 is a directory";
else
echo "$file1 is not a directory";
fi
if [ -x $file1 ]
then
echo "$file1 is executable";
else
echo "$file1 is not executable";
fi
if [ -r $dir1 -a -x $dir1 ]
then
echo "$file1 is readable N executable";
else
echo "$file1 is not both read & executable";
fi
Example5:
#!/bin/bash
#This script is going to loop into useraccount
passFile=/etc/passwd
userCount=0;
specialCount=0;
userIds=`cat $passFile | cut -d: -f3`;
echo $userIds;
for id in $userIds
do
if [ $id -ge 1000 ]
then
echo $id
#echo $userCount
let userCount=userCount+1;
#echo $userCount
else
#echo $specialCount
let specialCount=specialCount+1;
fi
done
echo "There are $userCount normal users and $specialCount special users on the system"
This shows the usage of single quote and double quote.
#!/bin/bash
test="bla bla bla bla";
echo 'I read a sentence like $test inside double quotes';
echo "I read a sentence like $test inside double quotes";
apple='$test'; #Single quotes takes it as a literals
banana=\$test; #Backslash does the same thing
MYIP=`ifconfig eth0 | grep inet | cut -d: -f2 | cut -d" " -f1`;
echo "My IP address is $MYIP";
Example2:
This shows the if condition operation.
#!/bin/bash
fruit1="apple";
fruit2="banana";
if [ $fruit1 != $fruit2 ]
then
echo "$fruit1 is not equal to $fruit2";
else
echo "$fruit1 is equal to $fruit2";
fi
# -n is for non empty & -z is for zero length
if [ -n $fruit1 ]
then
echo "Fruit1 is not the empty string";
else
echo "Fruit1 is an empty string";
fi
Example3:
#!/bin/bash
# -eq equal, -ne notequal
# -gt (greater than), -ge (greater than or = 2)
# -lt (less that), -le (less than or = 2)
num1=5;
num2=10;
num3=15;
if [ $num1 -ge $num2 ]
then
echo "$num1 is greater than or equal to $num2";
else
echo "$num1 is less than $num2";
fi
let num4=$num1+$num2;
if [ $num4 -eq $num3 ]
then
echo "$num4 is equal to $num3";
else
echo "$num4 is not equal to $num3";
fi
Example4:
#!/bin/bash
# -r is read permission set, -w for write, -x for execute
# -d (is the file a directory), -f for file, -s non-empty file
dir1=/home/bala;
file1=/tmp/script.sh;
if [ -d $file1 ]
then
echo "$file1 is a directory";
else
echo "$file1 is not a directory";
fi
if [ -x $file1 ]
then
echo "$file1 is executable";
else
echo "$file1 is not executable";
fi
if [ -r $dir1 -a -x $dir1 ]
then
echo "$file1 is readable N executable";
else
echo "$file1 is not both read & executable";
fi
Example5:
#!/bin/bash
#This script is going to loop into useraccount
passFile=/etc/passwd
userCount=0;
specialCount=0;
userIds=`cat $passFile | cut -d: -f3`;
echo $userIds;
for id in $userIds
do
if [ $id -ge 1000 ]
then
echo $id
#echo $userCount
let userCount=userCount+1;
#echo $userCount
else
#echo $specialCount
let specialCount=specialCount+1;
fi
done
echo "There are $userCount normal users and $specialCount special users on the system"