Shell-Skript erforderlich, um zu prüfen, ob ein Kernel über 4.1 ist

Shell-Skript erforderlich, um zu prüfen, ob ein Kernel über 4.1 ist

Ich brauche ein Skript, das prüft, ob ein Kernel über 4.1 liegt. Wenn ja, führt es das Skript aus, wenn nicht, tut es das nicht. Bitte helfen Sie. – Prajwal

So etwas wie das hier, aber mit Kernelprüfung

  if ! [ -x "$(command -v iptables)" ]; then
    echo "Error: iptables is not installed, please install iptables." >&2
    exit
  fi
#!/bin/bash
version_above_4(){
    # check if $BASH_VERSION is set at all
    [ -z $BASH_VERSION ] && return 1

    # If it's set, check the version
    case $BASH_VERSION in 
        4.*) return 0 ;;
        ?) return 1;; 
    esac
}

if version_above_4
then
    echo "Good"
else
    echo "No good"
fi

Antwort1

Versuche dies:

#!/usr/bin/env bash
VERSION_LIMIT=4.1
CURRENT_VERSION=$(uname -r | cut -c1-3)
if (( $(echo "$CURRENT_VERSION > $VERSION_LIMIT" |bc -l) )); then
    echo " Kernel version: $CURRENT_VERSION > Version Limit: $VERSION_LIMIT "
    return 0
else
    echo " Kernel version: $CURRENT_VERSION < Version Limit: $VERSION_LIMIT "
    return 1
fi

Antwort2

Dies sollte das gewünschte Ergebnis liefern. Ich uname -rdrucke die Kernelversion aus, awkteile sie dann nach Punkten auf und überprüfe die Haupt- und Nebenversionsnummern. version_over_4_1gibt 1 oder 0 zurück.

#!/bin/bash

version_over_4_1(){
    return $(uname -r | awk -F '.' '{
        if ($1 < 4) { print 1; }
        else if ($1 == 4) {
            if ($2 <= 1) { print 1; }
            else { print 0; }
        }
        else { print 0; }
    }')
}

if version_over_4_1
then
    echo "Kernel > 4.1"
else
    echo "Kernel <= 4.1"
fi

Antwort3

#! /usr/bin/env bash

version_over_4_1(){
  MAJOR_VERSION=$(uname -r | awk -F '.' '{print $1}')
  MINOR_VERSION=$(uname -r | awk -F '.' '{print $2}')
  if [ $MAJOR_VERSION -ge 4 ] && [ $MINOR_VERSION -gt 1 ] || [ $MAJOR_VERSION -ge 5 ] ; then
    return 0
  else
    return 1
  fi
}

Dies hat den Nachteil, dass awkund unamezweimal aufgerufen werden, ist aber möglicherweise intuitiver als Methoden, bei denen dies nicht der Fall ist.

#! /usr/bin/env bash

version_over_4_1(){
  read MAJOR_VERSION MINOR_VERSION <<<$(uname -r | awk -F '.' '{print $1, $2}')
  if [ $MAJOR_VERSION -ge 4 ] && [ $MINOR_VERSION -gt 1 ] || [ $MAJOR_VERSION -ge 5 ] ; then
    return 0
  else
    return 1
  fi
}

Dies ruft awkjeweils unamenur einmal auf, ist aber möglicherweise nicht so intuitiv, wie Sie es suchen.

Antwort4

# Lets check the kernel version
function kernel-check() {
  CURRENT_KERNEL_VERSION=$(uname --kernel-release | cut --delimiter="." --fields=1-2)
  CURRENT_KERNEL_MAJOR_VERSION=$(echo "${CURRENT_KERNEL_VERSION}" | cut --delimiter="." --fields=1)
  CURRENT_KERNEL_MINOR_VERSION=$(echo "${CURRENT_KERNEL_VERSION}" | cut --delimiter="." --fields=2)
  ALLOWED_KERNEL_VERSION="3.1"
  ALLOWED_KERNEL_MAJOR_VERSION=$(echo ${ALLOWED_KERNEL_VERSION} | cut --delimiter="." --fields=1)
  ALLOWED_KERNEL_MINOR_VERSION=$(echo ${ALLOWED_KERNEL_VERSION} | cut --delimiter="." --fields=2)
  if [ "${CURRENT_KERNEL_MAJOR_VERSION}" -lt "${ALLOWED_KERNEL_MAJOR_VERSION}" ]; then
    echo "Error: Kernel ${CURRENT_KERNEL_VERSION} not supported, please update to ${ALLOWED_KERNEL_VERSION}."
    exit
  fi
  if [ "${CURRENT_KERNEL_MAJOR_VERSION}" == "${ALLOWED_KERNEL_MAJOR_VERSION}" ]; then
    if [ "${CURRENT_KERNEL_MINOR_VERSION}" -lt "${ALLOWED_KERNEL_MINOR_VERSION}" ]; then
      echo "Error: Kernel ${CURRENT_KERNEL_VERSION} not supported, please update to ${ALLOWED_KERNEL_VERSION}."
      exit
    fi
  fi
}

kernel-check

Für alle, die noch danach suchen.

verwandte Informationen