Day 3: Linux shell scripting for DevOps
Every DevOps Engineer must have a good understanding of Linux and Shell scripting. Shell is a program that provides a command line interface to interact with the operating system.
1: What is Shell scripting in DevOps?
Shell scripting is very important for DevOps in automating day-to-day activities such as software development and deployment. Its application runs on top of the kernel and provides a command line interface to the system, so a shell script is a plain text that contains the command that a shell interpreter can execute. There are several shells available for Linux: a) BASH (Born again shell): It is one of the most commonly used UNIX/LINUX shells and it is the default shell in many Linux distributions. b) CSH (c shell): C shell's usage is similar to C programming language. c) KSH (korn shell): It is also known as POSIX shell, which allows you to create aliases to customize commands.
2: What is #!/bin/bash?
#!/bin/bash: It is called as Shebang line, which is used to instruct the computer to use a command interpreter when running the script. #!/bin/sh: It is a system's default shell, depending on the system it can be bash or a different compatible shell.
Note: #!/bin/bash has more advanced features than #!/bin/sh.
We can find a basic example of shell scripting below for our reference. Example: Write a Shell Script that prints We will complete the #45DaysOofDevOps challenge,
You can run a bash script from the terminal or by executing any bash file.
Run the following command from the terminal to execute a very simple bash statement. The output of the command will be "We will complete the #45Daysofdevopschallenge"
.
Write a Shell Script to take user input, input from arguments, and print the variables. The below command will get the user input(username) from the argument. The output of the command will print the variables.
Write an Example of If else in Shell Scripting by comparing 2 numbers -gt. This stands for “greater than”, then command output compares the 2 numbers and displays the output using if-else statements.
3: Variables in shell scripting:
Variables are used to store data temporarily. To get the value of a variable, the variable name is prefixed with a dollar sign ($).
4: Arguments in shell scripting:
Shell Script can take the arguments that are passed when executing the script from the command prompt.
Syntax: ./<script_filename>arg1 arg2
Special variables are $0, $1 and $2:
$0 - The name of the scripts
$1 - First argument that is passed from the command line.
$2 - Represents the second argument.
TASK 1: The script is executed with three given arguments (one is the directory name, the second is the start number of directories and the third is the end number of directories) it creates a specified number of directories with a dynamic directory name.
After the execution of the script: ./createDirectories.sh Day 1 90, will create 90 directories.
TASK 2: Backup all the works. Backups are an important part of DevOps Engineers' day-to-day activities. To save the file from unnecessary failure, backup ensures data recovery and automates data safety. We will take backup by compressing and archiving a source and target directory location. tar
the command helps to compress and archive the file with options.
-c : Creates Archive -v : Displays Verbose Information -f : creates archive with given filename.
The backup is stored in the target directory.
TASK 3: Cron and Crontab, to automate the backup Script
Cron
is a time-based task scheduler. We will automate the above backup script to run at specific intervals of time. crontab
is a file containing all schedules of the cron jobs a user wants to run regularly.
- First, use the crontab command to create your first crontab
Note:5 * cat /path/to/your/backup.sh - Replace your script path with actual backup.sh path
The backup will automatically run at a given specific time.
5: User Management:
A user is an entity, in a Linux operating system, that can manipulate files and perform several other operations. Each user is assigned an ID that is unique for each user in the operating system.
Useradd
- To create a User.
userdel
- To delete a user.
usermod
- To modify user properties.
id
- To display user information.
In the below task, we will create 2 users and display their names.
The command will fetch the usernames from /etc/passwd
I hope you enjoyed this guide on learning Shell scripting for DevOps engineers.
Happy Learning!
Subscribe to my newsletter
Read articles from RAKESH REVASHETTI directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
RAKESH REVASHETTI
RAKESH REVASHETTI
I'm Rakesh, a DevOps engineer who enjoys automation, continuous integration and deployment. With good hands-on experience in DevOps & cloud technology, i'm proficient in various tools and technologies related to infrastructure automation, containerization, cloud platform, monitoring and CI/CD.