Zadanie skriptu.

Napíš bash script, ktorý z výpisu spĺňa formát riadku alebo STDIN:

<server>: (<pocet> dbs)<disk> <total>G <used>G <free>G <perc>% <mount>

pre vypinac –full:

vypisu obsadenosti databazovych serverov vyberie tie, ktore maju zaplneny priestor na viac ako 75%

pre vypinac –count:

vypise pocet suborov

pre vypinac –validate:

vypise riadky, ktore splnaju strukturu do STDOUT a nevalidne riadky do STDERR

pre vypinac -h:

vypise help

Sprav script, tak aby vedel čítať súbor ako parameter, teda: $ ./script.sh <subor> ale zároveň aj ako STDIN: $ ./script.sh < cat subor

ukážkový súbor:

mysql1: (1016 dbs)/dev/xvda 62G 30G 29G 52% /

mysql2: (866 dbs)/dev/xvda 71G 36G 32G 54% /

mysql3: (1051 dbs)/dev/xvda 86G 43G 43G 50% /

mysql4: (648 dbs)/dev/sda1 58G 30G 25G 56% /

mysql5: (133 dbs)/dev/sda1 69G 68G 1.5G 98% /

mysql6: (1147 dbs)/dev/sda1 67G 43G 25G 64% /

mysql7: (705 dbs)/dev/sda1 67G 35G 30G 54% /

mysql8: (274 dbs)/dev/xvda 45G 29G 15G 67% /

mysql9: (222 dbs)/dev/xvda1 32G 18G 14G 56% /

mysql10: (289 dbs)/dev/xvda1 42G 23G 19G 55% /

mysql11: (1163 dbs)/dev/xvda1 37G 21G 16G 57% /

mysql12: (207 dbs)/dev/xvda 55G 32G 23G 59% /

mysql13: (175 dbs)/dev/xvda 32G 18G 13G 59% /

mysql14: (182 dbs)/dev/xvda 50G 28G 22G 57% /

mysql15: (188 dbs)/dev/xvda 30G 17G 12G 59% /

mysql16: (138 dbs)/dev/xvda 40G 34G 6.4G 84% /

mysql17: (178 dbs)/dev/xvda 40G 24G 17G 60% /

Vytvoríme si súbor

nano zadanieskriptu.sh

Napíšeme skript

#!/bin/bash

print_help() {
    echo "Usage: $0 [--full|--count|--validate|-h] [file]"
    echo "Options:"
    echo "  --full       Display database servers with more than 75% disk usage."
    echo "  --count      Count the number of lines in the input."
    echo "  --validate   Validate the input format. Valid lines to STDOUT, invalid to STDERR."
    echo "  -h           Display this help message."
}

process_input() {
    count=0
    while IFS= read -r line; do
        if [[ $1 == "--full" ]]; then
            if echo "$line" | grep -qP '^[^:]+: \([0-9]+ dbs\)[^ ]+ [0-9]+G [0-9]+G [0-9\.]+G [0-9]+% .+'; then
                perc=$(echo "$line" | grep -oP ' ([0-9]+)% ' | grep -oP '[0-9]+')
                if (( perc > 75 )); then
                    echo "$line"
                fi
            fi
        elif [[ $1 == "--count" ]]; then
            count=$((count+1))
        elif [[ $1 == "--validate" ]]; then
            if echo "$line" | grep -qP '^[^:]+: \([0-9]+ dbs\)[^ ]+ [0-9]+G [0-9]+G [0-9\.]+G [0-9]+% .+'; then
                echo "$line"
            else
                echo "$line" >&2
            fi
        fi
    done < "${2:-/dev/stdin}"

    if [[ $1 == "--count" ]]; then
        echo "$count"
    fi
}

if [[ $1 == "-h" ]]; then
    print_help
    exit 0
elif [[ $1 == "--full" || $1 == "--count" || $1 == "--validate" ]]; then
    process_input "$1" "$2"
else
    print_help
    exit 1
fi

Uložíme

Vytvoríme ďalší textový súbor

nano datafile.txt

Vložíme hodnoty

mysql1: (1016 dbs)/dev/xvda 62G 30G 29G 52% /
mysql2: (866 dbs)/dev/xvda 71G 36G 32G 54% /
mysql3: (1051 dbs)/dev/xvda 86G 43G 43G 50% /
mysql4: (648 dbs)/dev/sda1 58G 30G 25G 56% /
mysql5: (133 dbs)/dev/sda1 69G 68G 1.5G 98% /
mysql6: (1147 dbs)/dev/sda1 67G 43G 25G 64% /
mysql7: (705 dbs)/dev/sda1 67G 35G 30G 54% /
mysql8: (274 dbs)/dev/xvda 45G 29G 15G 67% /
mysql9: (222 dbs)/dev/xvda1 32G 18G 14G 56% /
mysql10: (289 dbs)/dev/xvda1 42G 23G 19G 55% /
mysql11: (1163 dbs)/dev/xvda1 37G 21G 16G 57% /
mysql12: (207 dbs)/dev/xvda 55G 32G 23G 59% /
mysql13: (175 dbs)/dev/xvda 32G 18G 13G 59% /
mysql14: (182 dbs)/dev/xvda 50G 28G 22G 57% /
mysql15: (188 dbs)/dev/xvda 30G 17G 12G 59% /
mysql16: (138 dbs)/dev/xvda 40G 34G 6.4G 84% /
mysql17: (178 dbs)/dev/xvda 40G 24G 17G 60% /

Nastavíme práva na spustenie:

chmod +x zadanieskriptu.sh

Spustíme script

./zadanieskriptu.sh --full datafile.txt

Výsledok

mysql5: (133 dbs)/dev/sda1 69G 68G 1.5G 98% /
mysql16: (138 dbs)/dev/xvda 40G 34G 6.4G 84% /