pmem / ndctl

A "device memory" enabling project encompassing tools and libraries for CXL, NVDIMMs, DAX, memory tiering and other platform memory device topics.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

ndctl:ndctl / inject-smart.sh fails with LANG=cs_CZ.UTF-8

lenticularis39 opened this issue · comments

With locale set to cs_CZ.UTF-8, inject-smart.sh test fails with the following error (full log of the test here):

++ printf '%0.0f\n' 23.0
/home/tglozar/dev/ndctl/test/inject-smart.sh: řádek 108: printf: 23.0: chybné číslo
+++ err 108
++++ basename /home/tglozar/dev/ndctl/test/inject-smart.sh
+++ echo test/inject-smart.sh: failed at line 108
+++ '[' -n '' ']'
+++ exit 1
+ old_val='23
test/inject-smart.sh: failed at line 108'
++ err 132
+++ basename /home/tglozar/d
ev/ndctl/test/inject-smart.sh
++ echo test/inject-smart.sh: failed at line 132
++ '[' -n '' ']'
++ exit 1

This is caused by the floating point separator being , in Czech locale rather than .:

$ printf '%f\n' 20.0; echo $?
bash: printf: 20.0: chybné číslo
20,000000
1
$ LC_ALL=C printf '%f\n' 20.0; echo $?
20.000000
0

Running the test with LC_ALL=C fixes the issue.

@lenticularis39 Does something like this fix it for you?

diff --git a/test/inject-smart.sh b/test/inject-smart.sh
index 046322b..4bca492 100755
--- a/test/inject-smart.sh
+++ b/test/inject-smart.sh
@@ -105,7 +105,14 @@ get_field()
        json="$($NDCTL list -b $bus -d $dimm -H)"
        val="$(jq -r ".[].dimms[].health.$smart_listing" <<< $json)"
        val="$(translate_val $val)"
-       printf "%0.0f\n" "$val"
+
+       # val can be e.g. "20.0". Printf complains on this for locales that
+       # use ',' as the decimal separator. For this %0.0f case, it doesn't
+       # matter what the decimal separator is, and bash helpfully does print
+       # "20" even if #$? indicates an error, and there is an error message
+       # on stderr. Ignore the error, and just pick the "20" to avoid such
+       # cases
+       printf "%0.0f\n" "$val" 2 >/dev/null || true
 }
 
 verify()

@stellarhopper Yes, that fixes the issue, if applied without typo 2 >/dev/null -> 2>/dev/null.