数组值的总计数

编程入门 行业动态 更新时间:2024-10-25 05:17:48
本文介绍了数组值的总计数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

在这里,我接受用户的几个挂载点,并使用每个值来获取主机上的可用空间.

Here I'm accepting few mount points from the user and using each value to get space available on the host.

./user_input.ksh -string /m01,/m02,/m03 #!/bin/ksh STR=$2 function showMounts { echo "$STR" arr=($(tr ',' ' ' <<< "$STR")) printf "%s\n" "$(arr[@]}" for x in "${arr[@]}" do free_space=`df -h "$x" | grep -v "Avail" | awk '{print $4}'` echo "$x": free_space "$free_space" done #echo "$total_free_space" }

问题:

  • 如果任何用户输入安装不可用,如何退出循环? 目前仅在日志中添加错误.
  • 如何获取total_free_space(即free_space的总和)?
  • How can I exit for loop if any of the user input mount not avaialble? currently it only add error in the log.
  • How to get total_free_space (i.e. sum of free_space)?
  • 推荐答案

    如果您想保留代码,请进行测试(此处没有ksh).如果您不在乎,请阅读爱德华·莫顿的答案.

    If you want to keep your code , test this (no ksh here). If you don't care, read Ed Morton's answer.

    ./user_input.ksh -string /m01,/m02,/m03 #!/bin/ksh STR=$2 function showMounts { echo "$STR" arr=($(tr ',' ' ' <<< "$STR")) printf "%s\n" "${arr[@]}" for x in "${arr[@]}"; do free_space=$(df -P "$x" | awk 'NR > 1 && !/Avail/{print $4}') echo "$x: free_space $free_space" ((total_free_space+=$free_space)) done echo "$((total_free_space/1024/1000))G" } showMounts

    警告:

    "${arr[@]}"

    不是

    "$(arr[@]}"

    更多推荐

    数组值的总计数

    本文发布于:2023-10-31 07:48:11,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1545482.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:数组

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!