On 3/16/19 3:23 PM, L A Walsh wrote:
On 3/16/2019 9:07 AM, Bruce Dubbs wrote:
It would be helpful to add a --terse option to df such that a user could
do something like:

DF=$(df -k --terse --output=used $PWD)

Right now I see no method of omitting the header or to collapse the
spaces between values to a single space.  This can be done of course
with tools like awk, cut, sed, grep, tail, etc, but would be a lot
cleaner if done inside df.

Would this work?  it is inside "df" (sorta) ;-):

df ()
{
     if [[ ${-//[^i]/} == i ]]; then
         declare -i x=0;
         command df -k --output=source,used "$@" | while read src used; do
             ((x++)) && printf "%s %s\n" "$src" "$used";
         done;
     else
         command df "$@";
     fi
}

*or*, all on 1 line:

df() { if [[ ${-//[^i]/} == i ]]; then declare -i x=0;command df -k
--output=source,used "$@"|while read src used; do ((x++)) && printf "%s
%s\n" "$src" "$used" ; done ; else command df "$@" ; fi ; }
-----
  So it will only take 1 line in your history; :-)

It's a tad longer than minimum, as it tests to see if it is interactive
and does your format for interactive use, but if the function
was defined in a script, the function will call 'df' w/no filtering
to give "standard output".

If you define your functions in bash_profile and in bashrc,
it will usually work the way you want it to... ;-) Though in
some cases util mods need more than a function wrapper.

Anyway this should give you the directories you listed on the cmd line
followed by size in 'KB':

df /home /backup
/dev/Data/Home 1350442108
/dev/mapper/Backup-Backup 11203606872

  (you sure you didn't want to include -h
along with -k, though you can pass it on the cmd line:

df -h /home /backup
/dev/Data/Home 1.3T
/dev/mapper/Backup-Backup 11T

I already use:

before=$(df -k /build|tail -n1|sed -e "s/ \{2,\}/ /g"|cut -d' ' -f3)

but this would be easier:

before=$(df -k --terse --output=used /build)

My thought is that --terse would be convenient for others to have the output more easily machine readable.

If I used $(df -k --output=used /build|tail -n1), I still might need to account for potential leading blanks.

  -- Bruce

Reply via email to