0

Following is the bash logic to compile and install Linux kernel if there is an update in the patch number in the Linux kernel version.

post_kernel_postinst() {
    # Extract the new and current kernal verisons
    NEW_KV=$(eselect kernel show | tail -n 1 | grep -oE '[0-9\.]{5,7}')
    CURRENT_KV=$(uname -r | grep -oE '[0-9\.]{5,7}')
  
    # Split the version strings into arrays
    IFS='.' read -r -a NEW_KV_ARR <<< "$NEW_KV"
    IFS='.' read -r -a CURRENT_KV_ARR <<< "$CURRENT_KV"

    # Access the patch version
    NEW_KERNEL_PATCH_VERSION="${NEW_KV_ARR[2]}"
    CURRENT_KERNEL_PATCH_VERSION="${CURRENT_KV_ARR[2]}"

    echo "NEW_KV $NEW_KV"
    echo "CURRENT_KV $CURRENT_KV"
    echo "NEW_KERNEL_PATCH_VERSION $NEW_KERNEL_PATCH_VERSION"
    echo "CURRENT_KERNEL_PATCH_VERSION $CURRENT_KERNEL_PATCH_VERSION"

    # Check if new kernel version is higher than the current kernel version
    if [[ $((NEW_KERNEL_PATCH_VERSION - CURRENT_KERNEL_PATCH_VERSION)) -ge 0 ]]; then
      echo "GoodBye!!"
    fi
}

The above bash script has been working for more than 2 years without any issues, but for the past 2 months or so, it's throwing a syntax error as below,

NEW_KV 6.14.3
CURRENT_KV 6.14.3
NEW_KERNEL_PATCH_VERSION 3
CURRENT_KERNEL_PATCH_VERSION 3
bash: 3: syntax error: invalid arithmetic operator (error token is "")

I haven't made any changes to the script for more than 2 years now.
Unable to figure out what change in my system is causing this.

18
  • 3
    Most likely one of your _PATCH_VERSION variables is picking up a stray character or byte (maybe something non-printable?). You could pre-declare those variables as integers and the script should fail earlier to give you a hint as to what's going on. Something along the lines of: declare -i NEW_KERNEL_PATCH_VERSION CURRENT_KERNEL_PATCH_VERSION Commented 2 days ago
  • 5
    @GreninjaShuriken4, a good place to start would be to run your script with set -x enabled, as with bash -x yourscript; that'll log each line as it's executed. You can make that log even more useful by running : 'NEW_KV_ARR=(' "${NEW_KV_ARR[@]}" ')' (a noop, but with trace logging enabled it prints the array's full contents) after your array assignment, and likewise for the other one. Commented 2 days ago
  • 4
    Side note: in most regex dialects, including POSIX's, you do not need to escape a . that appears in a character class. In fact, a character class containing just . is my favorite way to represent a literal . -- even though it's one character longer, I find [.] easier to read than \., especially when there's a lot of other escaping going on, or where \ needs to be escaped itself. Commented 2 days ago
  • 4
    Try reducing this to the minimal code to reproducing the problem. I think the error is happening in $((NEW_KERNEL_PATCH_VERSION - CURRENT_KERNEL_PATCH_VERSION)), so get rid of everything else around that to confirm.
    – Barmar
    Commented 2 days ago
  • 3
    And if you're going to use bashisms like [[, why not go all the way with if ((NEW_KERNEL_PATCH_VERSION > CURRENT_KERNEL_PATCH_VERSION))?
    – Barmar
    Commented 2 days ago

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.