1

Pseudocode

[ `uname -r` =~ ^4\.4.*$ ] && echo "yes"  

which is, however, unsuccessful in all cases. The uname -r command gives things like 4.6.0-040600-generic as an output.

  • 1
    I'm assuming you have some other systems that return 4.4.something? – Jeff Schaller Jun 13 '16 at 17:26
  • 1
    manual page is good place to start. Manual page for [ (test) does not have ~= operator. It is bash built-in [[, which accepts ~= operator. – Jakuje Jun 13 '16 at 17:27
  • 1
    also, "matching" on .*$ is superfluous -- just stop at "^4\.4\.". Otherwise you're saying "match start-of-line, 4, period, 4, (anything), end-of-line" - and you don't care what the (anything) is – Jeff Schaller Jun 13 '16 at 17:30
3

Bash operator [ may not be what you are looking for; however, [[ does support =~. Try this:

[[ $(uname -r) =~ ^4\.4 ]] && echo yes
  • you beat me by seconds. :) – Jakuje Jun 13 '16 at 17:25
2

Depending on your use case (e.g.testing multiple values), you might find the "case" syntax a better fit.

case $(uname -r) in
   4.4*) echo 4.4 based kernel found ;;
  3.10*) echo 3.10 based kernel found ;;
      *) echo unknown kernel found
esac
1

use [[ :

ver=$(cut -d. -f1-2 <(uname -r))
[[ $ver =~ 3.2 ]] && echo "y"

OR use [:

ver=$(cut -d. -f1-2 <(uname -r))
[ $ver == 3.2 ] && echo "y"
1

Another way:

case $(uname -r) in (4.4.*) echo yes;; esac

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.