liveBook · Manning

Shell script

As explained in AI for Everyday IT

Bash is a Unix shell and command language used to execute commands and automate operating-system tasks.

A shell script automates commands that would otherwise be run by hand. For system administrators, a Bash script can check kernel versions across multiple Linux servers, avoiding repeated logins and manual checks. section 9.3

Generative AI can help draft scripts for common administration tasks, such as finding today’s files or exporting logs to Splunk. For a useful draft, specify the inputs, commands, output, and access arrangements. section 1.1 section 9.3

Request a multiserver kernel check

Ask ChatGPT: “Write a Bash script that reads server addresses from a file, checks each server’s kernel version, and writes whether it is lower, equal to, or higher than an expected version to an output file. The management server uses key pairs, so no password is required.” The proposed script uses uname -r to retrieve each version and a function to compare it with the expected version. section 9.3

#!/bin/bash

# Define the file containing the list of servers
SERVER_LIST='servers.txt'

# Expected kernel version
EXPECTED_KERNEL_VERSION='4.18.0-513.5.1.el8_9'

# Output file where results will be stored
OUTPUT_FILE='kernel_check_results.txt'

# Function to compare kernel versions
# Returns 0 if equal, 1 if first is higher, 2 if second is higher
compare_kernel_versions() {
    # Using sort with version sort and tail to get the highest version
    higher_version=$(echo -e "$1\n$2" | sort -V | tail -1)
    if [ "$higher_version" == "$1" ] && [ "$higher_version" != "$2" ]; 
    then
        return 1 # First is higher
    elif [ "$higher_version" != "$1" ] && [ "$higher_version" == "$2" ];
    then
        return 2 # Second is higher
    else
        return 0 # Versions are equal
    fi
}

# Check if the SERVER_LIST file exists
if [ ! -f "$SERVER_LIST" ]; then
    echo "Server list file $SERVER_LIST not found."
    exit 1
fi

# Empty or create the output file
> "$OUTPUT_FILE"

# Read each line in SERVER_LIST as a server address
while IFS= read -r server; do
    echo "Processing $server..."
    # Get the kernel version of the server
    server_kernel_version=$(ssh -o BatchMode=yes -o ConnectTimeout=5
    "$server" 'uname -r')

    # Compare the server's kernel version with the expected version
    compare_kernel_versions "$server_kernel_version"
    "$EXPECTED_KERNEL_VERSION"
    comparison_result=$?
    # Append comparison result to the output file
    case $comparison_result in
        0) echo "$server: Kernel version is equal to the expected (
           $EXPECTED_KERNEL_VERSION)." >> "$OUTPUT_FILE";;
        1) echo "$server: Kernel version is higher than expected (
           $server_kernel_version > $EXPECTED_KERNEL_VERSION)." >> 
           "$OUTPUT_FILE";;
        2) echo "$server: Kernel version is lower than expected (
           $server_kernel_version < $EXPECTED_KERNEL_VERSION)." >> 
           "$OUTPUT_FILE";; 
    esac
done < "$SERVER_LIST"

echo "Script execution completed. Check $OUTPUT_FILE for results."

The listing sets the server-list, expected-version, and output-file variables and defines a comparison function using version sorting. section 9.3

Before using the script, populate servers.txt with server addresses and set EXPECTED_KERNEL_VERSION to the version you want to check. Review the generated script and verify its results before relying on it across servers. The same approach can be adapted for tasks such as monitoring disk usage or updating packages. section 9.3

How AI for Everyday IT covers it

How deep it goes
Works through script requirements, configuration, and a version-comparison function without teaching Bash syntax systematically.
Examples
Uses a kernel-version report and mentions log export, finding files, and disk-usage monitoring.
What it assumes
Assumes a management server with key pairs and a file containing server addresses.
What you can do after
Apply it via ChatGPT to draft and configure a Bash script for checking kernel versions across servers.
How it connects
Connects scripting with generative AI, SSH, and kernel-version comparison.

Also explained in

Compare how 6 books explain shell script →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of AI for Everyday IT. The section links point to the parts of the book it draws on.