0

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??*

1 Answer 1

2

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"
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.