Day 4 Task: Basic Linux Shell Scripting.

Day 4 Task: Basic Linux Shell Scripting.

·

2 min read

Content :-

  • What is shell scripts for devOps ?

  • What is #!/bin/bash ?

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

  • An Example of If else in Shell Scripting by comparing 2 numbers.

What is shell scripts for devOps ?

Shell scripts play a pivotal role in DevOps by automating a myriad of tasks throughout the software development lifecycle and deployment processes. They are written in shell languages like Bash or PowerShell and are instrumental in achieving several objectives. Firstly, they automate repetitive tasks such as building, testing, deploying, and monitoring software, thereby enhancing efficiency and reducing manual errors. Additionally, shell scripts aid in configuration management by configuring servers, installing dependencies, and managing environment variables across different environments. In the realm of CI/CD pipelines, they trigger builds, run tests, package applications, and deploy artifacts, facilitating a seamless delivery process. Furthermore, shell scripts are invaluable for monitoring system health, analyzing logs, and generating reports, ensuring the smooth operation of applications. They also play a crucial role in Infrastructure as Code (IaC), enabling the provisioning and management of cloud resources.

What is #!/bin/bash ?

The line #!/bin/bash at the beginning of a script file is called a shebang or hashbang. It's a special syntax recognized by Unix-like operating systems, including Linux and macOS.

Here's what it means:

  • #!: This sequence is called the shebang or hashbang.

  • /bin/bash: It specifies the path to the interpreter that should be used to execute the script. In this case, /bin/bash refers to the Bash shell interpreter.

When you run a script file with the shebang #!/bin/bash, the operating system knows to use the Bash shell interpreter located at /bin/bash to execute the commands in the script. This shebang line essentially tells the system which interpreter to use for running the script, allowing you to write scripts in Bash or other scripting languages and have them executed properly.

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

#!/bin/bash

Taking user input

echo "Enter your name:" read username

Input from command-line arguments

arg1=$1 arg2=$2

Printing the variables

echo "User input: $username"

echo "First argument: $arg1"

echo "Second argument: $arg2"

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

#!/bin/bash

Assigning values to variables

num1=10 num2=20

Comparing the two numbers

if [ $num1 -gt $num2 ]; then

echo "$num1 is greater than $num2"

elif

[ $num1 -lt $num2 ]; then

echo "$num1 is less than $num2"

else

echo "$num1 is equal to $num2"

fi