To define an array value:
The difference between an array variable and a scalar variable can be explained as follows.
Say that you are trying to show the names of various students as variable sets. Each individual variable is a scalar variable, as follows:
name01= "Zara"
Name02= "Qadir"
name03= "Mahnaz"
name04= "Ayan"
name05= "Daisy"
We can use a single array to store all the names mentioned above. Here is the simplest way to create an array variable to assign a value to one of its indexes. This is the representation, as follows:
Array_name[index]=value
Array_Name is the array name, the index is in the array, you want to set the item index, the value is the value you want to set the item.
As an example, the following command:
name[0]= "Zara"
Name[1]= "Qadir"
name[2]= "Mahnaz www.yiibai.com"
name[3]= "Ayan"
name[4]= "Daisy"
If you are using the syntax of the Ksh shell to initialize the array here:
Set-a array_name value1 value2 ... Valuen
If you are using the bash shell, here is the syntax for initializing the array:
Array_name= (value1 ... valuen)
To access array values:
When you set any array variable, and you can access it, as follows:
${array_name[index]}
Here, Array_Name is the name of the array, and index is the value that the index accesses. The following is a simple example:
#!/bin/sh
name[0]= "Zara"
Name[1]= "Qadir"
name[2]= "Mahnaz"
name[3]= "Ayan"
name[4]= "Daisy"
echo "A: ${name[0]}"
echo "Second Index: ${name[1]}"
This would producefollowing result:
$./test.sh
The Index:zara
Second Index:qadir
You can access all the items in the array in one of the following ways:
${array_name[*]}
${array_name[@]}
Array_Name is the name of the array you are interested in the following is the simplest example:
#!/bin/sh
name[0]= "Zara"
Name[1]= "Qadir"
name[2]= "Mahnaz"
name[3]= "Ayan"
name[4]= "Daisy"
echo "A: ${name[*]}"
echo "Second method: ${name[@]}"
This will produce the following results:
$./test.sh
Method:zara Qadir Mahnaz Ayan Daisy
Second Method:zara Qadir Mahnaz Ayan Daisy
There are various operator shells that are supported. This tutorial is based on the default shell (Bourne), so we want to cover all the important Bourne shell operators.
There are the following operators that we are going to discuss:
· The arithmetic operator.
· The relational operator.
· Boolean operator.
· The string operator.
· File Test action.
The Bourne shell did not initially have any mechanism to perform simple arithmetic, but it uses external programs, either awk or must be simple to program expr.
Here is a simple example of adding two numbers:
#!/bin/sh
Val= ' Expr 2 + 2 '
echo "Total value: $val"
This will produce the following results:
Total Value:4
Write down the following points:
· There must be a space between the operator and the expression, for example, 2+2 is incorrect because it should be written as 2 + 2.
· ", called an inverted comma, should contain a complete expression. Arithmetic operators:
Arithmetic operators have the following Bourne shell support.
Assuming variable a=10, variable b=20:
Examples of arithmetic operators
Operator
Describe
Example
+
Addition-adds values on either side of the operator
' Expr $a + $b ' would give 30
-
Subtraction-subtracts right hand operand to left hand operand
' Expr $a-$b ' would give-10
*
Multiplication-multiplies values on either side of the operator
' Expr $a * $b ' give 200
/
Division-divides left hand operand from right hand operand
' Expr $b/$a ' would give 2
%
Modulus-divides left hand operand from right hand operand and returns remainder
' Expr $b% $a ' would give 0
=
Assignment-assign right operand into left operand
A= $b would assign value of B into a
==
Equality-compares two numbers, if both are same then true.
[$a = = $b] would return false.
!=
Not equality-compares two numbers, if both are different then true.
[$a!= $b] would return true.
It is very important to note here that all the conditional will be placed in square brackets, they have a space around them, such as [$a = = $b] is correct, for [$a = = $b] is incorrect.
All arithmetic calculations, using long integers. Relational operators:
Bourne shell support, the specific numeric value of the relational operator. These operators cannot use string values unless their values are numbers.
For example, operators will try to check the relationship between 10 and 20, as well as between "10" and "20", but not "10" and "21".
Assuming variable a=10, variable b=20:
Relational operators
Operator
Describe
Example
-eq
Checks if the value of two operands are equal or not, if yes then condition true.
[$a-eq $b] is not true.
-ne
Checks if the value of two operands are equal or not, if values are not equal then condition true.
[$a-ne $b] is true.
-gt
Checks if the value of the left operand was greater than the value of right operand, if yes then condition true.
[$a-gt $b] is not true.
-lt
Checks if the value of the left operand was less than the value of right operand, if yes then condition true.
[$a-lt $b] is true.
-ge
Checks if the value of the left operand was greater than or equal to the "right" operand, if yes then condition True.
[$a-ge $b] is not true.
-le
Checks if the value of the left operand was less than or equal to the "right" operand, if yes then condition becomes E.
[$a-le $b] is true.
It is important to note that all of the conditional expressions are enclosed in square brackets and that they have a space around them, which is essential, such as [$a <= $b] is correct, [$a <= $b] is incorrect. Boolean operations:
Boolean operators are supported by the following Bourne shell.
Suppose the variable is a variable b=10, and then the variable b=20:
Boolean Operations Example
Operator
Describe
Example
!
This is logical negation. This inverts a true condition to false and vice versa.
[! false] is true.
-O
This is logical OR. If one of the operands is true then condition would to be true.
[$a-lt 20-o $b-gt] is true.
-A
This is logical AND. If Both the operands are true then condition would is true otherwise it would be false.
[$a-lt 20-a $b-GT] is false. String operators:
The following string operations are supported by the Bourne Shell.
Suppose the variable a= "ABC" and the Variable b= "EFG":
Relational Operations Examples
Operator
Describe
Example
=
Checks if the value of two operands are equal or not, if yes then condition true.
[$a = $b] is not true.
!=
Checks if the value of two operands are equal or not, if values are not equal then condition true.
[$a!= $b] is true.
-Z
Checks if the given string operand size is zero. If It is zero length then it returns TRUE.
[-Z $a] is not true.
-N
Checks if the given string operand the size is non-zero. If It is Non-zero length then it returns TRUE.
[-Z $a] is not false.
Str
Check If STR is not the empty string. If It is empty then it returns false.
[$a] is not false. File Test actions:
The following are the various properties associated with the Operation testing UNIX files.
Suppose a variable file holds the existing file name "test" with a size of 100 bytes, read, write, and Execute permissions:
File Test action Example
Operator
Describe
Example
-B File
Checks if file is a block special file if yes then condition becomes true.
[-B $file] is false.
-C file
Checks if file is a character special file if yes then condition becomes true.
[-B $file] is false.
-D File
Check if file is a directory if yes then condition becomes true.
[-D $file] is not true.
-F File
Check if file is a ordinary file as opposed to a directory or special file if yes then condition becomes true.
[-F $file] is true.
-G file
Checks if file has its set group ID (SGID) bit set if yes then condition becomes true.
[-G $file] is false.
-K File
Checks if file has its sticky bit set if yes then condition becomes true.
[-K $file] is false.
-P File
Checks if file is a named pipe if yes then condition becomes true.
[-P $file] is false.
-T file
Checks If file descriptor is open and associated with a terminal if yes then condition becomes true.
[-T $file] is false.
-U file
Checks if file has its set User ID (SUID) bit set if yes then condition becomes true.
[-U $file] is false.
-R File
Checks if file is readable if yes then condition becomes true.
[-R $file] is true.
-W File
Check If file is writable if yes then condition becomes true.
[-W $file] is true.
-X File
Check If file is execute if yes then condition becomes true.
[-X $file] is true.
-S file
Check if file has size greater than 0 if yes then condition becomes true.
[-S $file] is true.
-E File
Check if file exists. Is true even if file is a directory but exists.
[-e $file] is true. C Shell Operator:
The following links give a simple use of the C-Shell operator.
The C-shell operator Korn the shell operator:
The following links give a simple use of the Korn shell operator
Korn Shell operator
from:http://www.yiibai.com/shell/what_is_shell.html#