]> git.ipfire.org Git - thirdparty/coreutils.git/commitdiff
maint: simplify parsing df's output in shell scripts
authorBernhard Voelker <mail@bernhard-voelker.de>
Thu, 3 Jul 2014 23:54:45 +0000 (01:54 +0200)
committerBernhard Voelker <mail@bernhard-voelker.de>
Thu, 3 Jul 2014 23:54:45 +0000 (01:54 +0200)
Avoid complicated and error-prone parsing of df's output via
sed(1), cut(1), etc., and instead use df's more modern --output
option.

* src/ioblksize.h (in a comment): Simplify the extraction of the
device name of the mounted file system from df's output.
* tests/dd/skip-seek-past-dev.sh: Likewise.
* tests/du/2g.sh: Likewise for the 'avail' column here.
Also avoid the deprecated use of "tail -NUM".
* tests/misc/stat-mount.sh: While at it, remove the determination
of the mount point of "." via df(1) plus sed(1) as it is unused
since commit v8.5-159-gf57cb37 anyway.  Instead, improve this test
by verifying that the output of "stat -c%m ." at least starts with
a slash '/'.

src/ioblksize.h
tests/dd/skip-seek-past-dev.sh
tests/du/2g.sh
tests/misc/stat-mount.sh

index 276d38fe68f13ad1fc4b8794b865c14fb7fb6ab7..55aaeae115918c90c3186878fa350fc7a1f0977a 100644 (file)
@@ -63,7 +63,7 @@
    the readahead setting is 128KiB which was read using:
 
    file="."
-   device=$(df -P --local "$file" | tail -n1 | cut -d' ' -f1)
+   device=$(df --output=source --local "$file" | tail -n1)
    echo $(( $(blockdev --getra $device) * 512 ))
 
    However there isn't a portable way to get the above.
index caa2314dfbe4907b9812500d706cd0e66dc7a92c..5eb7ab5acd0b60eefeba1db4cb5d130efd79f623 100755 (executable)
@@ -33,7 +33,7 @@ get_device_size() {
 
 # Get path to device the current dir is on.
 # Note df can only get fs size, not device size.
-device=$(df -P . | tail -n1 | cut -d' ' -f1) || framework_failure_
+device=$(df --output=source . | tail -n1) || framework_failure_
 
 dev_size=$(get_device_size "$device") ||
   skip_ "failed to determine size of $device"
index cfd1fc4647b1255f0f2822aba4493298da912d67..f766d4d9dd0a776714d327fa52969cad04bf5169 100755 (executable)
@@ -27,10 +27,7 @@ very_expensive_
 
 # Get number of free kilobytes on current partition, so we can
 # skip this test if there is insufficient free space.
-
-# This technique relies on the fact that the 'Available' kilobyte
-# count is the number just before the one with a trailing '%'.
-free_kb=$(df -kP .|tail -1|sed 's/ [0-9][0-9]*%.*//;s/ *$//;s/.* //')
+free_kb=$(df -k --output=avail . | tail -n1)
 case "$free_kb" in
   [0-9]*) ;;
   *) skip_ "invalid size from df: $free_kb";;
index 814d23ce5106869de95f33588f2a125e2992f1e1..ab1ff479bfc00a68724fe5435168b255cfb4cf6f 100755 (executable)
 . "${srcdir=.}/tests/init.sh"; path_prepend_ ./src
 print_ver_ stat
 
-df_mnt=$(df -P . | sed -n '2s/.* \([^ ]*$\)/\1/p')
 stat_mnt=$(stat -c%m .) || fail=1
-test "$stat_mnt" || fail=1
+case "$stat_mnt" in
+  /*) ;;
+  *) fail=1;;
+esac
 
 Exit $fail