Basic Linux Shell Scripting for Devops
In the vast landscape of Linux, mastering the basics of shell scripting is like acquiring a magic wand that empowers users to command their systems with precision and efficiency. Whether you're a Linux enthusiast, a developer diving into system administration, or a newcomer eager to explore the command line, this blog is your gateway to understanding the fundamentals of shell scripting.
What Is Shell🐚 ?
The term "shell" refers to a command-line interface (CLI) that allows users to interact with the operating system by entering text-based commands. The shell is a program that interprets user inputs and executes commands. It acts as an intermediary between the user and the operating system kernel.
There are several shells available in Linux, with some of the most common ones being:
Bash (Bourne Again Shell): This is one of the most widely used shells and is the default shell for many Linux distributions. It is an enhanced version of the original Bourne Shell (sh) and includes features from other popular shells.
Zsh (Z Shell): Zsh is an extended and customizable shell with many advanced features. It includes powerful scripting capabilities and a user-friendly configuration system.
Fish (Friendly Interactive Shell): Fish is designed to be user-friendly and interactive. It has a straightforward syntax highlighting and autocompleting, making it particularly suitable for users who are new to the command line.
Dash: Dash is a lightweight and fast shell that aims to be POSIX-compliant. It is often used as the default system shell for some Linux distributions due to its efficiency.
What is Shell Scripting 📜?
Shell scripting in Linux involves writing a series of commands in a script file that can be executed by a shell. A shell script is a text file containing a sequence of commands that are executed as a single unit. These scripts can automate repetitive tasks, execute a sequence of commands, or perform complex operations by combining various shell commands and control structures.
Here are some key aspects of shell scripting in Linux:
Scripting Languages 🔡: Shell scripts are typically written using scripting languages supported by the shell, such as Bash, Zsh, or other compatible shells. Bash (Bourne Again SHell) is one of the most commonly used scripting languages for Linux shell scripting.
Text Editor 📝: You can create shell scripts using a text editor like Vim, Emacs, Nano, or any other editor of your choice. The script file should have execute permissions to be run as a program.
Shebang ⋕❗: The shebang (#!/bin/bash or similar) is the first line of a script and specifies the path to the shell interpreter that should be used to execute the script. It tells the system which shell should be used to interpret the script.
Example:
Variables 🔠: Shell scripts can use variables to store and manipulate data. Variables are created by assigning a value to a name.
Example:
Control Structures 🛂: Shell scripts support control structures like loops (for, while) and conditional statements (if, else). These structures allow you to control the flow of execution based on conditions.
Example:
Functions: Shell scripts can define functions to group and reuse code. Functions can be called within the script to perform specific tasks.
Example:
Command-Line Arguments: You can pass arguments to a shell script when you run it. These arguments are accessed within the script using special variables like $1, $2, etc.
Example:
What is Shell scripting for DevOps?
In the context of DevOps, shell scripting plays a crucial role in automating repetitive tasks, managing infrastructure, and facilitating the deployment and maintenance of software systems. It enables DevOps professionals to streamline workflows, improve efficiency, and enhance productivity.
Here are some ways in which shell scripting is used in DevOps:
Automation of Deployment Processes
Configuration Management
Continuous Integration/Continuous Deployment (CI/CD)
Log Parsing and Analysis
Backup and Recovery
Infrastructure as Code (IaC)
Environment Provisioning
Monitoring and Alerting Automation
What is #!/bin/bash? can we write #!/bin/sh as well ?
#!/bin/bash and #!/bin/sh are both examples of shebang lines in a shell script.
#!/bin/bash: This shebang line indicates that the script should be interpreted using the Bash shell. Bash (Bourne Again SHell) is a widely used shell on Linux systems and is the default shell for many distributions.
#!/bin/sh: This shebang line indicates that the script should be interpreted using the system's default shell. On many Unix-like systems, including Linux, /bin/sh is typically a symbolic link or reference to a compatible shell, often based on the original Bourne Shell. It might be Bash, Dash, or another POSIX-compliant shell.
While #!/bin/bash explicitly specifies the use of the Bash shell, #!/bin/sh is more general and allows the system to use whatever shell is set as the default. However, it's important to note that the behavior of /bin/sh can vary between different systems, and it might not necessarily be Bash.
Write a Shell Script which prints I will complete #90DaysOofDevOps challenge
Create one shell script using .sh extension and print whatever message you want using echo command as shown below,
To execute the shell script we can user ‘bash’ or ‘./’ command as shown below,
Write a Shell Script to take user input, input from arguments and print the variables.
We have created the shell script to print the multiplication table by taking user input as below,
below is the output,