Skip to main content

Command Palette

Search for a command to run...

#90 days of DevOps challenge- (Task4)

Basic Linux Shell Scripting for DevOps Engineers

Updated
3 min read
#90 days of DevOps challenge- (Task4)
M

I am passionate about learning new skills . Currently quenching my thirst for knowledge by learning DevOps methodology.

Shell scripting is the practice of writing scripts using shell languages, such as Bash, that automate tasks and processes on a command-line interface (CLI) or terminal. In the context of DevOps, shell scripting is a valuable tool that enables the automation of deployment, configuration, and management of infrastructure and applications.

In DevOps, shell scripting is often used to automate tasks that are repetitive and time-consuming, such as building and deploying code, creating and configuring virtual machines, monitoring logs and metrics, and executing tests. By using shell scripts, DevOps teams can save time and increase productivity while ensuring consistency and reliability.

For example, a shell script can be used to automate the deployment of a web application to a production server. The script can be written to perform tasks such as downloading the latest version of the code from a Git repository, compiling and packaging the code, copying the package to the server, and restarting the web server. By running this script, the deployment process can be completed quickly and consistently, with minimal manual intervention.

Difference between #!/bin/bash and #!/bin/sh:

The shebang, #!/bin/bash when used in scripts is used to instruct the operating system to use bash as a command interpreter. Each of the systems has its own shells which the system will use to execute its own system scripts. This system shell can vary from OS to OS(most of the time it will be bash). Whereas, when the shebang, #!/bin/sh used in scripts instructs the internal system shell to start interpreting scripts.

Below are some of the shebangs used for different purposes in shell scripts:

  • #!/bin/sh: It is used to execute the file using sh, which is a Bourne shell, or a compatible shell

  • #!/bin/csh: It is used to execute the file using csh, the C shell, or a compatible shell.

  • #!/usr/bin/perl -T: It is used to execute using Perl with the option for taint checks.

  • #!/usr/bin/php: It is used to execute the file using the PHP command-line interpreter.

  • #!/usr/bin/python -O: It is used to execute using Python with optimizations to code.

  • #!/usr/bin/ruby: It is used to execute using Ruby.

    (1) Write a Shell Script which prints I will complete #90DaysOofDevOps challenge

  • (2) Write a simple if else program using shell scripting

    • (3) Write a shell script to take input from arguments

      • This is a Bash script that uses the 'find' command to search for a file or directory with a given name in the entire file system ('/'). The name of the file or directory is passed to the script as the first argument ('$1') and is case-insensitive ('-iname').

        The '2> /dev/null' part redirects any error messages (such as "Permission denied" or "No such file or directory") to the null device, which discards them and prevents them from being displayed on the terminal.

        Overall, the script searches for a file or directory with the given name in the entire file system, and only displays the path to the file or directory if it is found and accessible by the user running the script.

        To use this script, save it as a file (e.g. 'search.sh'), make it executable with the 'chmod +x search.sh' command, and then run it with the name of the file or directory to search for as the argument (e.g. './search.sh myfile').

  • (4) Write a Bash script that prompts the user for their username, compares it to the current user's username, and if they match, runs the top command to display real-time information about their running processes.

“The woods are lovely, dark and deep,
But I have promises to keep,
And miles to go before I sleep,
And miles to go before I sleep.”

- Rudyard Kipling ❦❦❦