Scripting & Automation
Regular Expressions
Basic Syntax: Character Classes: Quantifiers: Common Patterns: Demonstration: Regular expressions (regex) provide a powerful way to search and manipulate strings using a declarative syntax. They are used for pattern matching and text processing in various programming and scripting languages. Understanding basic regex syntax is crucial for performing complex text searches and manipulations efficiently. These patterns form the foundation of regex and are essential for various scripting and programming tasks.
. # Matches any single character except newline
# Example: grep 'h.t' file.txt to find 'hat', 'hot', etc.
^ # Matches the start of a string
# Example: grep '^start' file.txt to find lines starting with 'start'
$ # Matches the end of a string
# Example: grep 'end$' file.txt to find lines ending with 'end'
[abc] # Matches any one character from the set {a, b, c}
# Example: grep '[abc]' file.txt to find lines with 'a', 'b', or 'c'
[^abc] # Matches any one character not in the set {a, b, c}
# Example: grep '[^abc]' file.txt to find lines without 'a', 'b', or 'c'
\d # Matches any decimal digit; equivalent to [0-9]
# Example: grep '\d' file.txt to find lines with digits
\D # Matches any non-digit character; equivalent to [^0-9]
# Example: grep '\D' file.txt to find lines without digits
\w # Matches any alphanumeric character; equivalent to [a-zA-Z0-9_]
# Example: grep '\w' file.txt to find lines with word characters
\W # Matches any non-alphanumeric character; not equivalent to [^a-zA-Z0-9_]
# Example: grep '\W' file.txt to find lines with non-word characters
\s # Matches any whitespace character; equivalent to [ \t\r\n\f]
# Example: grep '\s' file.txt to find lines with whitespace characters
\S # Matches any non-whitespace character; equivalent to [^ \t\r\n\f]
# Example: grep '\S' file.txt to find lines without whitespace characters
* # Matches zero or more repetitions of the preceding element
# Example: grep 'a*' file.txt to find lines with zero or more 'a's
+ # Matches one or more repetitions of the preceding element
# Example: grep 'a+' file.txt to find lines with one or more 'a's
? # Matches zero or one repetition of the preceding element
# Example: grep 'a?' file.txt to find lines with zero or one 'a'
{n} # Matches exactly n occurrences of the preceding element
# Example: grep 'a{2}' file.txt to find lines with two consecutive 'a's
{n,} # Matches n or more occurrences of the preceding element
# Example: grep 'a{2,}' file.txt to find lines with two or more consecutive 'a's
{n,m} # Matches between n and m occurrences of the preceding element
# Example: grep 'a{2,4}' file.txt to find lines with 2 to 4 consecutive 'a's
^\d+ # Matches a line beginning with one or more digits
# Example: grep '^\d+' file.txt to find lines starting with numbers
\w+$ # Matches a line ending with one or more word characters
# Example: grep '\w+$' file.txt to find lines ending with words
\b\w+\b # Matches whole words only
# Example: grep '\b\w+\b' file.txt to find complete words
# Demonstration 1: Using basic regex patterns with `grep`
echo "find all lines that start with a number" | grep '^[0-9]'
echo "exclude lines that start with a hashtag" | grep -v '^#'
echo "list files ending with .txt" | ls | grep '\.txt$'
echo "select words starting with 'a'" | grep '\<a\w*\>'
echo "find empty lines" | grep '^$'
# Demonstration 2: Using basic regex in `sed`
echo "replace 'cat' with 'dog'" | sed 's/cat/dog/'
echo "delete digits from input" | sed 's/[0-9]//g'
echo "capitalize words starting with 'm'" | sed 's/\bm\(\w*\)/\u&m/g'
echo "delete all whitespace" | sed 's/\s//g'
echo "append 'end' at the end of each line" | sed 's/$/ end/'
# Demonstration 3: Using basic regex in `awk`
echo "extract first word of each line" | awk '{print $1}'
echo "delete first word of each line" | awk '{$1=""; print $0}'
echo "print lines longer than 20 characters" | awk 'length($0) > 20'
echo "print lines where the second field is greater than 100" | awk '$2 > 100'
echo "replace commas with semicolons" | awk '{gsub(/,/, ";"); print}'
Advanced Matching: Lookaheads and Lookbehinds: Common Patterns: Detailed Examples: Demonstration: This intermediate regex guide provides more complex examples and common patterns used in data validation and parsing tasks. Understanding these patterns is essential for efficient text manipulation and validation in various applications.
(abc) # Captures a group. Matches the characters abc and saves them as a group.
# Example: grep '(abc)' file.txt to find lines with 'abc'
\1 # Backreference to the first captured group. Matches the same text as previously matched by the first group.
# Example: grep '\(abc\).*\1' file.txt to find lines with 'abc' followed by the same text
[a-z] # Matches any lowercase letter from a to z.
# Example: grep '[a-z]' file.txt to find lines with lowercase letters
[^a-z] # Matches any character that is not a lowercase letter.
# Example: grep '[^a-z]' file.txt to find lines without lowercase letters
(a|b) # Matches either a or b.
# Example: grep '(yes|no)' file.txt to find lines with 'yes' or 'no'
(?:abc) # Non-capturing group. Matches the characters abc but does not capture the group.
# Example: grep '(?:abc)' file.txt to find 'abc' without capturing it
\1 # Backreference to the first captured group to match the same text as previously matched by the first group.
# Example: grep '\(abc\).*\1' file.txt to find lines with 'abc' followed by the same text
^\w+@\w+$ # Matches email-like patterns, ensuring they start with word characters, followed by an '@' and more word characters.
# Example: grep '^\w+@\w+$' file.txt to find email-like patterns
(?=abc) # Positive lookahead. Asserts that what immediately follows the current position in the string is abc.
# Example: grep 'foo(?=bar)' file.txt to find 'foo' followed by 'bar'
(?!abc) # Negative lookahead. Asserts that what immediately follows the current position is not abc.
# Example: grep 'foo(?!bar)' file.txt to find 'foo' not followed by 'bar'
(?<=abc) # Positive lookbehind. Asserts that what immediately precedes the current position in the string is abc.
# Example: grep '(?<=abc)def' file.txt to find 'def' preceded by 'abc'
(?<!abc) # Negative lookbehind. Asserts that what immediately precedes the current position is not abc.
# Example: grep '(?<!abc)def' file.txt to find 'def' not preceded by 'abc'
\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3} # Matches IP addresses like 192.168.1.1
[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,} # Matches email addresses
\+?\d{1,3}[-.\s]?\(?\d{1,3}\)?[-.\s]?\d{1,4}[-.\s]?\d{1,4}[-.\s]?\d{1,9} # International phone numbers w. optional country code
^\d+$ # Matches lines that contain only digits.
\b\w{6}\b # Matches exactly six alphanumeric characters surrounded by word boundaries.
\d+,? # Matches one or more digits followed by an optional comma.
\d{2,4} # Matches between 2 and 4 digits.
\w+@\w+\.\w+ # Basic pattern for matching email addresses.
https?://(?:www\.)?\w+\.\w+ # Matches HTTP and HTTPS URLs.
^\s+|\s+$ # Matches leading and trailing whitespace.
"([^"]*)" # Matches a string inside double quotes.
\b\d{2}-\d{2}-\d{4}\b # Matches dates in the format DD-MM-YYYY.
[a-zA-Z]+:\/\/[^\s]* # Matches URLs like http://example.com.
\.(jpg|png|gif)$ # Matches file extensions for images.
\d{3}-\d{2}-\d{4} # Matches a Social Security number like 123-45-6789.
\b([01]?\d|2[0-3]):([0-5]?\d)\b # Matches time in HH:MM format, 24-hour clock.
[^\w\s] # Matches any non-word, non-space character.
\b[aeiouAEIOU] # Matches any word beginning with a vowel.
[^b]at # Matches 'at' preceded by any character except 'b'.
\d{5}-\d{4} # Matches U.S. ZIP+4 codes like 90210-1234.
(?i)hello # Case-insensitive matching of 'hello'.
\b(?!\d+\b)\w+ # Matches whole words that do not consist solely of digits.
# Demonstration 1: Using intermediate regex patterns with `grep`
# Using -P Perl-compatible regex to match email addresses, -o flag to print only the matched text.
echo "match email addresses" | grep -Po '[\w.%+-]+@[\w.-]+\.[a-zA-Z]{2,6}'
echo "extract all IP addresses" | grep -Po '\b(?:\d{1,3}\.){3}\d{1,3}\b'
echo "find lines with exactly three words" | grep -P '^\w+\s\w+\s\w+$'
echo "capture words after 'user:'" | grep -Po '(?<=user:)\w+'
echo "match multiline patterns" | grep -Pzo '(?s)start.*?end'
# Demonstration 2: Using intermediate regex in `sed`
echo "rename file extensions from .htm to .html" | sed -r 's/\.htm$/\.html/'
echo "swap first two words in a line" | sed -r 's/^(\w+)\s(\w+)/\2 \1/'
echo "mask first part of email" | sed -r 's/([\w.%+-]+)@/\*\*\*@/'
echo "delete comments from code" | sed -r '/^\s*#.*$/d'
echo "convert CSV to tab-separated" | sed -r 's/,/\t/g'
# Demonstration 3: Using intermediate regex with `awk`
echo "validate date format MM/DD/YYYY" | awk '/^\d{2}\/\d{2}\/\d{4}$/'
echo "sum the numbers in text" | awk '{s=0; while(match($0, /[0-9]+/)) {s+=substr($0, RSTART, RLENGTH); $0=substr($0, RSTART+RLENGTH);} print s}'
echo "extract last field after delimiter ':'" | awk -F':' '{print $NF}'
echo "split lines into columns by comma" | awk -F, '{print $1, $2, $3}'
echo "filter lines where field 5 is not 'NULL'" | awk '$5 != "NULL"'
Advanced Regex with Command-line Tools: Combining grep with sed and awk: Using regex with find: Complex Grep Examples: Regex in log analysis: Advanced Pattern Matching: This advanced regex guide demonstrates the use of regex in combination with other Unix command-line tools to perform sophisticated text processing tasks, highlighting their applicability in data extraction, log analysis, and system administration.
grep -P '^\d{3}-\d{2}-\d{4}$' file.txt # Uses Perl-compatible regex to match Social Security numbers formatted as 123-45-6789 in a file.
echo "test data" | grep -oP '\b\w+\b' # Prints each word on a new line using Perl regex.
grep -P '^(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,}$' users.txt # Matches passwords that contain at least one digit, one lowercase, one uppercase letter, and are at least 8 characters long.
grep -P '\b\d{3}-\d{3}-\d{4}\b' file.txt | sed 's/-/./g' # Finds phone numbers and replaces dashes with dots.
grep -Po 'https?://\S+' web.log | awk '{print $1}' | sort | uniq -c # Extracts URLs, prints them, sorts, and counts unique occurrences.
grep -Po '(?<=id=")\d+' file.html # Extracts numeric IDs following 'id="' using a positive lookbehind.
grep -Po 'class="[^"]+"' file.css | cut -d'"' -f2 # Extracts class names from a CSS file.
Scripting Intro & Concepts
Introduction to Bash Scripting: Variables and Data Types: Control Structures: Basic Functions: Combining Scripts and Functions: Loops, Conditions, and Case Statements: This guide provides a foundation for bash scripting, introducing you to scripts, variables, control structures, and functions. It aims to equip you with the basics needed to start writing useful bash scripts for automation and system management.
#!/bin/bash
# This line is called a shebang. It tells the system this file is a bash script.
echo "Hello, World!" # Prints "Hello, World!" to the console.
greeting="Welcome to bash scripting!"
user=$(whoami) # Command substitution, captures the output of 'whoami'.
echo "$greeting, $user!" # Displays the greeting with the user name.
# If statement
if [ "$user" == "root" ]; then
echo "You are the root user."
else
echo "You are not the root user."
fi
# For loop
for i in {1..5}
do
echo "Iteration $i"
done
# Defining a function
function greet {
echo "Hello, $1" # $1 is a positional parameter, representing the first argument passed to the function.
}
# Calling a function
greet "Visitor"
Parameter Passing and User Input: Advanced Function Usage: Conditional Execution: This intermediate bash scripting guide deepens your understanding of parameter handling, user interaction, and advanced function definitions, essential for writing more complex scripts and managing runtime conditions effectively.
#!/bin/bash
# Accessing passed arguments with special variables.
echo "Script Name: $0" # Displays the script's filename.
echo "First Parameter: $1" # Displays the first passed parameter.
echo "Second Parameter: $2" # Displays the second passed parameter.
echo "All Parameters: $@" # Displays all passed parameters.
echo "Total Number of Parameters: $#" # Displays the number of parameters passed.
# Reading user input during script execution.
read -p "Enter your name: " name # Prompts the user to enter their name.
echo "Hello, $name!" # Greets the user with the entered name.
# A function that calculates the sum of two numbers.
function add {
local sum=$(( $1 + $2 )) # Adds the first and second arguments passed to the function.
echo "Sum: $sum" # Outputs the sum.
}
# Calling the function with user-provided arguments.
add 5 7 # Calls the add function with 5 and 7 as arguments.
# A function to check if a file exists.
function check_file {
local file="$1" # Takes the first argument as the filename.
if [ -e "$file" ]; then # Checks if the file exists.
echo "File exists."
else
echo "File does not exist."
fi
}
# Using the function to check for a specific file.
check_file "/path/to/your/file.txt" # Replace with the actual file path you want to check.
# Using logical conditions with if statements.
if [ $1 -gt $2 ]; then # Checks if the first parameter is greater than the second.
echo "$1 is greater than $2"
elif [ $1 -eq $2 ]; then # Checks if the parameters are equal.
echo "$1 is equal to $2"
else
echo "$1 is less than $2"
fi
# Using a case statement to respond based on user input.
read -p "Do you like bash scripting? (yes/no): " answer # Asks the user if they like bash scripting.
case $answer in
yes|YES|Yes)
echo "That's great!"
;;
no|NO|No)
echo "That's okay, it's not for everyone."
;;
*)
echo "Please answer yes or no."
;;
esac
Advanced Data Structures and Script Optimization: Complex Function Definitions: Signal Handling and Script Debugging: This advanced bash scripting guide covers complex data structures, deeper function usage, signal handling, and debugging techniques. These concepts are pivotal for developing robust, efficient, and maintainable bash scripts that handle complex tasks.
#!/bin/bash
# Working with arrays.
fruits=('apple' 'banana' 'cherry') # Defines an array of fruits.
echo "${fruits[0]}" # Accesses the first element of the array.
fruits[3]='orange' # Adds another element to the array.
echo "${fruits[@]}" # Prints all elements of the array.
echo "${#fruits[@]}" # Prints the number of elements in the array.
# Associative arrays (hash maps).
declare -A capitals # Declares an associative array.
capitals["France"]="Paris" # Adds an element to the associative array.
capitals["Germany"]="Berlin" # Adds another element.
for country in "${!capitals[@]}"; do # Iterates over keys of the associative array.
echo "The capital of $country is ${capitals[$country]}"
done
# A function that uses recursion to calculate factorial.
function factorial {
local num=$1 # Local scope for the function's argument.
if [ $num -le 1 ]; then # Base case: factorial of 1 or 0 is 1.
echo 1
else
echo $(( num * $(factorial $((num - 1))) )) # Recursion step.
fi
}
echo "Factorial of 5: $(factorial 5)" # Calls the factorial function.
# A function to process text files.
function process_files {
local file=$1
while read line; do # Reads a file line by line.
echo "Processing: $line"
done < "$file"
}
# Trap signals.
trap 'echo "Signal SIGHUP received";' SIGHUP # Traps SIGHUP signal.
trap 'echo "Exiting..."; exit;' SIGINT # Traps SIGINT signal and exits.
# Set options for script debugging.
set -e # The script will exit if any commands fail.
set -u # The script will exit if it tries to use uninitialized variables.
set -x # Prints commands and their arguments as they are executed.
set -o pipefail # Causes a pipeline to return the exit status of the last command in the pipe that failed.
# Debugging example.
debug_function() {
echo "Starting debug..."
set -x # Enable debugging.
local temp=$1
echo "Value: $temp"
set +x # Disable debugging.
}
debug_function "Test"
Script Examples & Cron Jobs
System Health Check Script: Backup and Restore Script: Network Scanner Script: These scripts are tailored for real-world applications in DevOps, engineering, and cybersecurity. They provide practical solutions for system monitoring, data management, and network scanning to enhance operational efficiency and security.
#!/bin/bash
# This script checks the health of your server by inspecting disk usage, load average, and system uptime.
echo "Checking disk usage..."
df -h | grep -vE '^Filesystem|tmpfs|cdrom' | awk '{ print $5 " " $1 }' | while read output;
do
echo $output
usep=$(echo $output | awk '{ print $1}' | cut -d'%' -f1 )
partition=$(echo $output | awk '{ print $2 }' )
if [ $usep -ge 90 ]; then
echo "Running out of space \"$partition ($usep%)\" on $(hostname) as on $(date)"
fi
done
echo "Checking load average..."
uptime | cut -d ':' -f 5
echo "Checking system uptime..."
uptime | awk '{print $3,$4}' | cut -f1 -d,
#!/bin/bash
# This script creates a compressed backup of a specified directory and can restore it.
function backup {
tar czf "/backup/$(date +%Y%m%d_%H%M%S)_$1.tar.gz" $1
echo "Backup of $1 completed successfully."
}
function restore {
tar xzf $1 -C $2
echo "Restore completed successfully."
}
case $1 in
backup)
backup $2
;;
restore)
restore $2 $3
;;
*)
echo "Usage: $0 {backup|restore} [source] [target]"
;;
esac
Scheduling Tasks with Cron: Common Cron Commands: Cron Job Examples: Cron jobs are essential for automating repetitive tasks, system maintenance, and scheduled operations. Understanding cron syntax and common commands enables you to efficiently manage and schedule tasks on Unix-based systems.
# Cron job syntax: minute hour day month day_of_week command
# Example: Run a script every day at 3:30 AM
30 3 * * * /path/to/script.sh
# Example: Run a script every Monday at 8:00 PM
0 20 * * 1 /path/to/script.sh
# Example: Run a script every 15 minutes
*/15 * * * * /path/to/script.sh
# Example: Run a script at 2:00 AM on the first day of every month
0 2 1 * * /path/to/script.sh
crontab -e # Edit the current user's crontab file.
crontab -l # List the current user's crontab entries.
crontab -r # Remove all crontab entries for the current user.
crontab -u username -l # List crontab entries for a specific user.
crontab -u username -e # Edit crontab entries for a specific user.
# Backup script running daily at midnight
0 0 * * * /path/to/backup.sh
# Log cleanup script running every Sunday at 3:00 AM
0 3 * * 0 /path/to/cleanup.sh
# System update script running every Friday at 2:00 AM
0 2 * * 5 /path/to/update.sh
# Monitoring script running every 15 minutes
*/15 * * * * /path/to/monitor.sh
# Cron job to restart a service every hour
0 * * * * systemctl restart some-service
# Monitoring script that checks the health of a web server every 5 minutes
*/5 * * * * /path/to/server_health_check.sh
# Clean temporary files every day at midnight
0 0 * * * rm -rf /tmp/*
Advanced Scripting Examples: Monitor and Log Network Latency Security Audit Script Example: These advanced scripting examples demonstrate practical use cases for monitoring system resources, network latency, and automating tasks. They showcase the versatility of scripting for system administration, monitoring, and alerting in real-world scenarios.
# Script to monitor system resources and send an email alert if thresholds are exceeded.
#!/bin/bash
cpu_threshold=90
mem_threshold=80
disk_threshold=90
cpu_usage=$(top -bn1 | grep "Cpu(s)" | sed "s/.*, *\([0-9.]*\)%* id.*/\1/" | awk '{print 100 - $1}')
mem_usage=$(free | awk '/Mem/{printf("%.2f"), $3/$2*100}')
disk_usage=$(df | awk '$NF=="/"{printf("%.2f"), $5}')
if (( $(echo "$cpu_usage > $cpu_threshold" | bc -l) )); then
echo "CPU usage is above threshold: $cpu_usage%" | mail -s "High CPU Usage Alert"
fi
if (( $(echo "$mem_usage > $mem_threshold" | bc -l) )); then
echo "Memory usage is above threshold: $mem_usage%" | mail -s "High Memory Usage Alert"
fi
if (( $(echo "$disk_usage > $disk_threshold" | bc -l) )); then
echo "Disk usage is above threshold: $disk_usage%" | mail -s "High Disk Usage Alert"
fi
#!/bin/bash
# Script to monitor and log network latency
while true; do
ping -c 1 google.com | grep 'time=' | awk '{print $7}' >> latency.log
sleep 300 # Wait for 5 minutes
done
# Automatically ssh into a server and run commands
ssh user@server "uptime; df -h"
# Download files from a list of URLs
cat urls.txt | xargs -n 1 wget
# Send an alert if a server is not responding
ping -c 3 example.com || echo "Server down!" | mail -s "Server Down Alert" admin@example.com
#!/bin/bash
# Find all .sh files and check their permissions
find / -type f -name "*.sh" -exec ls -l {} \; | awk '$1 !~ /^-rwxr-xr-x$/ {print $9}'
# Check for unauthorized SSH access attempts
grep "Failed password" /var/log/auth.log | awk '{print $11}' | sort | uniq -c | sort -nr
# Scan for open ports on a local machine
netstat -tuln | grep LISTEN
# List all users with UID 0 (root privileges)
awk -F: '$3 == 0 {print $1}' /etc/passwd
# Check for no-password sudoers
grep NOPASSWD /etc/sudoers