Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

Problems:

  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)?
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
148 views
Welcome To Ask or Share your Answers For Others

1 Answer

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
" "${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

Caution:

"${arr[@]}"

not

"$(arr[@]}"

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...