I am trying to get an error message if one of the disks is filed more than 80%. So we have two arrays which have the data of disk partitions (they can have 3 partitions like in this example or more, but under 10). they always are in tandem and correspond 1 on 1. first element of the array disk available corresponds to the 1 element of disk used, 2 to the 2 and so on.

array1 as disk_available (not always the same as in this example)

array1=($(curl -ks -XGET https://API/cluster1| awk '/disk_space_available/ {print $5}'))
echo "${array1[*]}"
65536 65536 65536

and array2 as disk_space_used

array2=($(curl -ks -XGET https://API/cluster1| awk '/disk_space_used/ {print $5}'))
echo "${array2[*]}"
34856 11672 34994

we want to check if first element from disk_space_used (34856) represents more than 80% of first element (65536) from disk_available. if lower than 80% go to element number two and check it and so on, otherwise popup an error message. if more than 1 partition (element of array2) used_disk is greater than 80% of available in available_disk (element of array1) save it in another array/list/whatever so than we can print them all afterwards.

i tried this one but it fails, the percent part is not quite correct (got it from stackexchange somewhere): this code is just my attempt, not reproducible and probably wrong in more than one way.

for index in "${!array1[*]}"
    percent=$(( 100 * ${array2[$index]} / ${array1[$index]} + (1000 * ${array2[$index]} / ${array1[$index]}  % 10 >= 5 ? 1 : 0) ))
    if [ "$percent" -gt 80 ]; then
    a0+=("FAIL - Storage is $percent% full")
  • 2
    What is array, what is a0? What do you expect? For me it seems working fine after replace array to array1 and a0+=(..). with echo ...
    – pLumo
    May 27 '20 at 14:36
  • You say that the arrays are called disk_available and disk_space_used. Your sample code doesn't reference those arrays at all.
    – roaima
    May 27 '20 at 15:21
  • like I said, the array1 stands for full capacity, array2 for used capacity. maybe it is need it a 2 for and not 1!? i don’t have a reproducible code as i don t really know how to solve it. a0 is an array if all 3 elements of used capcity are over 80% so i save them all and print afterwards
    – trustbyte
    May 27 '20 at 15:26
  • Those disk space numbers should come from somewhere. A file, a command? Please edit the question with that information.
    – Quasímodo
    May 27 '20 at 15:46

normaly a percentage op needs total/used not avail/used !

in array or parametrs when we use * insteed of @ we get one string where you can't loop over!

anyhow try this

#get data from api save it to variable in order to avoide calling it twice (resources consumption and asyncroned results)!
disk_infos="$(curl -ks -XGET https://API/cluster1)";
# extract disk_space_available frome results 
disk_available=($(awk '/disk_space_available/ {print $5}' <<< "${disk_infos}"));
# extract disk_space_used from same results 
disk_used=($(awk '/disk_space_used/ {print $5}' <<< "${disk_infos}"));
# declare empty array for holding errors
# loop over arrays
for i in "${!disk_used[@]}";do 
    used_perc=$((disk_used[i]*100/disk_available[i])); # calculate percentage
    if [[ "${used_perc}" -gt 80 ]]; then # check perc value if greeter than 80
        disk_errors+=("FAIL - Storage is ${used_perc}% full at index ${i}"); # append the error to the holder 
# check if errors were encountred
if ((${#disk_errors[@]}));then # check if disk_errors count is not 0
    echo ${#disk_errors[@]} errors found; # print errors count
    printf '%s\n' "${disk_errors[@]}" ; # print each error msg in a line
    echo everything is fine ${#disk_errors[@]} errors found; # print errors count 
  • i did not understood what for the "<<< disk_infos" and i did not applied it in my end script. my arrays were correctly filled. but the part with the looping i got it with your script, thank you. now it correctly gets the percentage per partition in my end script and i get all the full disk reports. thank you Jonah!
    – trustbyte
    May 27 '20 at 19:12
  • cmd <<< "$variable" is_like echo "$variable | cmd"
    – Yunus
    May 27 '20 at 19:42
  • okeeeey, now i got it. no need to query the api more than need it. thanks for the added comments on the script.
    – trustbyte
    May 27 '20 at 20:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.