Skip to main content

Command Palette

Search for a command to run...

#Day 04 - Shell Scripting for DevOps

Published
3 min read
#Day 04 - Shell Scripting for DevOps
A

Passionate AWS Developer | DevOps Engineer with a strong background in cloud architecture and solutions engineering. Leveraging the power of Amazon Web Services (AWS), knowledge of the AWS global infrastructure, design and implement robust cloud-based solutions that align with clients' specific needs.

A shell script is a computer program designed to be run by the Unix/Linux shell which could be one of the following:

  • The Bourne Shell

  • The C Shell

  • The Korn Shell

  • The GNU Bourne-Again Shell

Shell scripting enables automation of deployment, configuration management, CI/CD pipelines, monitoring, logging, and performance optimization tasks.

What is Kernal?

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 to the user to use operating system services. Shell accepts human-readable commands from a user and converts them into something that the kernel can understand. It is a command language interpreter that executes commands read from input devices such as keyboards or from files. The shell gets started when the user logs in or starts the terminal.

What is Linux Shell Scripting?

A shell script is a computer program designed to be run by a Linux shell, a command-line interpreter. The various dialects of shell scripts are considered to be scripting languages. Typical operations performed by shell scripts include file manipulation, program execution, and printing text.

Task 01:

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

Shell scripting languages, such as Bash, are used for automating tasks in DevOps workflows. They provide a command-line interface for interacting with the operating system and executing commands.

Shell scripting for DevOps enables automation of deployment, configuration management, CI/CD pipelines, monitoring, logging, and performance optimization tasks.

Shell scripting is the use of scripting languages, such as Bash, to automate tasks and execute commands in an operating system for improved efficiency and consistency.

Examples:

Print "Hello world" example.


#!/bin/sh
echo "Hello world"

Task 02:

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

#!/bin/bash is known as The Shebang. The shell scripting starts with #!/bin/bash as first line. Shebang is a collection of characters or letters that consist of a number sign and exclamation mark, that is (#!) at the beginning of a script.

#!/bin/bash when used in script instructs the operating system to use bash as a command interpreter. It is the default shell assigned by Linux-based operating systems.

#!/bin/sh used in scripts instructs the internal system shell to start interpreting scripts. It is used to execute the file using sh, which is a Bourne shell, or a compatible shell

A script comprises several UNIX commands. Now, in each of the scripts, users are required to explicitly specify the type of shell they want to use to run their scripts. Now to explicitly specify the type of shell used by the script, Shebang is used. So we can use shebang, that is, #!/bin/bash at the start or top of the script to instruct our system to use bash as a default shell.

Task 03:

Write a Shell Script which prints I will complete #90DaysOofDevOps challenge.

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

Task 04:

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

vim userinput.sh

bash userinpt.sh
#!/bin/bash
echo "What is your name?"
read name
echo "Who is $name?"

Task 05:

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

vim comparenum.sh

#!/bin/bash
# Read user input and store it in the varibale 'num1'
read -p "Enter the first number:" num1
# Read user input and store it in the variable 'num2'
read -p "Enter the second number" num2

# Compare the two numbers
if [ $num1 -eq $num2 ];then
        echo "Both numbers are equal."
elif [ $num1 -lt $num2 ]; then
        echo "$num1 is less than $num2."
else
        echo "$num1 is greater than $num2."
fi

More from this blog

Akarsha's blog

28 posts