TrungTrung
TrungTrung

Reputation: 33

BASH scipt. how to code if (array=nothing) then echo "no element in array" in bash script

in bash script, I define a array:

array=$(awk '{print $4}' /var/log/httpd/sample | uniq -c | cut -d[ -f1)

Now, I want to translate this content to code in bash script:

"if there is NOT any element in array, it means array=nothing, then echo "nothing in array".

help me to do that??? Thanks a lot

*besides, I want to delete access_log's content periodically every 5min (/var/log/httpd/access_log). Please tell me how to do that??*

Upvotes: 0

Views: 85

Answers (1)

devnull
devnull

Reputation: 123558

Saying:

array=$(awk '{print $4}' /var/log/httpd/sample | uniq -c | cut -d[ -f1)

does not define an array. This simply puts the result of the command into the variable array.

If you wanted to define an array, you'd say:

array=( $(awk '{print $4}' /var/log/httpd/sample | uniq -c | cut -d[ -f1) )

You can get the count of the elements in the array by saying echo "${#foo[@]}".

For checking whether the array contains an element or not, you can say:

(( "${#array[@]}" )) || echo "Nothing in array"

Upvotes: 2

Related Questions