Skip to content

mreddyveera/shell-script

Repository files navigation

Shell Script Repository

Detailed description of shell scripting in DevOps cycle

πŸ“‹ Overview

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.

πŸ“ Repository Structure

The repository contains 15 numbered shell scripts, each focusing on specific concepts:

Beginner Level - Fundamentals

01_helloworld.sh

  • Purpose: Hello World introduction script
  • Concepts: Basic script structure, echo command
  • Usage: bash 01_helloworld.sh

02_variables.sh

  • Purpose: Introduction to echo without variables
  • Concepts: Basic echo output
  • Output: Prints a conversation between two people

03_variables.sh

  • 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"
  • Usage: bash 03_variables.sh

04_variables.sh

  • Purpose: Command-line arguments as variables
  • Concepts: Positional parameters ($1, $2, etc.)
  • Key Features:
    • Access script arguments using $1 and $2
    • Parameters are passed when executing the script
  • Usage: bash 04_variables.sh Mani Trinadh

05_variables.sh

  • Purpose: User input handling
  • Concepts: Reading user input, silent input capture
  • Key Features:
    • read command for user input
    • -s flag for silent input (passwords)
  • Usage: bash 05_variables.sh

Intermediate Level - Core Concepts

06_datatypes.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
  • Usage: bash 06_datatypes.sh 10 20

07_arrays.sh

  • 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]}
  • Usage: bash 07_arrays.sh

08_specialvariables.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

09_conditions.sh

  • Purpose: Conditional statements and control flow
  • Concepts: if/elif/else conditions, numeric comparisons
  • Key Features:
    • if [ $NUMBER -gt 100 ] - Greater than comparison
    • elif [ $NUMBER -eq 100 ] - Equal to comparison
    • else - Default case
  • Usage: bash 09_conditions.sh 150

Advanced Level - DevOps Automation

10_installscripts.sh

  • 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
  • Usage: sudo bash 10_installscripts.sh
  • Requirements: Root privileges

11_functions.sh

  • 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
  • Usage: sudo bash 11_functions.sh
  • Requirements: Root privileges

12_colors.sh

  • 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
  • Usage: sudo bash 12_colors.sh
  • Requirements: Root privileges

13_logs.sh

  • 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
  • Usage: sudo bash 13_logs.sh
  • Requirements: Root privileges
  • Output: Logs stored in /var/log/shell-scriptlogs/

14_deletelogs.sh

  • 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
  • Usage: sudo bash 14_deletelogs.sh
  • Requirements: Root privileges
  • Caution: Script deletes files, use with care

15_backuplogs.sh

  • 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
  • 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
  • Optional Parameters: Days parameter defaults to 14 if not provided

πŸš€ Getting Started

Prerequisites

  • 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)

Installation

  1. Clone the repository:
git clone https://github.com/mreddyveera/shell-script.git
cd shell-script
  1. Make scripts executable:
chmod +x *.sh

Running the Scripts

Basic 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 100

Root-required scripts:

sudo bash 10_installscripts.sh
sudo bash 11_functions.sh
sudo bash 12_colors.sh
sudo bash 13_logs.sh

Log management scripts:

bash 15_backuplogs.sh /path/to/source /path/to/destination [days]

πŸ“š Learning Path

Follow this progression to master shell scripting:

  1. Days 1-2: Start with scripts 01-05 to understand variables and basic syntax
  2. Days 3-4: Move to scripts 06-08 to learn data types, arrays, and special variables
  3. Days 5-6: Study scripts 09 to understand control flow
  4. Days 7-8: Progress to scripts 10-12 to learn real DevOps automation
  5. Days 9-10: Master scripts 13-15 for log management and backup operations

πŸ”§ Key Concepts Covered

  • 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

πŸ’‘ DevOps Applications

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

πŸ“ Notes

  • Scripts prefixed with #!bin/bash should use #!/bin/bash (with / after #!)
  • Root access is required for scripts that use dnf install commands
  • 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

πŸ”— Related Resources

πŸ‘¨β€πŸ’» Author

mreddyveera - Shell Script Repository Creator

πŸ“„ License

This repository is open source and available for educational purposes.

🀝 Contributing

Contributions are welcome! Feel free to submit issues or pull requests to improve the scripts or documentation.


Last Updated: May 11, 2026

About

Detailed description of shell scripting in Devops cycle

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages