
Necesito un script que verifique si un kernel es superior a 4.1 y, si es así, ejecuta el script; si no, no lo hace. Por favor, ayuda. - Prajwal
Algo como esto pero con verificación del kernel.
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
Respuesta1
Prueba esto:
#!/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
Respuesta2
Esto debería hacer lo que quieras. Yo suelo uname -r
imprimir la versión del kernel, luego awk
dividirla en puntos y verificar los números de versión mayor y menor.
version_over_4_1
devolverá 1 o 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
Respuesta3
#! /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
}
Esto tiene la desventaja de llamar a awk
y uname
dos veces, pero puede ser más intuitivo que los métodos que no lo hacen.
#! /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
}
Esto llama awk
y uname
solo una vez cada uno, pero puede que no sea tan intuitivo como busca.
Respuesta4
# 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
Para cualquiera que todavía esté buscando esto.