To declare an array in bash
Declare and an array called array and assign three values:
1 |
array=( one two three ) |
More examples:
1 2 3 4 5 6 7 |
files=( "/etc/passwd" "/etc/group" "/etc/hosts" ) limits=( 10, 20, 26, 39, 48) To print an array use: <pre lang="bash">printf "%s\n" "${array[@]}" printf "%s\n" "${files[@]}" printf "%s\n" "${limits[@]}" |
To Iterate Through Array Values
Use for loop syntax as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
for i in "${arrayName[@]}" do : # do whatever on $i done $i will hold each item in an array. Here is a sample working script: #!/bin/bash # declare an array called array and define 3 vales array=( one two three ) for i in "${array[@]}" do echo $i done |
Example script for get ip addresses domains
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
#!/bin/bash # Script nslookup host & write log # Autor Sobol Denis snakesclub@yandex.ru 2016 # NAME SERVERS NS=(8.8.8.8 77.88.8.88) DOMAIN=".domain.com" # HOSTS HOSTS=(www mail stat pay) for x in "${NS[@]}" do echo "nslookup from" $x for i in "${HOSTS[@]}" do nshost=`nslookup $i$DOMAIN $x | grep Address | tail -1` echo $i$DOMAIN $nshost done done |