In Linux shell an Array is the collection of same or different kind of elements which are accessible using zero-based index. In shells array there is no specific data type that means while declaring a shells array no need to define the data type for array. You can store integer, float, string etc in a same array.
This post is part of the shell scripts posts. You can read more basic shell script examples on this page ( http://coolstuffsite.blogspot.in/p/shells-script.html )
In this post i will make shells array easy for you with proper examples.
Declaring a Shell Arrays
To declare a shell array use the following syntax.
Note: In bash, declaring array is not necessary. We can insert element individually with array index.
Declaring and Assigning values to an Array:
Method 01:
Use the following syntax to declare and assigning or initializing the values.
index is position of element which start from zero.
value is the array value at particular index.
Method 02:
You can use another method to declare bash array as following syntax.
For example if we use '*' then it will create a array of all directories and files available in current directory. I will make you understand these concept in following examples .
Accessing Bash Array Elements:
To read the array element use the following syntax.
Example01 : Declaring array with method 01 and accessing array elements.
Output:
./bashArray01.sh
3rd elemets of array demoArray is : 100
6th or last element of array is : "This is demoArray script"
List of all elemets : Unix Linux 100 50.0 "This is demoArray script"
Number of elements in array is : 5
Example02: Declaring array with method 02 and accessing array elements.
In the below example 'declare -a demoArray=(Unix Linux 100 50.0 "This is demoArray script")' and 'demoArray=(Unix Linux 100 50.0 "This is demoArray script")' represents the same array.
./bashArray02.sh
3rd elements of array demoArray is : 100
6th or last element of array is :
List of all elements : Unix Linux 100 50.0 This is demoArray script
-------------------------------
Current dir files are : bashArray01.sh bashArray02.sh
-------------------------------
a b c d e f g h i j k l m n o p q r s t u v w x y z
Example02: In this example we are creating an array of all emailIds which are in the customer table of a mysql database. To understand this example consider a table "customer" which having the following details.
Once we get array of above emailIds we will access the emails using a for loop.
./bashArray03.sh
neeraj@gmail.com
tanu@gmail.com
shivam@yahoo.com
vinay.s@gmail.com
ajay.singh@gmail.com
-----------------------------
neeraj@gmail.com tanu@gmail.com shivam@yahoo.com vinay.s@gmail.com ajay.singh@gmail.com
-----------------------------
Total emails are : 5
This post is part of the shell scripts posts. You can read more basic shell script examples on this page ( http://coolstuffsite.blogspot.in/p/shells-script.html )
In this post i will make shells array easy for you with proper examples.
Declaring a Shell Arrays
To declare a shell array use the following syntax.
declare -a array_namewhere array_name is the name of array
Note: In bash, declaring array is not necessary. We can insert element individually with array index.
Declaring and Assigning values to an Array:
Method 01:
Use the following syntax to declare and assigning or initializing the values.
array_name[index]=valuewhere array_name is the name of array.
index is position of element which start from zero.
value is the array value at particular index.
Method 02:
You can use another method to declare bash array as following syntax.
array_name=(value1 value2 ... valuen)This method is useful when you need to take dynamic elements . In this method you can declare space separated static array elements as well as you can place any Linux command, wild cards or SQL query .
For example if we use '*' then it will create a array of all directories and files available in current directory. I will make you understand these concept in following examples .
Accessing Bash Array Elements:
To read the array element use the following syntax.
echo ${array_name[index]}To access all the array elements use the following syntax.
echo ${array_name[@]} #or echo ${array_name[*]}To print the number of elements of an array use the following syntax.
echo ${#array_name[@]}Now lets start some practice with examples.
Example01 : Declaring array with method 01 and accessing array elements.
#!/bin/bash # Declaring an array demoArray. declare -a demoArray # It is optional # Assigning diffrent data type elements to array demoArray demoArray[0]='Unix' demoArray[1]='Linux' demoArray[2]=100 demoArray[4]=50.0 demoArray[5]='"This is demoArray script"' # Printing third element which is an integer echo "3rd elemets of array demoArray is : ${demoArray[2]}" # Printing last element which is a string echo "6th or last element of array is : ${demoArray[5]}" # To print all the elements of the array echo "List of all elemets : ${demoArray[@]}" # To print the number of elements in the array echo "Number of elements in array is : ${#demoArray[*]}"
Output:
./bashArray01.sh
3rd elemets of array demoArray is : 100
6th or last element of array is : "This is demoArray script"
List of all elemets : Unix Linux 100 50.0 "This is demoArray script"
Number of elements in array is : 5
Example02: Declaring array with method 02 and accessing array elements.
In the below example 'declare -a demoArray=(Unix Linux 100 50.0 "This is demoArray script")' and 'demoArray=(Unix Linux 100 50.0 "This is demoArray script")' represents the same array.
#!/bin/bash # Declaring array and assigning static elements declare -a demoArray=(Unix Linux 100 50.0 "This is demoArray script") # Printing third element which is an integer echo "3rd elements of array demoArray is : ${demoArray[2]}" # Printing last element which is a string echo "6th or last element of array is : ${demoArray[5]}" # To print all the elements of the array echo "List of all elements : ${demoArray[@]}" # Declaring and initializing array current directory contents. #declare -a currDirContent=(*) currDirContent=(*) # Printing all elements of current directory. echo "-------------------------------" echo "Current dir files are : ${currDirContent[@]}" # Declaring lower case alphabets array lowercase=({a..z}) # Printing a to z echo "-------------------------------" echo "${lowercase[@]}"Output:
./bashArray02.sh
3rd elements of array demoArray is : 100
6th or last element of array is :
List of all elements : Unix Linux 100 50.0 This is demoArray script
-------------------------------
Current dir files are : bashArray01.sh bashArray02.sh
-------------------------------
a b c d e f g h i j k l m n o p q r s t u v w x y z
Example02: In this example we are creating an array of all emailIds which are in the customer table of a mysql database. To understand this example consider a table "customer" which having the following details.
mysql> select * from customer; +------------+--------------+----------------------+-----------+ | customerid | customername | emailid | accountno | +------------+--------------+----------------------+-----------+ | 1001 | Neeraj singh | neeraj@gmail.com | 30004 | | 1002 | Tanu | tanu@gmail.com | 30005 | | 1003 | Shivam Kumar | shivam@yahoo.com | 30006 | | 1004 | vinay | vinay.s@gmail.com | 30007 | | 1005 | Ajay singh | ajay.singh@gmail.com | 30008 | +------------+--------------+----------------------+-----------+
Once we get array of above emailIds we will access the emails using a for loop.
#!/bin/bash # Creating array of emails from database. declare -a EMAILIDS=(`mysql -umukesh -p*** -hlocalhost -e "SELECT emailid FROM mydb.customer"`) # The above command will be equivalent to : # EMAILIDS=(neeraj@gmail.com tanu@gmail.com shivam@yahoo.com vinay.s@gmail.com ajay.singh@gmail.com ) # Accessing emails using for loop for email in "${EMAILIDS[@]}" do echo "$email" done # Print all array of emails echo "-----------------------------" echo ${EMAILIDS[@]} # Printing number of emails echo "-----------------------------" echo "Total emails are : ${#EMAILIDS[@]}"Output:
./bashArray03.sh
neeraj@gmail.com
tanu@gmail.com
shivam@yahoo.com
vinay.s@gmail.com
ajay.singh@gmail.com
-----------------------------
neeraj@gmail.com tanu@gmail.com shivam@yahoo.com vinay.s@gmail.com ajay.singh@gmail.com
-----------------------------
Total emails are : 5
No comments:
Post a Comment