function containsElement() { local value=$1shiftforitemin"${@:1}";do[["$item"=="$value"]] &&return0donereturn5} A=("one""two""three four")ifcontainsElement"three four""${A[@]}"; then echoinfi
4.0 版的bash中引入的关联数组使用字符串作为下标,并且必须在使用前声明: $ declare -A array $ for subscript in a b c d e > do > array[$subscript]="$subscript $RANDOM" > done $ printf ":%s:\n" "${array["c"]}" ## print one element :c 1574: $ printf ":%s:\n" "${array[@]...
To retrieve the array you need to useparameter expansion, which involves the dollar sign and curly brackets (${ }). The positions of the elements in the array are numbered starting from zero. To get the first element of this array use${plagues[0]}like so: echo${plagues[0]} ## blood...
# Usage: random_array_element "array" local arr=("$@") printf '%s\n' "${arr[RANDOM % $#]}" } 1. 2. 3. 4. 5. 用法示例: $ array=(red green blue yellow brown) $ random_array_element "${array[@]}" yellow # Multiple arguments can also be passed. $ random_array_element 1...
$ array=(red green blue yellow brown) $ random_array_element "${array[@]}" yellow # Multiple arguments can also be passed. $ random_array_element 1 2 3 4 5 6 7 3 循环一个数组 每次printf调用时,都会打印下一个数组元素。当 打印到达最后一个数组元素时,它 ...
ELEMENTS=${#ARRAY[@]} # echo each element in array # for loop for (( i=0;i<$ELEMENTS;i++)); do echo ${ARRAY[${i}]} done 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 8.2. Read file into bash array #!/bin/bash
Beachten Sie jedoch, dass diese Methode fehlschlagen kann, wenn ein Array-Element ein ! enthält.In unserem Beispiel unten verwenden wir das Schlüsselwort basename zum Drucken der Array-Elemente. Der Code für unser Beispiel ist unten angegeben:...
The difference between the two will arise when you try to loop over such an array using quotes. The * notation will return all the elements of the array as a single word result while the @ notation will return a value for each element of the Bash array as a separate word. This becomes...
${#arrayname[@]} gives you the length of the array. $ cat arraymanip.sh declare -a Unix=('Debian' 'Red hat' 'Suse' 'Fedora'); echo ${#Unix[@]} #Number of elements in the array echo ${#Unix} #Number of characters in the first element of the array.i.e Debian ...
i=0count_of_elements=${#array[@]} #counting the number of array elementswhile true; dorest=$(($i%$count_of_elements)) #counting rest of the division by count of array elementsprintf "${array[$rest]}," #dispay resulti=$((i+1))done 如果您更改输入数组(例如,如果它是array=(1 2 ...