Scripting array issue

Hi guys,

I'm a scripting noob in need of some help :slight_smile:
I am creating a script that checks the filesystems and will alert based upon if the percent full is greater than the threshold set.

The problem that I am having is that when I set the 'filesystem' variable, all of the output is treated as 1 single variable instead of 4 variables that will run through.

so: /dev/sda2 /dev/sda1 /dev/sda3 /dev/sda4 will be set as 1 variable instead of being broken out and spilt into 4 different variables...
I think this needs to be done with an array, but i'm not sure how to accomplish this.

Any suggestions?

Thanks:)

#!/bin/bash
THRESHOLD="30%"
filesystems=$(df -H | grep -vE '^Filesystem|tmpfs|cdrom' | awk '{print $1}')
for i in $filesystems
do
UTILIZED=$(/bin/df -k $filesystems | /usr/bin/tail -1 | /bin/awk '{print $5}')
# Compare the utilized value against the threshold:
  if [[ "$UTILIZED" > "$THRESHOLD" || "$UTILIZED" = "100%" ]]; then
        echo -e "OS|Filesystem|PctUsed\t$filesystems\t2\t$filesystems is at $UTILIZED.  Please have a sysadmin check the environment for issues."
  else
        echo -e "OS|Filesytem|PctUsed\t$filesystems\t0"
  fi

done

exit 0

Looks just like ksh - must tell shell it is an array:

Array variables

1 Like
df | awk -v THRESHOLD=30 '/\/dev\/sd/{if(int($5)>THRESHOLD) print $1}'

Please post a sample of:

df -H
df -k

And matching sample desired output.

It is wise to keep an eye on inodes, too.

Here are the outputs requested:

# df -H
Filesystem             Size   Used  Avail Use% Mounted on
/dev/sda2               11G   3.6G   6.1G  37% /
/dev/sda5               11G   6.4G   3.3G  67% /opt
/dev/sda3               11G   5.0G   4.8G  51% /var
/dev/sda1              128M    12M   110M  10% /boot
tmpfs                  4.3G      0   4.3G   0% /dev/shm

# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/sda2             9.5G  3.4G  5.7G  37% /
/dev/sda5             9.5G  6.0G  3.1G  67% /opt
/dev/sda3             9.5G  4.6G  4.4G  52% /var
/dev/sda1             122M   11M  105M  10% /boot
tmpfs                 4.0G     0  4.0G   0% /dev/shm
$ df -i .
/home                (/home      ) :  1834176 total i-nodes
                                                    116539 free i-nodes
                                                   1717637 used i-nodes
                                                      1473 % i-nodes used
$

HPUX does a strange percentage calc, but inodes are not a problem on vxfs, as it makes more on demand, so some filtering to ensure only static inode count FS are checked is in order! :smiley:

#!/usr/bin/env ruby
require 'net/smtp'
def send_email(from,to,subject,message,server)
    msg =

---------- Post updated at 10:02 PM ---------- Previous update was at 09:58 PM ----------


#!/usr/bin/env ruby
require 'net/smtp'
def send_email(from,to,subject,message,server)
    msg = <<-END_OF_MESSAGE
    From: <#{from}>
    To: <#{to}>
    Subject: #{subject}

    #{message}
   END_OF_MESSAGE

   Net::SMTP.start(server) do |smtp|
   smtp.send_message msg, from, to
   end
end

threshold=80
df=`df -H`
msg=[]
df.split("\n").each do|s|
  s=s.split
  cap=s[4].to_i
  filesystem=s[0]
  if cap >= threshold
    msg<<"#{filesystem}: #{cap}"
  end
end
if msg.size>1
 send_email("root@localhost","root","Alert","#{msg.join("\n")}","localhost")
end
threshold=33
df -P | { read
 while read fs size used avail pcent mount
 do
  if [ ${pcent%?} -gt ${threshold:?} ]
  then
    printf "Filesystem %s on %s is %s full\n" "$fs" "$mount" "$pcent"
  fi
 done
}

We got the "df -H" , but you posted "df -h" not "df -k". When we have a script which is not working we like to see what the output from the key commands in the script looks like.
Also what do you want the final output of the script to look like?

Btw. If you have "df -P" on your system, just use cfajohnson's post .

@DGPickett . This is clearly not HP-UX (because "df -H" is not valid), but we don't know yet what O/S it is.

df -P is standard (POSIX).

@cfajohnson
Though not the first time I have seen "df -P", it is very rarely used in day-today scripting because of the units. The percentage used figure is good and all we needed for this exercise.
The syntax is not valid in umpteen versions of unix, though we can deduce that the O/P has a modern one which may well offer a POSIX compliant "df" option. Whether the POSIX version is called "df" or "bdf" is a matter for the standards people.

@DGPickett
You may find that issuing "df" commands as a non-root user gives you a different view of free space.

What units? The -P option doesn't change the units.

When scripting, one should always use the -P option, otherwise parsing the output is much harder.

I doubt that there are any modern Unixes in which is is not valid. Can you name one?

The POSIX version is called df.