shell script (using an array of statistical data)

                    Using arrays Statistics

Example: count the number of / etc / passwd in / bin / bash and / sbin / nologin how much

! # / bin / the bash 
# define variables, get / etc / passwd last row 
of NF = `awk -F": " '{} of NF Print $' / etc / passwd` 
# define an associative array Login 
DECLARE -A Login 
# traversal NF variable, i.e. etc / passwd last row 
for NF I in $ 
do 
# login to the array as the index value of ++ 
        the let login [$ I] ++ 
DONE 

# get index from the login array 
for j in $ {! login [ *]} 
do 
# output and the index corresponding to the index value of the 
echo "$ {J $ Login [$ {J}]}" 
DONE


Example Two: IP access and display times access.log

#! / bin / the bash 
# taking the first row access.log 
IP = `awk '{}. 1 Print $' access.log` 
# define an array ipinfo 
DECLARE -A ipinfo 
# variable traverse IP 
for $ I in IP 
do 
# take variable IP ipinfo indexing value is assigned, it will be the same ++ 
        the let ipinfo [$ I] ++ 
dONE 
# get final ipinfo index, and taking the corresponding value 
for J in $ {! ipinfo [*]} 
do 
        echo " ipinfo {J $ $ [$ {J}]} " 
DONE

# The above two examples idea what the statistics, what will be as an array index ++, and finally through the array, the value

Guess you like

Origin blog.51cto.com/13760226/2413507