4.Basic Linux Shell Scripting for DevOps Engineers(Day-4)

4.Basic Linux Shell Scripting for DevOps Engineers(Day-4)

What is Kernel ?

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 special user program which provide an interface to user to use operating system services. Shell accept human readable commands from user and convert them into something which kernel can understand. It is a command language interpreter that execute commands read from input devices such as keyboards or from files. The shell gets started when the user logs in or start 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 (Day-4)

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

Shell scripting is a powerful tool used in DevOps for automating various tasks involved in software development, deployment, and maintenance. DevOps is a set of practices and tools that aim to bridge the gap between software development and operations teams. Shell scripting plays a vital role in this process by allowing developers and operations teams to automate tasks, streamline processes, and improve efficiency.

A shell script is a series of commands written in a scripting language that can be executed by a shell program. The shell program is a command-line interpreter that provides a user interface for interacting with the operating system. In DevOps, shell scripts can be used to automate various tasks such as building and deploying applications, configuring servers, managing databases, monitoring systems, and more.

Some common uses of shell scripting in DevOps include:

  1. Automating builds and deployments: Shell scripts can be used to automate the process of building and deploying software applications. This includes compiling code, packaging binaries, and deploying the application to production servers.

  2. Configuration management: Shell scripts can be used to configure servers and applications. This includes setting environment variables, installing software packages, and configuring system settings.

  3. Infrastructure management: Shell scripts can be used to manage infrastructure resources such as virtual machines, containers, and cloud services. This includes provisioning new resources, scaling resources up or down, and managing security settings.

  4. Monitoring and logging: Shell scripts can be used to monitor systems and applications and generate logs for debugging and troubleshooting purposes.

Overall, shell scripting is an essential skill for DevOps professionals as it allows them to automate routine tasks, improve efficiency, and reduce errors.

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

    #!/bin/bash is called a shebang or a hashbang. It's a special sequence of characters that tells the shell which interpreter to use to execute the script. In this case, #!/bin/bash specifies that the script should be run using the Bash shell.

    Yes, you can also use #!/bin/sh instead of #!/bin/bash to specify that the script should be run using the default shell on the system. On most Linux distributions, /bin/sh is a symlink to /bin/bash, which means that using #!/bin/sh or #!/bin/bash will result in the same behavior. However, there may be some subtle differences between the two shells, so it's important to choose the appropriate interpreter for your script based on your needs.

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

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

Save the above code into a file named 90daysofdevops.sh and make it executable using the chmod command:

chmod +x 90daysofdevops.sh

Then, run the script using the following command:

./90daysofdevops.sh

or

bash 90daysofdevops.sh

or

sh 90daysofdevops.sh

or

. 90daysofdevops.sh

This will print the message "I will complete #90DaysOfDevOps challenge" to the terminal.

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

#!/bin/bash

# Take user input
echo "Enter your name: "
read name

# Take input from arguments
echo "Enter your age: "
read age

# Print the variables
echo "Your name is: $name"
echo "Your age is: $age"

Save the above code into a file named input_variables.sh and make it executable using the chmod command:

chmod +x input_variables.sh

Then, you can run the script and provide the age as a command-line argument:

./input_variables.sh
  1. Write an Example of If else in Shell Scripting by comparing 2 numbers
#!/bin/bash

# Take user input for two numbers
echo "Enter the first number: "
read num1
echo "Enter the second number: "
read num2

# Compare the two numbers using if-else statement
if [ $num1 -gt $num2 ]
then
  echo "$num1 is greater than $num2"
elif [ $num1 -eq $num2 ]
then
  echo "$num1 is equal to $num2"
else
  echo "$num1 is less than $num2"
fi

Save the above code in a file, let's say compare_numbers.sh. You can then execute the script by typing ./compare_numbers.sh in the terminal. The script will prompt you to enter two numbers, and it will compare the numbers using an if-else statement.