我需要一个脚本来检查内核是否超过 4.1,如果是就运行该脚本,如果不是就不运行,请帮忙。 - Prajwal
类似这样的但是带有内核检查
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
这具有两次调用和的缺点uname
,但可能比不这样做的方法更直观。
#! /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
}
awk
每次调用uname
仅一次,但可能并不像您所寻找的那样直观。
答案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
对于仍在寻找此内容的人来说。