程式扎記: [Linux 文章收集] Unix Shell Programming - Using Shell Arrays

標籤

2016年3月3日 星期四

[Linux 文章收集] Unix Shell Programming - Using Shell Arrays

Source From Here 
Introduction 
A shell variable is capable enough to hold a single value. This type of variables are called scalar variables. Shell supports a different type of variable called an array variable that can hold multiple values at the same time. Arrays provide a method of grouping a set of variables. Instead of creating a new name for each variable that is required, you can use a single array variable that stores all the other variables. All the naming rules discussed for Shell Variables would be applicable while naming arrays. 

Defining Array Values 
The difference between an array variable and a scalar variable can be explained as follows. Say that you are trying to represent the names of various students as a set of variables. Each of the individual variables is a scalar variable as follows − 
  1. NAME01="Zara"  
  2. NAME02="Qadir"  
  3. NAME03="Mahnaz"  
  4. NAME04="Ayan"  
  5. NAME05="Daisy"  
We can use a single array to store all the above mentioned names. Following is the simplest method of creating an array variable is to assign a value to one of its indices. This is expressed as follows: 
  1. NAME[0]="Zara"  
  2. NAME[1]="Qadir"  
  3. NAME[2]="Mahnaz"  
  4. NAME[3]="Ayan"  
  5. NAME[4]="Daisy"  
If you are using ksh shell the here is the syntax of array initialization − 
  1. set -A array_name value1 value2 ... valuen  
If you are using bash shell the here is the syntax of array initialization − 
  1. array_name=(value1 ... valuen)  
Accessing Array Values 
After you have set any array variable, you access it as follows: 
  1. ${array_name[index]}  
Here array_name is the name of the array, and index is the index of the value to be accessed. Following is the simplest example: 
- test.sh 
  1. #!/bin/sh  
  2.   
  3. NAME[0]="Zara"  
  4. NAME[1]="Qadir"  
  5. NAME[2]="Mahnaz"  
  6. NAME[3]="Ayan"  
  7. NAME[4]="Daisy"  
  8. echo "First Index: ${NAME[0]}"  
  9. echo "Second Index: ${NAME[1]}"  
This would produce following result: 
# ./test.sh
First Index: Zara
Second Index: Qadir

You can access all the items in an array in one of the following ways: 
  1. ${array_name[*]}  
  2. ${array_name[@]}  
Here array_name is the name of the array you are interested in. Following is the simplest example: 
- test2.sh 
  1. #!/bin/sh  
  2.   
  3. NAME[0]="Zara"  
  4. NAME[1]="Qadir"  
  5. NAME[2]="Mahnaz"  
  6. NAME[3]="Ayan"  
  7. NAME[4]="Daisy"  
  8. echo "First Method: ${NAME[*]}"  
  9. echo "Second Method: ${NAME[@]}"  
This would produce following result: 
# ./test.sh
First Method: Zara Qadir Mahnaz Ayan Daisy
Second Method: Zara Qadir Mahnaz Ayan Daisy

Supplement 
Bash For Loop Array: Iterate Through Array Values 
  1. #!/bin/bash  
  2. # declare an array called array and define 3 vales  
  3. array=( one two three )  
  4. for i in "${array[@]}"  
  5. do  
  6.     echo $i  
  7. done  

[Linux 常見問題] Shell - Does linux shell support list data structure?

沒有留言:

張貼留言

網誌存檔

關於我自己

我的相片
Where there is a will, there is a way!