🐚

Introduction to Shell Scripting Basics

Sep 4, 2024

Shell Scripting Tutorial Notes

Introduction

  • Presenter: Abhishek
  • Topic: Shell Scripting from Zero to Hero
  • Focus on basics, role in DevOps, and simple examples.

What is Shell Scripting?

  • Shell scripting is a way to automate tasks on Linux systems.
  • Reduces manual effort in repetitive tasks.
  • Useful for DevOps engineers in day-to-day activities like automation, file handling, and system management.

Importance of Automation

  • Automation helps reduce manual activities in any field, including software and daily tasks.
  • Example: Writing numbers from 1 to 10 manually versus automating it when the range increases.

Basic Commands in Shell Scripting

  1. echo: Prints text to the terminal.
  2. touch: Creates new files.
  3. ls: Lists files and directories in a directory.
  4. man: Displays the manual or help for commands.
  5. chmod: Changes file permissions:
    • 777: Read, write, and execute for everyone.
    • 444: Read permissions only.
  6. cat: Displays the content of files.
  7. pwd: Prints the current working directory.
  8. mkdir: Creates a new directory.
  9. cd: Changes the directory.
  10. rm -rf: Deletes files/directories.

Writing a Shell Script

  • Start with a shebang (#!/bin/bash) to specify the script's interpreter.
  • Use proper indentation and comments for readability.
  • Example script to create files and directories: #!/bin/bash mkdir Abhishek cd Abhishek touch first_file second_file
  • Save the file using :wq in vi or vim editor.

Executing a Shell Script

  • Use sh script.sh or ./script.sh to run a shell script.
  • Grant execute permission with chmod before running the script.

Role of Shell Scripting in DevOps

  • Automates infrastructure and configuration management tasks.
  • Example: Monitoring node health across multiple virtual machines.
  • Shell scripts can log into machines, check system parameters, and notify users about issues.

Common Use Cases for Shell Scripting in DevOps

  • Automating routine monitoring tasks (CPU, memory usage).
  • Facilitate interactions with Git and other Linux-based tools.
  • Configuration management for various applications/services.

Advanced Topics (To Explore Later)

  • Handling signals in scripts (using trap).
  • Writing cron jobs for scheduled tasks.

Conclusion

  • Shell scripting is an essential skill for DevOps engineers.
  • Encourages hands-on practice to solidify understanding.
  • Feedback requested for advanced topics.

Call to Action

  • Like, comment, and subscribe for more content.