0

I would like to ask some help how can I improve the basic script below.

So, I was able to pass the arguments to bash, but is it possible for my awk command, to make it more flexible?

I encountered an error in df -h command where it shows 2 lines for 1 filesystem (supposed to be one line only)

and then, the script does not show correct parameters since argument numbers (the $6 and $5) have changed.

Sample:

df -h

Filesystem                   Size            Used  Avail  Use% Mounted on
/dev/test/test2/test3        1.1G            100M  1G     1%   /test
/dev/test/test2/test3/test4
                          1.1G            100M  1G     1%   /tst2


$ cat myscript.sh
#!bin/bash

df -h "$@" | awk '{print $6,"=",$5}' | sed -n '1!p'

$ ./myscript.sh /test /tst2

My output will be

/test=1%

=/tst2

but should be

/test=1%

/tst2=1%
2
  • awk '{print $NF,"=",$(NF -1)}'. You can get rid of the sed as well...
    – jasonwryan
    Commented Oct 30, 2016 at 5:57
  • Oh, Thank you, I think its working correctly now. I will retest this. very helpful!
    – kukiaz
    Commented Oct 30, 2016 at 8:11

1 Answer 1

1

Just use this instead and you won't have to worry about post processing the df output.

df -P

This will make sure output for 1 filesystem does not span over multiple lines.

So your final script will be

df -h -P "$@" | awk '{ print $6,"=",$5}' | sed -n '1!p'

1
  • I'd suggest LANG=C LC_ALL=C df -h -P "$@" | ..., just to ensure the output always uses the POSIX/C locale, so that the decimal point is always a .. In some locales, like the one I prefer to use (fi_FI.utf8), the decimal point is a comma ,, and that confuses many scripts. Commented Oct 30, 2016 at 16:51

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .