Today we will learn about arrays and loops in shell scripts. The concept of array is probably not unfamiliar to the coder brothers anymore, so we will ignore it.
Defines the values in the array
1. Indirect definition
The indirect definition is that you will assign each value to each index of the array, for example:
1 2 | ARRAYNAME[INDEX]=value |
1 2 3 4 | FRUIT[0]='APPLE' FRUIT[1]='ORANGE' FRUIT[2]='BANANA' |
2. Definition of mix
With this type, we can declare the array with a range of values, we can also add values later if desired.
1 2 | ARRAYNAME=(VALUE1 VALUE2 VALUE3) |
1 2 | CAR=(TOYOTA VOLVO VINFAST) |
or
1 2 | CAR=([0]=TOYOTA [1]=VOLVO [2]=VINFAST) |
Access values
1. Get all values
To print out all the values in the array, we use:
1 2 3 | echo ${ARRAYNAME[@]} echo ${ARRAYNAME[*]} |
1 2 3 4 | CAR=([0]=TOYOTA [1]=VOLVO [2]=VINFAST) echo ${CAR[@]} echo ${CAR[*]} |
Result:
1 2 3 | TOYOTA VOLVO VINFAST TOYOTA VOLVO VINFAST |
2. Get the value first
1 2 | echo ${ARRAYNAME} |
1 2 3 4 | CAR=([0]=TOYOTA [1]=VOLVO [2]=VINFAST) echo ${CAR} echo ${CAR[0]} |
Result:
1 2 3 | TOYOTA TOYOTA |
3. Get specific values
1 2 | echo ${ARRAYNAME[INDEX]} |
1 2 | echo ${CAR[2]} |
Result:
1 2 | VINFAST |
4. Get the value according to the string index
1 2 | echo ${ARRAYNAME[WHICH_ELEMENT]:STARTING_INDEX:COUNT_ELEMENT} |
1 2 3 | STRING=('THIS IS A LONG STRING') echo ${STRING[0]:8:13} |
Result:
1 2 | A LONG STRING |
5. Specific value lengths
1 2 | echo ${#ARRAYNAME[INDEX]} |
1 2 3 4 | CAR=(TOYOTA VOLVO VINFAST) echo ${#CAR} // tương đương ${#CAR[0]} echo ${#CAR[1]} |
Result:
1 2 3 | 6 5 |
6. Array length
1 2 3 4 | CAR=(TOYOTA VOLVO VINFAST) echo ${#CAR[@]} echo ${#CAR[*]} |
Result:
1 2 3 | 3 3 |
Loop
If there is an array, of course there will be a loop. Well one more thing about the operators in the shell, I forgot to write this article, so you can refer to another article temporarily ( link )
In a shell, there are four types of loops: while, for, until, select. As follows
While
The syntax of while loop is no different in the languages that we often use, as long as the while
condition is satisfied, the loop will continue to run.
1 2 3 4 5 | while command do //something done |
Now we will create a loop that prints out the elements in an array
1 2 3 4 5 6 7 8 9 10 11 | #!/bin/sh arr=(3 6 8 9 20 22 30 49 50 9) i=0 while [ $i -lt ${#arr[@]} ] do echo ${arr[$i]} i=`expr $i + 1` done |
Result:
1 2 3 4 5 6 7 8 9 10 11 | 3 6 8 9 20 22 30 49 50 9 |
You can also have multiple nested loops
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | #!/bin/sh a=0 while [ $a -lt 10 ] # this is loop1 do b=$a while [ $b -ge 0 ] # this is loop2 do echo -n "$b " b=`expr $b - 1` done echo a=`expr $a + 1` done |
Result:
1 2 3 4 5 6 7 8 9 10 11 | 0 1 0 2 1 0 3 2 1 0 4 3 2 1 0 5 4 3 2 1 0 6 5 4 3 2 1 0 7 6 5 4 3 2 1 0 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 |
For
This is the loop that will traverse each element in the array
1 2 3 4 5 6 7 8 | #!/bin/sh arr=(3 6 8 9 20 22 30 49 50 9) for i in "${arr[@]}" do echo $i done |
Result:
1 2 3 4 5 6 7 8 9 10 11 12 | arr=(1 2 3 4 5) # loops iterate through a # set of values until the # list (arr) is exhausted for i in "${arr[@]}" do # access each element # as $i echo $i done |
Until
This loop will run until a condition is met. For example, now I want to get the elements in the array with index <5, then the condition will be until index> 5
1 2 3 4 5 6 7 8 9 10 11 | #!/bin/sh arr=(3 6 8 10 20 22 30 49 50 9) i=0 until [ $i -gt 5 ] do echo ${arr[$i]} i=`expr $i + 1` done |
Result:
1 2 3 4 5 6 7 8 9 10 11 | #!/bin/sh arr=(3 6 8 10 20 22 30 49 50 9) i=0 until [ $i -gt 5 ] do echo ${arr[$i]} i=`expr $i + 1` done |
Select
Literally you will give the user a choice of an option and you will print the results of that option. Try the example below
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | #!/bin/bash PS3="Select number of the gift you want ==> " echo "Select what you want for Christmas eve" select answer in "A cat" "A car" "A new Macbook M1" "A Girl Friend" do case $answer in "A cat") echo "Good choice. Get ready for your meow meow" ;; "A car") echo "Oke, i will buy you a toy" ;; "A new Macbook M1") echo "I'm not Bill Gate" ;; "A Girl Friend") echo "I'm not Cupid" ;; *) echo "Well, you need to enter something from the list!" ;; esac done |
Result:
1 2 3 4 5 6 | Select what you want for Christmas eve 1) A cat 3) A new Macbook M1 2) A car 4) A Girl Friend Select number of the gift you want ==> 4 I'm not Cupid |
Reference articles: