You are currently viewing Bash array and  Operator in shell script

Bash array and Operator in shell script

Bash array and Operator in shell script 

Bash array : As we know that shell variable can store a single value, but array can store multiple value at same time.

syntax

NameOfArray=(value1 … valuen)

#!/bin/bash
Cources=('ccna' 'ccnp' 'linux')
echo "First Value: ${Cources[0]}"
echo "Second Value: ${Cources[1]}"
echo "All: ${Cources[*]}"

Output

First Value: ccna
Second Value: ccnp
All: ccna ccnp linux

echo ${Cources[0]} # Element #0
echo ${Cources[@]} # All elements, space-separated
echo ${#Cources[@]} # Number of elements
echo ${#Cources} # String length of the 1st element
echo ${#Cources[3]} # String length of the Nth element
echo ${Cources[@]:3:2} # Range (from position 3, length 2)

Another way is

for i in "${Cources[@]}"; do
echo $i
done

Output

ccna
ccnp
linux

Operators in Bash script or shell script

We can perform arithmetic operation in bash script by sing some arithmetic operator

Arithmetic Operators

+ (Addition)
(Subtraction)
* (Multiplication)
/ (Division)
% (Modulus)
= (Assignment)
== (Equality)
!= (Not Equality)

#!/bin/sh
a=10
b=20
val=`expr $a + $b`
echo "a + b : $val"
if [ $a == $b ]
then
echo "a is equal to b"
fi
if [ $a != $b ]
then
echo "a is not equal to b"
fi

Output

a + b : 30
a is not equal to b

Relational Operators

-eq equal
-ne not equal
-gt greater than
-lt less than
-ge greater than and equal
-le less than or equal

#!/bin/sh
a=10
b=20
if [ $a -eq $b ]
then
echo " a is equal to b"
else
echo " a is not equal to b"
fi

Output

a is not equal to b

Note: Expressions should be inside square braces with spaces around them.

Example [ $a <= $b ] is correct and [$a <= $b] is incorrect. we will look example on later chapter.

Boolean Operators

! logical negation
-o logical OR
-a logical AND

#!/bin/sh

a=10
b=20

if [ $a != $b ]
then
echo "a is not equal to b"
else
echo " a is equal to b"
fi

if [ $a -lt 100 -a $b -gt 15 ]
then
echo "returns true"
else
echo "returns false"
fi

Output

a is not equal to b
returns true

String Operators

= equal
!= not equal
-z operand size is zero
-n size is non zero
str str is not the empty string

#!/bin/sh

a="ken"
b="corner"

if [ $a = $b ]
then
echo "a is equal to b"
else
echo "a is not equal to b"
fi


if [ -z $a ]
then
echo "string length is zero"
else
echo "string length is not zero"
fi


if [ $a ]
then
echo "string is not empty"
else
echo "string is empty"
fi

Output

a is not equal to b
string length is not zero
string is not empty

File Operators

-b file is block special file
-c file is character special file
-d file is directory
-f file is ordinary file
-g file is set group ID (SGID) bit set;
-k file has its sticky bit set;
-p file is named pipe;
-t file is associated with a terminal;
-u file is Set User ID (SUID) bit set;
-r file is readable;
-w file is writable;
-x file is executable;
-s file has size greater than 0;
-e file exists;

#!/bin/sh

file="/home/file.sh"


if [ -d $file ]
then
echo "File is a directory"
else
echo "This is not a directory"
fi

if [ -s $file ]
then
echo "File size is zero"
else
echo "File size is not zero"
fi

if [ -e $file ]
then
echo "File exists"
else
echo "File does not exist"
fi

Output

This is not a directory
File size is not zero
File does not exist

Bonus Script

Suppose i Have one Ubuntu server with two instance of tomcat ( Java) now whenever any of tomcat CPU usage goes to or equal to 99% then it must display a message in  screen.(remember you may save the output in any log file)

#!/bin/bash


TomcatPID=$(pidof java)
TomcatPID1=$(echo $TomcatPID | awk '{print $1}')
TomcatPID2=$(echo $TomcatPID | awk '{print $2}')


echo "Java1 $TomcatPID1"
echo "Java2 $TomcatPID2"


TomcatCPUtmp1=$(ps -p $TomcatPID1 -o %cpu)
TomcatCPU1=${TomcatCPUtmp1:5}
TomcatCPU1Default=${TomcatCPU1:=0}


TomcatCPUtmp2=$(ps -p $TomcatPID2 -o %cpu)
TomcatCPU2=${TomcatCPUtmp2:5}
TomcatCPU2Default=${TomcatCPU2:=0}

echo "Cpu1 $TomcatCPU1Default"
echo "Cpu2 $TomcatCPU2Default"


if [ "${TomcatCPU1Default/\.*}" -ge 99 ] || [ "${TomcatCPU2Default/\.*}" -ge 99 ];
then
echo "Tomcat CPU usage is equal to greater then 99%"
else
echo "OK"
fi

 

understanding the script

pidof java will give pid of both tomcat

echo $TomcatPID | awk ‘{print $1}’  getting pid of tomcat 1

“${TomcatCPU1Default/\.*}” -ge 99   converting float value into decimal so that it will compare easily.


You May Also Enjoy Reading This …

Leave a Reply