【问题标题】:BASH scipt. how to code if (array=nothing) then echo "no element in array" in bash scriptBASH 脚本。如何编码 if (array=nothing) 然后在 bash 脚本中回显“数组中没有元素”
【发布时间】:2013-12-03 05:02:10
【问题描述】:

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

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

现在,我想将此内容翻译成 bash 脚本中的代码:

"如果数组中没有任何元素,则表示array=nothing,则echo "nothing in array"。

帮我做那件事???非常感谢

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

【问题讨论】:

    标签: arrays linux bash shell


    【解决方案1】:

    说:

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

    不定义数组。这只是将命令的结果放入 变量 array

    如果你想定义一个数组,你会说:

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

    您可以通过说echo "${#foo[@]}" 来获取数组中元素的计数。

    为了检查数组是否包含元素,你可以说:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-12-19
      • 1970-01-01
      • 2013-01-01
      • 2016-12-21
      • 2016-08-24
      • 1970-01-01
      • 2018-07-05
      • 1970-01-01
      相关资源
      最近更新 更多