- DevOps Lifecycle -Newspaper WordPress Theme
90DaysOfDevOpsDay 4 - Basic Linux Shell Scripting for DevOps Engineer

Day 4 – Basic Linux Shell Scripting for DevOps Engineer

What is Kernel

A kernel is a central component of an operating system that manages system resources and provides a bridge between software applications and the computer’s hardware. Kernel Allows programs to interact with the computer’s resources such as the CPU, memory, disk drives, and input/output devices.

What is Shell

Shell is a special program that provides a command-line interface (CLI)/ terminal so that users can interact with the operating system through special text commands. A shell allows users to run commands, execute programs, and manipulate files and directories on the system.

What is Linux Shell Scripting

It’s a way of writing commands or a set of instructions in a text file to run in a Linux shell/ terminal. For writing shell scripts we can use different scripting languages such as Bash, Zsh, Csh, Ksh, Fish, etc.

Bash: Bash is the default shell in most Linux distributions and is widely used for writing shell scripts.

What is Shell Scripting for DevOps

Shell scripting is used in DevOps to automate tasks and processes for managing the development and deployment of software applications in a fast-paced and dynamic environment.

Here are some examples of how shell scripting is used in DevOps:

  • Deployment automation: DevOps teams use shell scripts to automate the deployment of software applications to production, testing, and staging environments. These scripts can automate tasks such as copying files, installing dependencies, setting up servers, configuring databases, and more.
  • Infrastructure management: DevOps teams use shell scripts to manage infrastructure as code, which allows them to treat infrastructure as if it were software. Shell scripts can be used to automate the provisioning and configuration of servers, load balancers, databases, and other infrastructure components.
  • Continuous Integration and Continuous Delivery (CI/CD): Shell scripting is used to automate the build, test, and deployment stages of the CI/CD pipeline. This ensures that code changes are thoroughly tested and deployed to production quickly and reliably.
  • Monitoring and logging: DevOps teams use shell scripts to automate the monitoring and logging of software applications and infrastructure. These scripts can be used to collect performance metrics, log files, and other data that can be used to troubleshoot issues and improve system performance.

Overall, shell scripting is a powerful tool to automate repetitive tasks, manage infrastructure as code, and streamline the software development and deployment process

What is #!/bin/bash?

#!/bin/bash is the shebang line and it is written first at the beginning of the script. It is nothing but the absolute path to the bash interpreter. This line is used to specify the interpreter with which the given script will be run by default.

Syntax of shebang line is #!/bin/<shell_interpreter>

Can we write #!/bin/sh as well?

Answer is YES

Some of the most common shell interpreters include:

  • Bash (Bourne-Again SHell)
  • sh (Bourne Shell)
  • csh (C Shell)
  • tcsh (TENEX C Shell)
  • zsh (Z Shell)
  • ksh (KornShell)
  • fish (Friendly Interactive SHell)

Each shell interpreter has its own syntax, features, and capabilities. Bash is a popular shell interpreter in Linux environments and is known for its powerful scripting capabilities.

Write a Shell Script that prints I will complete the #90DaysOfDevOps challenge

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

Write an example of If else in Shell Scripting for comparing 2 numbers

#!/bin/bash 
echo "Just you and Computer number Guess Game!!!"
echo "Please enter your number"
read number

computer=$((1 + RANDOM % 100))
echo "The computer Guess is $computer"

echo "Your choice is $number"

if [ $computer -ge $number ]
then
	echo "Computer won the game"
else 
	echo "You won the GAME!!!"
fi 

Improve the above script to handle multiple conditions and invalid inputs by the users [ Use if-elif-else-fi ]

#!/bin/bash 
echo "Just you and Computer number Guess Game!!!"

echo "Please enter your number"
read number

if ! [[ $number =~ ^[1-9][0-9]?$|^100$ ]]; then
	echo "enter valid number from 1 to 100 only"
	exit 1
fi

computer=$((1 + RANDOM % 100))
echo "The computer Guess is $computer"

echo "Your choice is $number"

if [ $computer -gt $number ]
then
	echo "Computer won the game"
elif [ $number -gt $computer ]; then
	echo "You won the GAME!!!"
else 
	echo "You both are guess same number!!!GREAT!!!"
fi

Hope the blog helps you,  Follow me for more content on Cloud and DevOps. Thank you! 🙏

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Subscribe Today

GET EXCLUSIVE FULL ACCESS TO PREMIUM CONTENT

SUPPORT NONPROFIT JOURNALISM

EXPERT ANALYSIS OF AND EMERGING TRENDS IN CHILD WELFARE AND JUVENILE JUSTICE

TOPICAL VIDEO WEBINARS

Get unlimited access to our EXCLUSIVE Content and our archive of subscriber stories.

Exclusive content

- Advertisement -Newspaper WordPress Theme

Latest article

More article