【问题标题】:how to delete access_log's content periodically & how to code "if (array=nothing) then..." in bash script如何定期删除 access_log 的内容以及如何在 bash 脚本中编写“if (array=nothing) then ...”
【发布时间】:2013-12-19 00:43:55
【问题描述】:

首先,我想每隔 5 分钟定期删除 access_log 的内容 (/var/log/httpd/access_log)。请告诉我该怎么做??

其次,在 bash 脚本中,我定义了一个数组:

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

现在,我想知道如何使用带有此内容的 if 命令:

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

这是一个关于数组的示例脚本:

# Tinh n2
arr=(${array[*]})
for (( i=(${#arr[@]} - 2),j=(${#arr[@]} - 1); i >= (${#arr[@]} - 2) , j > (${#arr[@]} - 2); i--,j-- )); do
  b=$( expr ${arr[j]} - ${arr[i]} )
  n2=$(abs $b)
done 
echo "n2 = $n2"

请告诉我如何编码?

非常感谢。

【问题讨论】:

    标签: arrays apache bash logging


    【解决方案1】:

    要截断文件,请将此命令添加到您的 crontab 配置文件中:

     > /var/log/httpd/access_log
    

    注意:'>' 是必需的

    要检查空数组,请测试元素计数

    if [ ${#array[@]} -eq 0 ]; then
        echo "Array is empty"
    else
        echo "We have nail"
    fi
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-12-10
      • 2012-01-27
      • 1970-01-01
      • 2021-08-10
      • 2016-07-04
      • 1970-01-01
      • 2015-10-30
      相关资源
      最近更新 更多