🐚Day 4: Fundamental of Shell Scripting! πŸš€

Β·

2 min read

🐚Day 4:  Fundamental of Shell Scripting! πŸš€

πŸ€”What is Shell Scripting?

First a fall, Shell is a command line interface used to communicate with the operating system. Shell scripting in Linux is the process of automating day-to-day or regular tasks on a Linux computer or elsewhere to improve efficiency.

🎼 Purpose of Shell Scripting in DevOps:

-> The tasks that Shell Script performs in DevOps are:

  • πŸ”„πŸ–₯️ Monitoring Node Health: Checking the performance and status of a node (a computer or device) using the β€œTOP” command or custom scripts.

  • πŸ“‚πŸ‘¨β€πŸ’» Code Management: Managing the source code of a software project using tools like Git or SVN.

  • βš™οΈπŸ”§ Configuration Management: Managing the configuration of a software system using tools like Ansible or Puppet.

  • πŸŒπŸ—οΈ Infra Management: Managing the infrastructure of a software system using tools like AWS or Azure.

πŸ“œ What is#!/bin/bash? can we write#!/bin/shas well?

The #!/bin/bash or #!/bin/sh is the shebang line at the beginning of a script, indicating the interpreter to use. previously, #!/bin/bash and #!/bin/sh are the same, if back to the history Linux redirects #!/bin/sh to #!/bin/bash using linking concept. even though the request is taken by "sh" but forwarded to "bash".

While bash and sh are slightly difference in terms of syntax. for example, way of writing "for Loops" in bash is totally different to way of writing in sh. However, using bash allows access to more features.

What is the Difference Between Using bash and sh? – Its Linux FOSS

Source: https://itslinuxfoss.com

🌟 Examples of Shell Scripting:

1) Shell Script which prints "I will complete #90DaysofDevOps challenge"?

Syntax:

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


2) Shell Script to take user input, input from arguments, and print the variables.

Syntax:

#!/bin/bash
echo "Enter your name:"
read username
echo "Hello, $username! You entered: $1 from arguments."


3) Shell Scripting comparing 2 numbers using the "if-else" condition:

Syntax:

#!/bin/bash

echo "Enter number 1"
read num1

echo "Enter number 2"
read num2

if [ "$num1" -gt "$num2" ]; then
        echo "$num1 is greater than $num2"
else
        echo "$num1 is lesser than $num2"
fi

Notes:

  • Use "$num1" and "$num2" to ensure that variables are properly enclosed in quotes, preventing issues with empty or null variables.

  • Use -gt for numeric comparison.

Happy Coding :) 🌐✨

πŸ”— Let's Connect..!

🌐LinkedIn

🌐Twitter

🌐GitHub

Β