数组值的总计数 [英] Total count of the array values

查看:118
本文介绍了数组值的总计数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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"
}

问题:

  1. 如果任何用户输入安装不可用,如何退出循环? 目前仅在日志中添加错误.
  2. 如何获取total_free_space(即free_space的总和)?
  1. How can I exit for loop if any of the user input mount not avaialble? currently it only add error in the log.
  2. 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[@]}"

这篇关于数组值的总计数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆