[DTrace-devel] [PATCH] test: use more reliable way of finding kernel versions

Eugene Loh eugene.loh at oracle.com
Tue Dec 14 19:55:38 UTC 2021


On 12/14/21 2:15 PM, Nick Alcock via DTrace-devel wrote:

> In several places we were using cut and/or awk to find kernel versions:
> in each case the code used worked for versions like 5.15 or 5.11.4 but
> failed for things like 5.15+ or 5.15-00024-g679b45e057b4-dirty,
> because they are splitting fields on dots alone.
>
> The rarely-used and little-known grep -o can do all of this more simply
> and reliably, since it can split fields using full regular expressions
> (and thus can easily be told that what we are looking for here is a run
> of digits only).
>
> Signed-off-by: Nick Alcock <nick.alcock at oracle.com>
> ---
>   test/unittest/noresolve/test.x      | 2 +-
>   test/unittest/pid/test.x            | 2 +-
>   test/unittest/stack/tst.stack_fbt.x | 2 +-
>   3 files changed, 3 insertions(+), 3 deletions(-)
>
> diff --git a/test/unittest/noresolve/test.x b/test/unittest/noresolve/test.x
> index 0b811132eea3..7d9eeabefb20 100755
> --- a/test/unittest/noresolve/test.x
> +++ b/test/unittest/noresolve/test.x
> @@ -4,5 +4,5 @@
>   #
>   
>   # UEK4 will not ever have the pid provider.
> -[[ $(uname -r | cut -d. -f 1-2) = 4.1 ]] && exit 1
> +[[ $(uname -r | grep -Eo '^[0-9]+\.[0-9]+') = 4.1 ]] && exit 1
>   exit 0
> diff --git a/test/unittest/pid/test.x b/test/unittest/pid/test.x
> index cbe2b4601f48..0447f0cb6c0c 100755
> --- a/test/unittest/pid/test.x
> +++ b/test/unittest/pid/test.x
> @@ -11,5 +11,5 @@
>   [[ $_test =~ .**/tst\.restartloop ]] && exit 0
>   
>   # UEK4 will not ever have the pid provider.
> -[[ $(uname -r | cut -d. -f 1-2) = 4.1 ]] && exit 1
> +[[ $(uname -r | grep -Eo '^[0-9]+\.[0-9]+') = 4.1 ]] && exit 1
>   exit 0
> diff --git a/test/unittest/stack/tst.stack_fbt.x b/test/unittest/stack/tst.stack_fbt.x
> index 0bbc4706b3e8..86f2694a97f4 100755
> --- a/test/unittest/stack/tst.stack_fbt.x
> +++ b/test/unittest/stack/tst.stack_fbt.x
> @@ -1,6 +1,6 @@
>   #!/bin/bash
>   
> -read MAJOR MINOR <<< `uname -r | awk -F. '{print $1, $2}'`
> +read MAJOR MINOR <<< `uname -r | grep -Eo '^[0-9]+\.[0-9]+'

Is that right?

1)  Is there a missing back apostrophe?

2)  Are you getting two fields (MAJOR and MINOR) out?  Or just one?

>   if [ $MAJOR -eq 5 -a $MINOR -lt 8 ]; then
>   	exit 0



More information about the DTrace-devel mailing list