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)

Output

Another way is

Output

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)

Output

Relational Operators

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

Output

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

Output

String Operators

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

Output

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;

Output

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)

 

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

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