需要 Shell 腳本來檢查核心是否高於 4.1

需要 Shell 腳本來檢查核心是否高於 4.1

我需要一個腳本來檢查核心是否超過 4.1,如果是,則運行該腳本,如果不是,則不運行,請幫忙。 - 普拉吉瓦爾

類似這樣的東西,但帶有內核檢查

  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

答案1

嘗試這個:

#!/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

答案2

這應該要做你想做的事。我用來uname -r列印核心版本,然後awk按句點分割並檢查主要版本號和次要版本號。 version_over_4_1將返回 1 或 0。

#!/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

答案3

#! /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
}

awk這有呼叫and兩次的缺點uname,但可能比不呼叫 and 的方法更直觀。

#! /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
}

這只會調用一次awkuname,但可能不如您所尋找的那麼直觀。

答案4

# 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

對於任何仍在尋找這個的人。

相關內容