List of Topics
1) What is array
2) Options with Array
1) What is Array
2) Syntax of Array
Where,
Name - Is any name for an array
Index could be any number or expression that must evaluate to a number greater than or equal to zero.You can declare an explicit array using declare -a arrayname.
3) Syntax of Array
declare -a arrayn=(element1 element2 element3)
4) Other options with the array
echo ${#arrayname[@]}
echo ${#arrayname[Index]}
[user@Server ~]$ cat a.sh
#! /bin/bash
## Start of Script
Unix[0]='Debian'
Unix[1]='Red hat'
Unix[2]='Ubuntu'
Unix[3]='Suse Linux'
echo ${#Unix[3]}
echo ${#Unix[@]}
##End of Script
[user@Server ~]$ sh a.sh
6
10
4
14. Deleting an
Entire Array
#!/bin/bash
$ ./arraymanip.sh
#!/bin/bash
1) What is array
2) Options with Array
1) What is Array
Name [index] = value
Where,
Name - Is any name for an array
Index could be any number or expression that must evaluate to a number greater than or equal to zero.You can declare an explicit array using declare -a arrayname.
If the elements has
the white space character, enclose it with in a quotes.
3) Syntax of Array
declare -a Unix=('Debian' 'Red hat' 'Suse' 'Fedora');
declare -a arrayn=(element1 element2 element3)
echo ${arrayn[1]}
In bash, array is created automatically when a variable is used in the format like,
There are different ways to print the whole elements
of the array. If the index number is @ or *, all members of an array are
referenced. You can traverse through the array elements and print it, using
looping statements in bash.
4) Other options with the array
a) To find length of the Array
echo ${#arrayname[Index]}
[user@Server ~]$ cat a.sh
#! /bin/bash
## Start of Script
Unix[0]='Debian'
Unix[1]='Red hat'
Unix[2]='Ubuntu'
Unix[3]='Suse Linux'
echo ${#Unix} #Number of characters in the first element of the array.i.e Debian
echo ${#Unix[@]}
##End of Script
[user@Server ~]$ sh a.sh
6
10
4
unset is used to
delete an entire array.
$cat arraymanip.sh
#!/bin/bash
Unix=('Debian' 'Red
hat' 'Ubuntu' 'Suse' 'Fedora' 'UTS' 'OpenLinux');
Shell=('bash' 'csh'
'jsh' 'rsh' 'ksh' 'rc' 'tcsh');
UnixShell=("${Unix[@]}"
"${Shell[@]}")
unset UnixShell
echo ${#UnixShell[@]}
$ ./arraymanip.sh
0
regex[1]="line1\|here1"
regex[2]="line2\|here2"
regex[3]="line3\|here3"
regex[4]="line4\|here4"
for i in "${!regex[@]}"
do
grep -h -r
--color=always "${regex["$i"]}" file.txt >>
"pattern$i.txt"
done
#!/bin/bash
array=(un1Z1269E9909
rb1Z129098515)
echo ${array[0]:0:2}
echo ${array[1]:0:2}
three=${(array[3]:0:2):=word}
echo $three
var="$1"
var=${var:=word}
echo "$var"
No comments:
Post a Comment