As RudiC already told you the output is not a string. Try the command on a command line in your terminal and you will see a nicely formatted (ASCII)-table.
The answer to your question is simple: use a shell script. I assume (because your interest in Java processes) that you have some programming background and the good news is: all the common UNIX/Linux shells have a twofold purpose. They can be used to enter commands interactively but they are also (quite powerful) language interpreters for the programming language they implement. If you have a Windows background: think of cmd.exe
and powershell rolled into one.
To implement a script for your filesystems, you might want to start with selecting a better option set to df
: the command you used is optimized for human reading (hence the option -h
), which is not optimal for machine reading. As you might have noticed there are different units used for filesystem sizes and parsing (K)ilobytes, (M)egabytes, (G)igabytes and whatnot will make your code more complicated.
Using the -P
(POSIX) switch will make that easier because only one unit (1024-byte blocks) will be used. Furthermore, POSIX is *the* UNIX standard and this way you will be able to use your script on a wide variety of UNIX systems whereas -h
is only available on Linux-systems and if you use it your script will only run on Linux-systems.
Now try the command on your terminal and look at the output. Here is an example of the laptop i am writing this:
# df -P
Filesystem 1024-blocks Used Available Capacity Mounted on
udev 16383972 0 16383972 0% /dev
tmpfs 3281216 9532 3271684 1% /run
/dev/sda6 19553560 6880668 11656572 38% /
tmpfs 16406076 284 16405792 1% /dev/shm
tmpfs 5120 4 5116 1% /run/lock
tmpfs 16406076 0 16406076 0% /sys/fs/cgroup
/dev/sda4 20511356 407184 19039212 3% /opt/games
/dev/sda7 19553560 44992 18492248 1% /altroot
/dev/sda3 50385596 6707112 41095956 15% /opt/images
/dev/sda1 474730 135491 310209 31% /boot
/dev/sda8 49082176 2452096 44113756 6% /home
tmpfs 3281216 36 3281180 1% /run/user/1000
You first need to analyse which part of the information you are interested in: probably not the first line of output because this is header information. Maybe also not in filesystems of the type "tmpfs" or "udev" because these are not real filesystems, they are only created at runtime. You can filter lines with the grep
command (i suggest you read the man page for it, also try the command and play around with it by changing it and observing the different outcomes), so let us try this:
# df -P | grep -ve tmpfs -ve udev -ve Filesystem
/dev/sda6 19553560 6880672 11656568 38% /
/dev/sda4 20511356 407184 19039212 3% /opt/games
/dev/sda7 19553560 44992 18492248 1% /altroot
/dev/sda3 50385596 6707112 41095956 15% /opt/images
/dev/sda1 474730 135491 310209 31% /boot
/dev/sda8 49082176 2452104 44113748 6% /home
OK, this is is what we are really interested in. Now we need to read the columns one by one and assign the values to variables which we will later be able to process. For this there is the shell built-in command read
. The shell will split the input along whitespace itself so we don't have to do this ourselves. For instance:
# df -P | grep -ve tmpfs -ve udev -ve Filesystem | while read device size used avail percent mount ; do
echo MOUNT: $mount
echo " device...: $device"
echo " size.....: $size"
echo " used.....: $used (${percent})"
echo " available: $avail"
echo ""
done
As you see commands can span several lines and you still can enter them on the command line. You can also save this to a file and execute it - this is a "script"! There is no difference between what you enter on the command line and what you write in scripts - everything you use on the command line can be used in a script and vice versa.
A few things in the above command need to be explained:
read var1 var2 var3 ....
this command reads a line of input (in the case above provided by the directing the output of the previous command into it, a so-called "pipeline") and splits it along whitespace. The first "word" then goes to a variable called "var1", the second word to "var2" and so on. If there are more words than variables the last variable gets all the remaining words, if there are more variables than words the last variables will be empty. In our case there are exactly as many variables as there are words and each variable gets one word.
while command ; do
commands ....
done
This is a loop which is repeated as long as command (which can be any command or built-in, in our case it is the read
) returns a return code of 0. Every command, when ending, sets such a return code and UNIX convention is that a return code of 0 means "success", everything else some sort of failure.
In our case the read
will return 0 as long as there are input lines to read, only when there is nothing to read any more it will return non-0, which will terminate the while-loop.
A last thing about variables: when you assign them you do that:
var=content
but when you want to use them (i.e. to display them) you need to put a "$" before their name like this:
echo $var
you can also surround the output of a variable by fixed text:
# var=mycontent
# echo $var
mycontent
echo ==$var==
==mycontent==
but in some cases where the fixed text will be characters it might be ambiguous:
# var=mycontent
# echo foo$varbar
??
This would display "foo" followed by the content of the variable varbar
instead of the variable var
followed by the fixed text "bar". TO remove such ambiguities use the braces:
# var=mycontent
# echo foo${var}bar
foomycontentbar
Now, this is a rather lame excuse for a good introduction into shell programming, but i hope i have piqued your interest enough to make you start scripting. Have fun with the shell!
bakunin