Home

markdown notes

Bash Scripting

directory structure

logs
|-- application.log
|-- system.log
cd logs
cat application.log                 # prints the whole file content
grep "ERROR" application.log        # filters the lines with ERROR text
grep -c "ERROR" application.log     # count of occurences after filtering
echo -e "\nNew line added"          # prints \n as -e
find . -name "*.log" -mtime -1
touch analyse-logs.sh   # creates file
vim analyse-logs.sh     # terminal text editor
# press i to go to insert mode
:wq # write and quit
chmod +x analyse-logs.sh

Shebang

all have .sh - how does os know if script is for bash, zsh or other shell implementation?
#! tells the shell script

Variables

LOG_DIR="/Users/arpit/logs"
APP_LOG_FILE="application.log"
SYS_LOG_FILE="system.log"
ERROR_PATTERNS=("ERROR" "FATAL" "CRITICAL")

cat "$APP_LOG_FILE"                 # prints the whole file content
grep "ERROR" "$LOG_DIR/$APP_LOG_FILE"        # filters the lines with ERROR text
grep -c "${ERROR_PATTERNS[0]}" "$APP_LOG_FILE"     # count of occurences after filtering
echo -e "\nNew line added"          # prints \n as -e

Command Substitution

LOG_FILES=$(find $LOG_DIR -name "*.log" -mtime -1)
echo $LOG_FILES

for Loops and if conditions

LOG_DIR="/Users/arpit/logs"
APP_LOG_FILE="application.log"
SYS_LOG_FILE="system.log"

ERROR_PATTERNS=("ERROR" "FATAL" "CRITICAL")

echo -e "\nList of log files updated in last 24 hours"
LOG_FILES=$(find $LOG_DIR -name "*.log" -mtime -1)
echo "$LOG_FILES"

for LOG_FILE in $LOG_FILES; do
    echo -e "\n"
    echo "====================================="
    echo "====== log file - ${LOG_FILE} ======"
    echo "====================================="

    for PATTERN in ${ERROR_PATTERNS[@]}; do
        echo -e "\nSearching $PATTERN"
        grep "$PATTERN" "$LOG_FILE"

        echo -e "\nnumber of $PATTERN found"
        ERROR_COUNT=$(grep -c "$PATTERN" "$LOG_FILE")
        echo "$ERROR_COUNT"

        if [ "$ERROR_COUNT" -lt 10 ]; then
            echo -e "\nWarning!! Less than 10 $PATTERN issues in log file $LOG_FILE"
        elif [ "$ERROR_COUNT" -gt 10 ]; then
            echo -e "\nAction Required!! Too many $PATTERN issues in log file $LOG_FILE"
        fi
    done
done

Redirection Operators > and >>

OUT_FILE="analysis.txt"
echo -e "\nList of log files updated in last 24 hours" > "$OUT_FILE"
LOG_FILES=$(find $LOG_DIR -name "*.log" -mtime -1)
echo "$LOG_FILES" >> "$OUT_FILE"