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


You May Also Enjoy Reading This …

Leave a Reply

Your email address will not be published. Required fields are marked *