Basic Linux Shell Scripting for DevOps Engineers. #Day-4

Nikunj VaishnavNikunj Vaishnav
5 min read

What is Kernel?

The kernel is a computer program that is the core of a computer’s operating system, with complete control over everything in the system.

What is Shell?

A shell is a special user program that provides an interface for users to interact with operating system services. It accepts human-readable commands from users and converts them into instructions that the kernel can understand. The shell is a command language interpreter that executes commands read from input devices such as keyboards or from files. It starts when the user logs in or opens a terminal.

Types of Shell:

There are several types of shells available in Unix-like operating systems, each with its own features and use cases. Here are some of the most commonly used shells:

1. Bourne Shell (sh)

  • Path: /bin/sh

  • Description: The original Unix shell, developed by Stephen Bourne. It is a simple and lightweight shell that is POSIX-compliant. Many scripts use #!/bin/sh for compatibility.

2. Bash (Bourne Again Shell)

  • Path: /bin/bash

  • Description: An enhanced version of the Bourne Shell, developed by the GNU Project. It includes features such as command-line editing, job control, and support for arrays and functions.

3. C Shell (csh)

  • Path: /bin/csh

  • Description: Developed by Bill Joy, the C Shell introduces a syntax resembling the C programming language. It includes features like job control and history mechanisms.

4. Korn Shell (ksh)

  • Path: /bin/ksh

  • Description: Developed by David Korn, it incorporates features from both the Bourne Shell and the C Shell. It is known for its scripting capabilities and efficiency.

5. Z Shell (zsh)

  • Path: /bin/zsh

  • Description: A highly customizable and feature-rich shell, Z Shell combines features from Bash, Csh, and Ksh. It offers advanced features like improved tab completion, spell correction, and themes.

6. Dash (Debian Almquist Shell)

  • Path: /bin/dash

  • Description: A lightweight, POSIX-compliant shell used primarily for scripting. It is the default /bin/sh on Debian-based systems due to its speed and efficiency.

7. Fish (Friendly Interactive Shell)

  • Path: /usr/bin/fish

  • Description: Focuses on user-friendliness and interactive use. It includes features like autosuggestions, syntax highlighting, and a web-based configuration interface.

8. Tcsh

  • Path: /bin/tcsh

  • Description: An enhanced version of the C Shell (csh) with additional features like command-line editing and programmable completion.

Key Differences and Use Cases:

  • Compatibility and Portability: For scripts that need to run on various Unix-like systems, sh (or dash) is often used.

  • Advanced Scripting: For more powerful scripting capabilities, bash or zsh are popular choices due to their extended features.

  • Interactive Use: For an enhanced interactive experience, fish and zsh are preferred for their user-friendly features.

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

The line #!/bin/bash is known as a shebang (or hashbang). It is used at the beginning of a script to specify the interpreter that should be used to execute the script. In this case, #!/bin/bash indicates that the script should be run using the Bash shell, which is located at /bin/bash.

Yes, you can write #!/bin/sh as well. This shebang line indicates that the script should be run using the sh shell, which is typically a link to a POSIX-compliant shell. The exact shell that /bin/sh points to can vary between systems; on many systems, it points to a lightweight shell like dash (Debian Almquist Shell) for performance reasons, but it could also be a link to bash or another shell.

Here's a brief comparison of the two:

  • #!/bin/bash: Specifically invokes the Bash shell. Bash includes a variety of extensions and features beyond those found in the POSIX standard, making it a powerful and flexible shell for scripting.

  • #!/bin/sh: Invokes the default system shell, which is expected to be POSIX-compliant. This ensures better portability across different Unix-like systems since scripts written for sh are more likely to run without modification on various platforms.

Using #!/bin/sh is recommended if you want your script to be portable and compatible with different Unix-like operating systems, while #!/bin/bash is preferred if you need specific features provided by Bash.

What is Linux Shell Scripting?

Linux shell scripting involves writing programs (scripts) that can be run by a Linux shell, such as bash (Bourne Again Shell). These scripts automate tasks, perform system administration tasks, and facilitate the interaction between users and the operating system.

Shell Script that prints I will complete #90DaysOfDevOps challenge

#!/bin/bash
echo "I will complete #90DaysOfDevOps challenge"

Execute Script and check result:

Shell Script that takes user input, input from arguments, and prints the variables.

Execute Script and check result:

An example of an If-Else statement in Shell Scripting by comparing two numbers.

#!/bin/bash
num1=10
num2=20
if [ $num1 -gt $num2 ]; then
    echo "$num1 is greater than $num2"
elif [ $num1 -lt $num2 ]; then
    echo "$num1 is less than $num2"
else
    echo "$num1 is equal to $num2"
fi

Execute Script and check result:

Conclusion

Understanding the basics of Linux shell scripting is essential for DevOps engineers. The kernel and shell are fundamental components of the operating system, with the kernel managing system resources and the shell providing an interface for user interaction. Various types of shells, such as Bash, Zsh, and Fish, offer different features and use cases, making it important to choose the right one for your needs. Using shebangs like #!/bin/bash or #!/bin/sh helps ensure scripts are executed with the appropriate interpreter, balancing portability and functionality. By mastering shell scripting, DevOps engineers can automate tasks, streamline system administration, and enhance their overall productivity.

Connect and Follow Me On Socials :

LinkedIn|Twitter|GitHub

Like👍 | Share📲 | Comment💭

0
Subscribe to my newsletter

Read articles from Nikunj Vaishnav directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Nikunj Vaishnav
Nikunj Vaishnav

👋 Hi there! I'm Nikunj Vaishnav, a passionate QA engineer Cloud, and DevOps. I thrive on exploring new technologies and sharing my journey through code. From designing cloud infrastructures to ensuring software quality, I'm deeply involved in CI/CD pipelines, automated testing, and containerization with Docker. I'm always eager to grow in the ever-evolving fields of Software Testing, Cloud and DevOps. My goal is to simplify complex concepts, offer practical tips on automation and testing, and inspire others in the tech community. Let's connect, learn, and build high-quality software together! 📝 Check out my blog for tutorials and insights on cloud infrastructure, QA best practices, and DevOps. Feel free to reach out – I’m always open to discussions, collaborations, and feedback!