Detailed description of shell scripting in DevOps cycle
This repository contains a comprehensive collection of shell scripts designed to help developers and DevOps engineers understand and master shell scripting concepts, from basic fundamentals to advanced automation tasks. These scripts are structured progressively, making them ideal for learning shell scripting in the context of DevOps workflows.
The repository contains 15 numbered shell scripts, each focusing on specific concepts:
- Purpose: Hello World introduction script
- Concepts: Basic script structure, echo command
- Usage:
bash 01_helloworld.sh
- Purpose: Introduction to echo without variables
- Concepts: Basic echo output
- Output: Prints a conversation between two people
- Purpose: Working with basic variables
- Concepts: Variable declaration and usage, variable expansion
- Key Features:
- Define variables:
PERSON1="Mani" - Use variables in echo:
echo "$PERSON1: Hi $PERSON2"
- Define variables:
- Usage:
bash 03_variables.sh
- Purpose: Command-line arguments as variables
- Concepts: Positional parameters ($1, $2, etc.)
- Key Features:
- Access script arguments using
$1and$2 - Parameters are passed when executing the script
- Access script arguments using
- Usage:
bash 04_variables.sh Mani Trinadh
- Purpose: User input handling
- Concepts: Reading user input, silent input capture
- Key Features:
readcommand for user input-sflag for silent input (passwords)
- Usage:
bash 05_variables.sh
- Purpose: Working with different data types and operations
- Concepts: Arithmetic operations, command substitution
- Key Features:
- Arithmetic addition:
SUM=$((NUMBER1+NUMBER2)) - Command substitution:
DATE=$(date) - Variable calculations
- Arithmetic addition:
- Usage:
bash 06_datatypes.sh 10 20
- Purpose: Working with arrays
- Concepts: Array declaration, array indexing, array size
- Key Features:
- Declare arrays:
NAMES=("ALEX" "JAMES" "VRINDA" "NAVYA") - Get array size:
SIZE=${#NAMES[@]} - Access elements:
${NAMES[0]},${NAMES[1]} - Access last element:
${NAMES[$SIZE-1]}
- Declare arrays:
- Usage:
bash 07_arrays.sh
- Purpose: Understanding special variables
- Concepts: Built-in variables in bash
- Key Features:
$@- All arguments passed to script$#- Number of arguments$0- Script name$PWD- Present working directory$HOME- Home directory of current user$USER- Current user running the script$$- Process ID of current script$!- Process ID of last command
- Usage:
bash 08_specialvariables.sh arg1 arg2 arg3
- Purpose: Conditional statements and control flow
- Concepts: if/elif/else conditions, numeric comparisons
- Key Features:
if [ $NUMBER -gt 100 ]- Greater than comparisonelif [ $NUMBER -eq 100 ]- Equal to comparisonelse- Default case
- Usage:
bash 09_conditions.sh 150
- Purpose: Package installation with error checking
- Concepts: Root user validation, package management, exit codes
- Key Features:
- Check if user is root:
if [ $USER -ne 0 ] - Check package installation:
dnf list installed mysql - Error checking:
if [ $? -ne 0 ] - Install packages:
dnf install mysql -y - Exit on errors:
exit 1
- Check if user is root:
- Usage:
sudo bash 10_installscripts.sh - Requirements: Root privileges
- Purpose: Reusable functions for validation
- Concepts: Function definition, function calls, parameter passing
- Key Features:
- Define functions:
VALIDATE() { ... } - Call functions with parameters:
VALIDATE $? - Code reusability and error handling
- Define functions:
- Usage:
sudo bash 11_functions.sh - Requirements: Root privileges
- Purpose: Colored output for better readability
- Concepts: ANSI color codes, enhanced user feedback
- Key Features:
- Red color:
R="\e[31m" - Green color:
G="\e[32m" - Yellow color:
Y="\e[33m" - Use with echo:
echo -e "$R ERROR::Message" - Enhances visibility of error/success messages
- Red color:
- Usage:
sudo bash 12_colors.sh - Requirements: Root privileges
- Purpose: Logging script execution
- Concepts: Log file management, output redirection, logging best practices
- Key Features:
- Create logs folder:
/var/log/shell-scriptlogs - Generate unique log file names with timestamp:
LOG_FILE_NAME="$LOGS_FOLDER/$LOG_FILE-$TIMESTAMP.log" - Redirect output to log:
&>>$LOG_FILE_NAME - Timestamp format:
TIMESTAMP=$(date +%Y-%m-%d-%H-%M-%s) - Combines colors, functions, and logging
- Create logs folder:
- Usage:
sudo bash 13_logs.sh - Requirements: Root privileges
- Output: Logs stored in
/var/log/shell-scriptlogs/
- Purpose: Delete old log files
- Concepts: File finding, log rotation, deletion operations
- Key Features:
- Find old files:
find . -name "*.log" -mtime +14 - Delete files matching criteria:
rm -rf $file - Read file list using while loop
- Cleanup old logs to save disk space
- Find old files:
- Usage:
sudo bash 14_deletelogs.sh - Requirements: Root privileges
- Caution: Script deletes files, use with care
- Purpose: Backup old logs before deletion
- Concepts: Argument validation, conditional logic, zip operations
- Key Features:
- Accept command-line arguments:
SOURCE_DIR=$1 DEST_DIR=$2 DAYS=${3:-14} - Default parameter value:
${3:-14}(default 14 days if not provided) - Validate arguments:
if [ $# -lt 2 ] - Check directory existence:
if [ ! -d $SOURCE_DIR ] - Find files older than X days:
find $SOURCE_DIR -name "*.log" -mtime +$DAYS - Create zip archive:
zip -@ "$ZIP_FILE" - Delete after backup: Safe log cleanup with backup
- Accept command-line arguments:
- Usage:
bash 15_backuplogs.sh <SOURCE_DIR> <DEST_DIR> [DAYS]- Example:
bash 15_backuplogs.sh /home/ec2-user/app-logs /home/ec2-user/backups 14
- Example:
- Optional Parameters: Days parameter defaults to 14 if not provided
- Bash shell (usually pre-installed on Linux/macOS)
- Basic familiarity with Linux command line
- Root/sudo access for scripts requiring package management (scripts 10-15)
- Clone the repository:
git clone https://github.com/mreddyveera/shell-script.git
cd shell-script- Make scripts executable:
chmod +x *.shBasic scripts (no special permissions needed):
bash 01_helloworld.sh
bash 03_variables.sh
bash 04_variables.sh John Jane
bash 06_datatypes.sh 5 10
bash 07_arrays.sh
bash 08_specialvariables.sh arg1 arg2
bash 09_conditions.sh 100Root-required scripts:
sudo bash 10_installscripts.sh
sudo bash 11_functions.sh
sudo bash 12_colors.sh
sudo bash 13_logs.shLog management scripts:
bash 15_backuplogs.sh /path/to/source /path/to/destination [days]Follow this progression to master shell scripting:
- Days 1-2: Start with scripts 01-05 to understand variables and basic syntax
- Days 3-4: Move to scripts 06-08 to learn data types, arrays, and special variables
- Days 5-6: Study scripts 09 to understand control flow
- Days 7-8: Progress to scripts 10-12 to learn real DevOps automation
- Days 9-10: Master scripts 13-15 for log management and backup operations
- Variables: Declaration, assignment, and usage
- Command-line Arguments: Positional parameters and argument parsing
- User Input: Reading and handling user input
- Arithmetic Operations: Mathematical calculations in bash
- Arrays: Array declaration, indexing, and manipulation
- Special Variables: Built-in bash variables
- Conditional Statements: if/elif/else constructs
- Functions: Creating reusable code blocks
- Error Handling: Exit codes and validation
- Logging: File-based logging with timestamps
- Color Output: ANSI escape codes for colored terminal output
- File Operations: Finding, backing up, and deleting files
- Package Management: Using dnf/yum for system administration
These scripts demonstrate practical DevOps scenarios:
- Automated system setup: Installing required packages
- Monitoring and logging: Creating comprehensive log trails
- Log rotation: Managing disk space with old log deletion
- Backup strategies: Archiving logs before cleanup
- Error handling: Graceful failure and recovery
- User feedback: Clear error messages and status updates
- Scripts prefixed with
#!bin/bashshould use#!/bin/bash(with/after#!) - Root access is required for scripts that use
dnf installcommands - Log files are created in
/var/log/shell-scriptlogs/(or/home/ec2-user/shell_script.logs/for script 15) - Some scripts have specific requirements for source directories and permissions
mreddyveera - Shell Script Repository Creator
This repository is open source and available for educational purposes.
Contributions are welcome! Feel free to submit issues or pull requests to improve the scripts or documentation.
Last Updated: May 11, 2026