Skip to main content

Command Palette

Search for a command to run...

Basic Shell Scripting

Day04

Published
โ€ข6 min readโ€ขView as Markdown
Basic Shell Scripting

What is Kernel and Shell in Linux

๐ŸŒ Kernel: The kernel is like the ๐Ÿงฑ foundation of the operating system. It connects the ๐Ÿ–ฅ๏ธ hardware and the ๐Ÿ“Ÿ software, managing resources and providing essential functions. It handles tasks such as ๐Ÿ’พ memory management, โŒš process scheduling, ๐Ÿ–ฅ๏ธ device drivers, ๐Ÿ—„๏ธ file system management, and ๐ŸŒ network communication. Think of it as the behind-the-scenes superhero that keeps everything running smoothly.

๐Ÿ’ป Shell: The shell is the ๐Ÿš command-line interface that lets you interact with the operating system. It's like your virtual ๐Ÿ—ฃ๏ธ voice to communicate with the computer. You can enter commands and execute programs or scripts. The shell interprets your commands and talks to the kernel to perform the requested operations. It offers features like ๐Ÿ“ฅ input/output redirection, ๐Ÿšฐ piping, ๐Ÿ“œ scripting, ๐Ÿ“ฆ variable manipulation, and control structures. It's your tool for managing the system efficiently.

Examples:

  • Kernel: Imagine the kernel as the conductor of an ๐ŸŽต orchestra. It coordinates the various instruments (hardware and software) to create harmonious music (a functional system).

  • Shell: Picture the shell as a ๐Ÿ“ฑ personal assistant. You give it commands, and it carries out your requests. For example, typing "ls" in the shell lists the files and directories in the current location.

Remember, different shells have their own flavors and names:

  • ๐Ÿš Bash (Bourne Again Shell) is like the friendly and ubiquitous one, similar to a Swiss Army knife ๐Ÿ‡จ๐Ÿ‡ญ.

  • ๐Ÿš Zsh (Z Shell) is known for its customization options and powerful features, like a fancy sports car ๐ŸŽ๏ธ.

  • ๐Ÿš Ksh (Korn Shell) is a reliable and efficient option, comparable to a sturdy workhorse ๐Ÿด.

  • ๐Ÿš Csh (C Shell) has a syntax inspired by the C programming language, similar to a specialized tool for specific tasks ๐Ÿ”ง.

Together, the kernel and shell form a powerful duo that allows you to harness the capabilities of your Linux system.

What is Shell Scripting for DevOps?

๐Ÿง Linux Shell Scripting for DevOps is like having a ๐Ÿค– robot assistant that automates tasks, manages configurations, and simplifies operations in the world of DevOps. It's all about making life easier for developers and operations teams by leveraging the power of scripting languages like Bash.

Here's how it works:

๐Ÿค– Automation: Shell scripting allows you to automate repetitive tasks like setting up environments, deploying applications, running tests, and performing maintenance. It's like having a trusty robot ๐Ÿค– that does the work for you, saving time and reducing errors.

๐Ÿ”ง Configuration Management: Shell scripting helps you manage and control system configurations. You can define scripts to ensure that servers, services, and applications are properly configured. It's like having a magic wand ๐Ÿช„ that keeps everything in the right place, making deployments consistent and hassle-free.

โš™๏ธ CI/CD: Shell scripting plays a vital role in Continuous Integration and Deployment (CI/CD) pipelines. You can use scripts to automate building, testing, and deploying software. It's like having a conveyor belt ๐Ÿญ that takes your code through all the necessary steps, ensuring speedy and reliable releases.

๐Ÿ” Monitoring and Alerting: Shell scripting enables you to automate system monitoring. You can write scripts to gather and analyze metrics, log files, and application performance. It's like having a vigilant watchdog ๐Ÿถ that keeps an eye on your systems and barks when something goes wrong.

๐Ÿ—๏ธ Infrastructure as Code (IaC): Shell scripting can be used alongside infrastructure-as-code tools like Ansible, Terraform, or Kubernetes. You can write scripts to provision and manage infrastructure resources programmatically. It's like having a magic spell ๐Ÿช„ that creates and configures your infrastructure with a wave of your hand.

Example: Let's say you want to automate the deployment of your application using a CI/CD pipeline. You can write a shell script that builds the code, runs tests, and deploys it to the production server. With a single command, your script takes care of the entire process, saving you time and effort. ๐Ÿš€

In a nutshell, Linux shell scripting for DevOps is like having a helpful robot assistant ๐Ÿค– that takes care of repetitive tasks, manages configurations, and streamlines operations. It's a powerful tool in the DevOps toolbox, enabling faster development cycles, smoother deployments, and efficient system management.

What is #!/bin/bash? can we write #!/bin/sh as well?

๐Ÿ“ #!/bin/bash and #!/bin/sh are special constructs known as shebangs or hashbangs. They're like signposts that tell the system which interpreter or shell to use when executing a shell script.

  1. #!/bin/bash: This shebang specifies that the script should be executed using the ๐Ÿš Bash shell. Bash (Bourne Again Shell) is a widely-used and feature-rich shell that provides powerful capabilities. It's like having a ๐Ÿš€ rocket engine for your script, enabling advanced functionalities.

  2. #!/bin/sh: This shebang specifies that the script should be executed using the default system shell, typically following the POSIX shell standard. The /bin/sh path points to the location of the default shell on most systems. The actual shell may vary, but it's usually a shell that adheres to the POSIX standard.

Example: Imagine you have a script that performs system configurations. If you use #!/bin/bash, it's like having a ๐Ÿ› ๏ธ toolbox with a wide range of tools and features specifically provided by Bash. On the other hand, if you use #!/bin/sh, it's like having a ๐Ÿงฐ basic toolkit with common tools that work across different systems, ensuring portability and compatibility.

So, you can choose either #!/bin/bash or #!/bin/sh at the beginning of your shell script. The decision depends on whether you need specific Bash features or you want your script to work on different systems with different default shells.

Write a Shell Script to take user input, input from arguments and print the variables.

Here's a shell script that takes user input and command-line arguments, and then prints the variables:

  1. First I have created a folder called scripts and switched to it.

  2. I have added the Path of scripts directory to the $PATHvariable so that we need not to use ./ to execute the script

  3. Now I created a script script.sh using vim

  4. After saving it with esc :wq , we need to change the permission of it using chmod u+x script.sh so that it got the permission to execute by user.

  5. The script will prompt you to enter your name. After you enter your name and press Enter, it will print the user input (name) as well as the command-line arguments passed, here I have passed 'Hello' and 'Linux' as arguments. So now lets execute it.

Example of If else in Shell Scripting by comparing 2 numbers

  1. Now I created a script script2.sh using vim

  2. This script will ask for number1 and number2 from user and upon enter the values, it will evaluate using the if else that if either the numbers are equal, or number1 is greater than number2 or number2 is greater than number1. Don't forget to change the permission of the script.

Feel free to modify and expand the scripts as per your requirements. Good luck with your #90DaysOfDevOps challenge!