Friday, November 22, 2013

Selection And Repetition Part I

In this part, we will learn how to use selection and repetition command which every programming language will used to execute a statement with certain conditional and repeat a few statement with a short code script.

7.1 test dan operator

test is sh shell utility that used to check information about a file and used to compare both string or numeric.
syntax: test expresion
test work proses is return the status information with 0 value (correct) or 1 (false) where this value status can be read by special variable $.
[elvin@TestBed$]test 7 -gt 3
[elvin@TestBed$]echo $?
0
This statement 7 -gt (greather than) 3 is being evaluated test return 0 for variable status $. 
This mean this statement is true.
if we compare with below expresion
[elvin@TestBed$]test 3 -lt 1
[elvin@TestBed$]echo $?
1
 this status value is 1, which mean false statement
if you see the symbol -gt and -lt, this want mean operator.
simple operator is special character that do operation for a few operand, example 2+3.
"+" is an operator. 2 and 3 is an operand. 
for test example, the operator is -lt and -gt, and the number is on left and the operand is on the right. 
Below is the operator that bash can be used:
OperatorInformation
bil1 -eq bil2return true if bil1 is same as bil2
bil1 -ne bil2return true if bil1 is not same as bil2
bil1 -lt bil2return true if bil1 is less than bil2
bil1 -le bil2return true if bil1 is less than enough bil2
bil1 -gt bil2return true if bil1 is greater than bil2
bil1 -ge bil2return true if bil1 is greater than enough bil2

7.1.2. Operasi string

OperatorInformation
-z STRINGreturn true if STRING length is zero
STRING1 == STRING2return true if STRING1 is same as STRING2

7.1.3 Operator file

OperatorInformation
-f FILEreturn true if FILE is exist and is a normal file
-d FILEreturn true is FILE is exist and is a directory

7.1.4 Operator logic

ekspr1 -o ekspr2return true is one of the expression is true  (or,||)
ekspr1 -a ekspr2return true is expression 1 and expression 2 true(and,&&)
! ekspresireturn true if the expression is false (not!)

0 comments:

Post a Comment