Day 3: Shell scripting and few insights

Shell Scripting for DevOps

Shell scripting is a powerful tool for DevOps engineers, allowing them to automate repetitive tasks, manage infrastructure, and streamline deployment processes. It essentially involves writing a series of commands that the shell (usually Bash on Linux/Unix systems) interprets and executes line by line.

Here are some of the key benefits of using shell scripting in DevOps:

  • Automation: Automating repetitive tasks like system configuration, software installation, and file management saves DevOps engineers time and reduces the risk of human error.

  • Efficiency: Scripts can execute complex workflows quickly and consistently, improving overall efficiency.

  • Repeatability: Once a script is written and tested, it can be reused for similar tasks across different environments.

  • Integration: Shell scripts can be integrated with other DevOps tools like configuration management systems and CI/CD pipelines for a more cohesive workflow.

Examples of Shell Scripting in DevOps:

  • Installing application dependencies: A script can automatically download and install the necessary libraries or frameworks needed for an application to run.

  • Provisioning new servers: Scripts can configure new servers with the desired operating system settings, software packages, and security policies.

  • Deploying application updates: Scripts can automate the process of fetching the latest application code, restarting services, and migrating databases during deployments.

  • Monitoring system health: Scripts can be used to collect system metrics like CPU usage, memory consumption, and disk space, and trigger alerts if thresholds are exceeded.

#!/bin/bash vs. #!/bin/sh

  • #!/bin/bash: This line, also known as the "shebang," specifies the interpreter to be used for the script. In this case, /bin/bash is the full path to the Bash shell interpreter. Using bash ensures the script has access to all Bash features and functions.

  • #!/bin/sh: This shebang specifies the generic Bourne shell interpreter (/bin/sh). While some simple scripts might work with both, sh might not support all Bash features. It's generally recommended to use #!/bin/bash for portability and access to the full range of Bash functionality.

Shell Script Examples:

  1. Printing a Message (90 Days of DevOps Challenge):

Bash

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

Use code with caution.

content_copy

  1. Taking User Input and Arguments:

Bash

#!/bin/bash
# Get user input
read -p "Enter your name: " name

# Get argument (if provided)
argument="$1"  # "$1" refers to the first argument passed to the script

# Print variables
echo "Hello, $name!"
if [[ -n "$argument" ]]; then
  echo "Argument provided: $argument"
fi

Use code with caution.

content_copy

  1. If-Else Example (Number Comparison):

Bash

#!/bin/bash
read -p "Enter the first number: " num1
read -p "Enter the second number: " num2

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