Skip to main content

Linux Shell Scripting: Automate Like a Pro! 🚀

 

Linux Shell Scripting: Automate Like a Pro! 🚀


Now that you’ve mastered the core Linux concepts, it’s time to take the next big leapShell Scripting! 🖥️💡

🔹 Why Learn Shell Scripting?
Shell scripting is a must-have skill for DevOps, automation, system administration, and cloud computing. It helps you:
✅ Automate repetitive tasks
✅ Manage system configurations efficiently
✅ Perform bulk operations in a single command
✅ Improve productivity & minimize human errors




🌍 Shape Your Future with AI & Infinite Knowledge...!!

🌐 Want to Generate Text-to-Voice, Images & Videos? 👉 http://www.ai.skyinfinitetech.com 📚 Read In-Depth Tech & Self-Improvement Blogs 👉 http://www.skyinfinitetech.com ▶ Watch Life-Changing Videos on YouTube 👉 https://www.youtube.com/@SkyInfinite-Learning 🔥 Transform Your Skills, Business & Productivity – Join Us Today! 🔥



1️⃣ What is Shell Scripting?

A shell script is a file containing a series of commands that the Linux shell executes sequentially. Instead of running commands one by one, you can put them in a script and run them automatically!


📌 Basic Structure of a Shell Script:


#!/bin/bash echo "Hello, DevOps World!" # Prints a message
  • #!/bin/bash → Shebang, tells Linux to use the Bash shell
  • echo → Prints text to the screen


🔹 Running a Shell Script:
1️⃣ Create a script file: nano myscript.sh
2️⃣ Make it executable: chmod +x myscript.sh
3️⃣ Run the script: ./myscript.sh



2️⃣ Variables & User Input in Shell Scripts

🔹 Defining Variables:


name="DevOps Engineer" echo "Hello, $name!"


🔹 User Input:


echo "Enter your name:" read user_name echo "Welcome, $user_name!"

🔹 Environment Variables:


echo "Your home directory is: $HOME"


3️⃣ Conditional Statements: Decision Making in Shell


🔹 If-Else Condition:


#!/bin/bash echo "Enter your age:" read age if [ $age -ge 18 ]; then echo "You are eligible to vote!" else echo "Sorry, you are not eligible." fi


🔹 Case Statements:


echo "Enter a number between 1-3:" read num case $num in 1) echo "You chose One!";; 2) echo "You chose Two!";; 3) echo "You chose Three!";; *) echo "Invalid choice!";; esac


4️⃣ Loops: Automating Repetitive Tasks

🔹 For Loop:


for i in 1 2 3 4 5 do echo "Number: $i" done


🔹 While Loop:


count=1 while [ $count -le 5 ] do echo "Iteration: $count" ((count++)) done


🔹 Until Loop:


x=1 until [ $x -gt 5 ] do echo "Number: $x" ((x++)) done


5️⃣ Functions in Shell Scripting

Functions allow code reuse and better script structure.


#!/bin/bash function greet() { echo "Hello, $1! Welcome to Linux Scripting!" } greet "DevOps Engineer"


6️⃣ File Handling in Shell Scripts

🔹 Checking if a file exists:


if [ -f /etc/passwd ]; then echo "File exists!" else echo "File does not exist!" fi


🔹 Reading a file line by line:


while read line; do echo "$line" done < myfile.txt


7️⃣ Process Management in Shell Scripts

🔹 Killing a process:


#!/bin/bash echo "Enter process name to kill:" read pname pkill $pname


🔹 Monitoring CPU & Memory Usage:


echo "Top 5 CPU-consuming processes:" ps -eo pid,comm,%cpu --sort=-%cpu | head -6


8️⃣ Scheduling Jobs with Crontab

Want to run scripts automatically at a scheduled time? Use Crontab!
🔹 Opening Crontab Editor:


crontab -e


🔹 Scheduling a script to run every day at midnight:


0 0 * * * /path/to/script.sh


9️⃣ Real-World Shell Scripting Examples

🔹 Automating Backup Process:


#!/bin/bash tar -czf backup_$(date +%F).tar.gz /home/user/documents echo "Backup completed!"


🔹 Checking Server Uptime & Health:


#!/bin/bash echo "System Uptime:" uptime echo "Disk Usage:" df -h echo "Memory Usage:" free -m

Comments

Popular posts from this blog

Introduction to Terraform – The Future of Infrastructure as Code

  Introduction to Terraform – The Future of Infrastructure as Code In today’s fast-paced DevOps world, managing infrastructure manually is outdated . This is where Terraform comes in—a powerful Infrastructure as Code (IaC) tool that allows you to define, provision, and manage cloud infrastructure efficiently . Whether you're working with AWS, Azure, Google Cloud, or on-premises servers , Terraform provides a declarative, automation-first approach to infrastructure deployment. Shape Your Future with AI & Infinite Knowledge...!! Read In-Depth Tech & Self-Improvement Blogs http://www.skyinfinitetech.com Watch Life-Changing Videos on YouTube https://www.youtube.com/@SkyInfinite-Learning Transform Your Skills, Business & Productivity – Join Us Today! In today’s digital-first world, agility and automation are no longer optional—they’re essential. Companies across the globe are rapidly shifting their operations to the cloud to keep up with the pace of innovatio...

📊 Monitoring & Logging in Kubernetes – Tools like Prometheus, Grafana, and Fluentd

  Monitoring & Logging in Kubernetes – Tools like Prometheus, Grafana, and Fluentd Monitoring and logging are essential for maintaining a healthy and well-performing Kubernetes cluster. In this guide, we’ll cover why monitoring is important, key monitoring tools like Prometheus and Grafana, and logging tools like Fluentd to help you gain visibility into your cluster’s performance and logs. Shape Your Future with AI & Infinite Knowledge...!! Want to Generate Text-to-Voice, Images & Videos? http://www.ai.skyinfinitetech.com Read In-Depth Tech & Self-Improvement Blogs http://www.skyinfinitetech.com Watch Life-Changing Videos on YouTube https://www.youtube.com/@SkyInfinite-Learning Transform Your Skills, Business & Productivity – Join Us Today! 🚀 Introduction In today’s fast-paced cloud-native environment, Kubernetes has emerged as the de-facto container orchestration platform. But deploying and managing applications in Kubernetes is just half the ba...

🔒 Kubernetes Security – RBAC, Network Policies, and Secrets Management

  Kubernetes Security – RBAC, Network Policies, and Secrets Management Security is a critical aspect of managing Kubernetes clusters. In this guide, we'll cover essential security mechanisms like Role-Based Access Control (RBAC) , Network Policies , and Secrets Management to help you secure your Kubernetes environment effectively. Shape Your Future with AI & Infinite Knowledge...!! Want to Generate Text-to-Voice, Images & Videos? http://www.ai.skyinfinitetech.com Read In-Depth Tech & Self-Improvement Blogs http://www.skyinfinitetech.com Watch Life-Changing Videos on YouTube https://www.youtube.com/@SkyInfinite-Learning Transform Your Skills, Business & Productivity – Join Us Today! 🚀 Introduction: Why Kubernetes Security Is Non-Negotiable As Kubernetes becomes the backbone of modern cloud-native infrastructure, security is no longer optional—it’s mission-critical . With multiple moving parts like containers, pods, services, nodes, and more, Kuberne...