Introduction to Shell Scripting

Introduction to Shell Scripting

what is shell scripting?

ยท

3 min read

Table of contents

No heading

No headings in the article.

Shell is an interface of the operating system. It accepts commands from users and interprets them to the operating system. If you want to run a bunch of commands together, you can do so by creating a shell script.

Shell scripting plays a crucial role in DevOps practices as it allows automation and orchestration of various tasks and processes within the software development lifecycle. Shell scripting refers to writing scripts using shell programming languages like Bash (Bourne Again SHell) or other compatible shells.

Tasks

  • Explain in your own words and examples, what is Shell Scripting for DevOps.

Shell scripting in Linux is a powerful tool for automating tasks, managing systems, and creating efficient workflows. It allows system administrators and DevOps professionals to automate repetitive tasks, customize their environment, and achieve better efficiency and productivity in managing Linux-based system

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

The line #!/bin/bash is called a shebang or a hashbang. It is used in Unix-like operating systems (such as Linux) to specify the interpreter or the shell that should be used to execute the script.

In this case, #!/bin/bash indicates that the script should be executed using the Bash shell, which is a popular and powerful shell that provides extensive features and capabilities.

On the other hand, #!/bin/sh specifies the default system shell, which may or may not be Bash. The sh shell is typically a symbolic link or a lightweight shell that aims for compatibility with the original Unix shell (Bourne shell).

  • Write a Shell Script which prints I will complete #90DaysOofDevOps challenge
vim challenge.sh
echo " I will complete #90DaysOfDevOps Challenge "

we use Vim editor for shell scripting. or we can also use nano editor for shell scripting.

using bash You can execute a shell script by explicitly invoking the Bash interpreter using the "bash" command followed by the script file name. or ./ we can run our shell script.

using "./": The "./" notation is used to execute a shell script if the script file has executable permissions. It specifies that the shell should look for the script in the current directory.

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

using input from arguments we can execute our first user input script.

Variables: Shell scripts use variables to store and manipulate data. Variables in Bash are defined without specifying a data type and are case-sensitive. You can assign values to variables using the = operator and access their values by prefixing the variable name with a $ sign.

  • Write an Example of If else in Shell Scripting by comparing 2 numbers.

  •     if CONDITION
        then
          COMMANDS_1
        else
          COMMANDS_2
        fi
    

    The COMMANDS_1 will be executed if the CONDITION evaluates to True. Otherwise, COMMANDS_2 will be run if CONDITION returns False. There can only be one else clause in a statement.

nano if_else.sh

If you find my blog valuable, I invite you to like, share, and join the discussion. Your feedback is immensely cherished as it fuels continuous improvement. Let's embark on this transformative DevOps adventure together! ๐Ÿš€ #devops #90daysofdevops #trainwithshubham #tws #LearningTogether #connections #community #Linux #LinuxCommands.

ย