├── README.md ├── day01 └── hello.sh ├── day02 ├── check_if_jetha_loyal.sh ├── create_user.sh ├── for_loop.sh ├── jetha_lal_ki_duniya.sh └── while_loop.sh ├── day03 ├── create_ec2.sh ├── deploy_django_app.sh └── error_handle.sh └── day04 └── create_ec2.sh /README.md: -------------------------------------------------------------------------------- 1 | # Shell Scripting in One Shot – Comprehensive Guide for DevOps 2 | 3 | ## 1. Introduction to Shell and Environment Setup 4 | - What is Shell? (Bash, Zsh, Ksh, etc.) 5 | - Shell vs. Terminal vs. Bash 6 | - Installing and Setting Up Bash (Linux/macOS/WSL for Windows) 7 | - Essential Configuration Files (`~/.bashrc`, `~/.bash_profile`, `~/.zshrc`) 8 | - Setting Up a DevOps-Friendly Shell Environment 9 | - PS1 Prompt Customization 10 | - Useful Aliases and Functions 11 | - Environment Variables (`$PATH`, `$HOME`, `$USER`) 12 | 13 | ## 2. Basic Scripting Skills 14 | - Writing Your First Shell Script (`.sh` file, shebang `#!/bin/bash`) 15 | - Executing Scripts (`chmod +x script.sh`, `./script.sh`, `bash script.sh`) 16 | - Variables and Data Types (String, Integer, Arrays) 17 | - Reading User Input (`read` command) 18 | - Basic Operators (Arithmetic, Relational, Logical) 19 | - Control Flow: 20 | - Conditional Statements (`if-else`, `case`) 21 | - Looping (`for`, `while`, `until`) 22 | - Functions in Shell Scripts 23 | - Exit Codes and Status (`$?`, `exit` command) 24 | 25 | ## 3. Intermediate Scripting Techniques 26 | - Working with Files and Directories (`ls`, `cp`, `mv`, `rm`, `mkdir`, `find`) 27 | - String Manipulation (`sed`, `awk`, `cut`, `tr`) 28 | - File Permissions and Ownership (`chmod`, `chown`, `umask`) 29 | - Input and Output Redirection (`>`, `>>`, `<`, `2>`, `&>`) 30 | - Process Management (`ps`, `top`, `kill`, `nohup`, `&`, `jobs`, `fg`, `bg`) 31 | - Cron Jobs and Task Automation (`crontab -e`, `at`, `systemd timers`) 32 | - Working with Logs (`tail -f`, `grep`, `awk` for parsing logs) 33 | 34 | ## 4. Advanced Scripting and Debugging 35 | - Writing Robust Scripts with Error Handling (`set -e`, `trap`, `||`, `&&`) 36 | - Debugging Techniques (`bash -x script.sh`, `set -x`, `set -v`) 37 | - Regular Expressions and Pattern Matching (`grep -E`, `sed -r`) 38 | - Advanced File Processing (`awk`, `sed`, `xargs`, `cut`, `paste`) 39 | - Networking with Shell Scripts (`ping`, `curl`, `wget`, `netstat`, `ss`) 40 | - Parallel Execution and Background Jobs (`&`, `wait`, `xargs -P`) 41 | - Working with APIs in Shell Scripts (cURL for REST API calls) 42 | - Secure Shell Scripting (`ssh`, `scp`, `sftp`, `expect`) 43 | 44 | ## 5. Real-World Applications and Integration 45 | - Shell Scripting in DevOps Pipelines (CI/CD Integration) 46 | - Automating AWS/GCP/Azure Operations (`aws-cli`, `gcloud`, `az-cli`) 47 | - Automating Kubernetes Tasks (`kubectl`, `helm`, `jq`, `yq`) 48 | - Writing System Health Checks & Monitoring Scripts 49 | - Backup and Restore Automation 50 | - Log Parsing and Analysis with Shell Scripting 51 | 52 | ## 6. Shell Mastery and Continuous Learning 53 | - Writing Modular & Reusable Shell Scripts 54 | - Best Practices for Readable and Maintainable Shell Scripts 55 | - Shell Scripting Performance Optimization 56 | - Learning Alternative Shells (Zsh, Fish, Dash) 57 | - Moving Beyond Shell: When to Use Python, Ansible, or Terraform 58 | - Keeping Up with DevOps Industry Trends 59 | 60 | ## 7. Projects to Keep Up with the Industry 61 | - **Automated Log Monitoring & Alert System** 62 | - Parses logs, filters errors, and sends alerts via email or Slack. 63 | - **Infrastructure Backup Automation** 64 | - Automates backup of critical files, databases, or VM snapshots. 65 | - **CI/CD Pipeline Helper** 66 | - Automates repository cloning, testing, and deployment tasks. 67 | - **System Health Check Script** 68 | - Checks CPU, Memory, Disk Usage, Running Services, and Network Status. 69 | - **Kubernetes Resource Monitor** 70 | - Automates collection of Kubernetes cluster metrics for monitoring. 71 | - **AWS Instance Management Script** 72 | - Starts/stops AWS EC2 instances on demand or based on schedule. 73 | -------------------------------------------------------------------------------- /day01/hello.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # This is script for TWS 4 | 5 | echo "TWS: Hello Dosto" 6 | 7 | echo "Learners: DevOps Wale bhaiya , hum toh comment karenge" 8 | 9 | echo "TWS: Toh dosto, like bhi kar do" 10 | -------------------------------------------------------------------------------- /day02/check_if_jetha_loyal.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | << disclaimer 4 | This is just for infotainment purpose 5 | disclaimer 6 | 7 | # This is function definition 8 | 9 | function is_loyal() { 10 | read -p "$1 ne mud ke kise dekha: " bandi 11 | read -p "$1 ka pyaar %" pyaar 12 | 13 | if [[ $bandi == "daya bhabhi" ]]; 14 | then 15 | echo "$1 is loyal" 16 | elif [[ $pyaar -ge 100 ]]; 17 | then 18 | echo "$1 is loyal" 19 | else 20 | echo "$1 is not loyal" 21 | fi 22 | } 23 | 24 | # This is function call 25 | is_loyal "tom" 26 | -------------------------------------------------------------------------------- /day02/create_user.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | read -p "Enter username: " username 4 | 5 | echo "you entered $username" 6 | 7 | sudo useradd -m $username 8 | 9 | echo "New User added" 10 | -------------------------------------------------------------------------------- /day02/for_loop.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # This is for and while loops 4 | 5 | << comment 6 | 1 is argument 1 which is folder name 7 | 2 is start range 8 | 3 is end rangeask 9 | 10 | comment 11 | 12 | for (( num=$2 ; num<=$3; num++ )) 13 | do 14 | mkdir "$1$num" 15 | done 16 | 17 | 18 | -------------------------------------------------------------------------------- /day02/jetha_lal_ki_duniya.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # This is Jetha Lal ki Duniya 4 | 5 | << comment 6 | Anything 7 | written 8 | here will not be execute 9 | comment 10 | 11 | name="babitaji" 12 | 13 | echo "Name is $name, and date is $(date)" 14 | 15 | echo "enter the name:" 16 | 17 | read username 18 | 19 | echo "You entered $username" 20 | 21 | echo "The characters in $0 are: $1 $2" 22 | -------------------------------------------------------------------------------- /day02/while_loop.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | num=0 4 | 5 | while [[ $num -le 10 ]] 6 | do 7 | echo $num 8 | num=$((num+2)) 9 | done 10 | -------------------------------------------------------------------------------- /day03/create_ec2.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | set -euo pipefail 3 | 4 | check_awscli() { 5 | if ! command -v aws &> /dev/null; then 6 | echo "AWS CLI is not installed. Please install it first." >&2 7 | exit 1 8 | fi 9 | } 10 | 11 | install_awscli() { 12 | echo "Installing AWS CLI v2 on Linux..." 13 | 14 | # Download and install AWS CLI v2 15 | curl -s "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip" 16 | sudo apt-get install -y unzip &> /dev/null 17 | unzip -q awscliv2.zip 18 | sudo ./aws/install 19 | 20 | # Verify installation 21 | aws --version 22 | 23 | # Clean up 24 | rm -rf awscliv2.zip ./aws 25 | } 26 | 27 | wait_for_instance() { 28 | local instance_id="$1" 29 | echo "Waiting for instance $instance_id to be in running state..." 30 | 31 | while true; do 32 | state=$(aws ec2 describe-instances --instance-ids "$instance_id" --query 'Reservations[0].Instances[0].State.Name' --output text) 33 | if [[ "$state" == "running" ]]; then 34 | echo "Instance $instance_id is now running." 35 | break 36 | fi 37 | sleep 10 38 | done 39 | } 40 | 41 | create_ec2_instance() { 42 | local ami_id="$1" 43 | local instance_type="$2" 44 | local key_name="$3" 45 | local subnet_id="$4" 46 | local security_group_ids="$5" 47 | local instance_name="$6" 48 | 49 | # Run AWS CLI command to create EC2 instance 50 | instance_id=$(aws ec2 run-instances \ 51 | --image-id "$ami_id" \ 52 | --instance-type "$instance_type" \ 53 | --key-name "$key_name" \ 54 | --subnet-id "$subnet_id" \ 55 | --security-group-ids "$security_group_ids" \ 56 | --tag-specifications "ResourceType=instance,Tags=[{Key=Name,Value=$instance_name}]" \ 57 | --query 'Instances[0].InstanceId' \ 58 | --output text 59 | ) 60 | 61 | if [[ -z "$instance_id" ]]; then 62 | echo "Failed to create EC2 instance." >&2 63 | exit 1 64 | fi 65 | 66 | echo "Instance $instance_id created successfully." 67 | 68 | # Wait for the instance to be in running state 69 | wait_for_instance "$instance_id" 70 | } 71 | 72 | main() { 73 | check_awscli || install_awscli 74 | 75 | echo "Creating EC2 instance..." 76 | 77 | # Specify the parameters for creating the EC2 instance 78 | AMI_ID="" 79 | INSTANCE_TYPE="t2.micro" 80 | KEY_NAME="" 81 | SUBNET_ID="" 82 | SECURITY_GROUP_IDS="" # Add your security group IDs separated by space 83 | INSTANCE_NAME="Shell-Script-EC2-Demo" 84 | 85 | # Call the function to create the EC2 instance 86 | create_ec2_instance "$AMI_ID" "$INSTANCE_TYPE" "$KEY_NAME" "$SUBNET_ID" "$SECURITY_GROUP_IDS" "$INSTANCE_NAME" 87 | 88 | echo "EC2 instance creation completed." 89 | } 90 | 91 | main "$@" 92 | -------------------------------------------------------------------------------- /day03/deploy_django_app.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # Deploy a Django app and handle errors 4 | 5 | # Function to clone the Django app code 6 | code_clone() { 7 | echo "Cloning the Django app..." 8 | if [ -d "django-notes-app" ]; then 9 | echo "The code directory already exists. Skipping clone." 10 | else 11 | git clone https://github.com/LondheShubham153/django-notes-app.git || { 12 | echo "Failed to clone the code." 13 | return 1 14 | } 15 | fi 16 | } 17 | 18 | # Function to install required dependencies 19 | install_requirements() { 20 | echo "Installing dependencies..." 21 | sudo apt-get update && sudo apt-get install -y docker.io nginx docker-compose || { 22 | echo "Failed to install dependencies." 23 | return 1 24 | } 25 | } 26 | 27 | # Function to perform required restarts 28 | required_restarts() { 29 | echo "Performing required restarts..." 30 | sudo chown "$USER" /var/run/docker.sock || { 31 | echo "Failed to change ownership of docker.sock." 32 | return 1 33 | } 34 | 35 | # Uncomment the following lines if needed: 36 | # sudo systemctl enable docker 37 | # sudo systemctl enable nginx 38 | # sudo systemctl restart docker 39 | } 40 | 41 | # Function to deploy the Django app 42 | deploy() { 43 | echo "Building and deploying the Django app..." 44 | docker build -t notes-app . && docker-compose up -d || { 45 | echo "Failed to build and deploy the app." 46 | return 1 47 | } 48 | } 49 | 50 | # Main deployment script 51 | echo "********** DEPLOYMENT STARTED *********" 52 | 53 | # Clone the code 54 | if ! code_clone; then 55 | cd django-notes-app || exit 1 56 | fi 57 | 58 | # Install dependencies 59 | if ! install_requirements; then 60 | exit 1 61 | fi 62 | 63 | # Perform required restarts 64 | if ! required_restarts; then 65 | exit 1 66 | fi 67 | 68 | # Deploy the app 69 | if ! deploy; then 70 | echo "Deployment failed. Mailing the admin..." 71 | # Add your sendmail or notification logic here 72 | exit 1 73 | fi 74 | 75 | echo "********** DEPLOYMENT DONE *********" 76 | -------------------------------------------------------------------------------- /day03/error_handle.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | create_directory() { 4 | mkdir demo 5 | } 6 | 7 | if ! create_directory; then 8 | echo "The code is being exited as the directory already exists" 9 | exit 1 10 | fi 11 | 12 | echo "this should not work beacuse the code is interrupted" 13 | 14 | -------------------------------------------------------------------------------- /day04/create_ec2.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | set -euo pipefail 3 | 4 | check_awscli() { 5 | if ! command -v aws &> /dev/null; then 6 | echo "AWS CLI is not installed. Please install it first." >&2 7 | exit 1 8 | fi 9 | } 10 | 11 | install_awscli() { 12 | echo "Installing AWS CLI v2 on Linux..." 13 | 14 | # Download and install AWS CLI v2 15 | curl -s "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip" 16 | sudo apt-get install -y unzip &> /dev/null 17 | unzip -q awscliv2.zip 18 | sudo ./aws/install 19 | 20 | # Verify installation 21 | aws --version 22 | 23 | # Clean up 24 | rm -rf awscliv2.zip ./aws 25 | } 26 | 27 | wait_for_instance() { 28 | local instance_id="$1" 29 | echo "Waiting for instance $instance_id to be in running state..." 30 | 31 | while true; do 32 | state=$(aws ec2 describe-instances --instance-ids "$instance_id" --query 'Reservations[0].Instances[0].State.Name' --output text) 33 | if [[ "$state" == "running" ]]; then 34 | echo "Instance $instance_id is now running." 35 | break 36 | fi 37 | sleep 10 38 | done 39 | } 40 | 41 | create_ec2_instance() { 42 | local ami_id="$1" 43 | local instance_type="$2" 44 | local key_name="$3" 45 | local subnet_id="$4" 46 | local security_group_ids="$5" 47 | local instance_name="$6" 48 | 49 | # Run AWS CLI command to create EC2 instance 50 | instance_id=$(aws ec2 run-instances \ 51 | --image-id "$ami_id" \ 52 | --instance-type "$instance_type" \ 53 | --key-name "$key_name" \ 54 | --subnet-id "$subnet_id" \ 55 | --security-group-ids "$security_group_ids" \ 56 | --tag-specifications "ResourceType=instance,Tags=[{Key=Name,Value=$instance_name}]" \ 57 | --query 'Instances[0].InstanceId' \ 58 | --output text 59 | ) 60 | 61 | if [[ -z "$instance_id" ]]; then 62 | echo "Failed to create EC2 instance." >&2 63 | exit 1 64 | fi 65 | 66 | echo "Instance $instance_id created successfully." 67 | 68 | # Wait for the instance to be in running state 69 | wait_for_instance "$instance_id" 70 | } 71 | 72 | main() { 73 | check_awscli || install_awscli 74 | 75 | echo "Creating EC2 instance..." 76 | 77 | # Specify the parameters for creating the EC2 instance 78 | AMI_ID="" 79 | INSTANCE_TYPE="t2.micro" 80 | KEY_NAME="" 81 | SUBNET_ID="" 82 | SECURITY_GROUP_IDS="" # Add your security group IDs separated by space 83 | INSTANCE_NAME="Shell-Script-EC2-Demo" 84 | 85 | # Call the function to create the EC2 instance 86 | create_ec2_instance "$AMI_ID" "$INSTANCE_TYPE" "$KEY_NAME" "$SUBNET_ID" "$SECURITY_GROUP_IDS" "$INSTANCE_NAME" 87 | 88 | echo "EC2 instance creation completed." 89 | } 90 | 91 | main "$@" 92 | --------------------------------------------------------------------------------