Day 5 : Advanced Linux Shell Scripting for DevOps Engineers with User management
Exploring Shell Scripting ๐
Welcome to Day 5 of our 90-day DevOps journey! Today, we're diving into shell scripting, an essential skill for automating tasks and managing systems efficiently. Shell scripting is a powerful tool that allows us to write scripts to execute commands sequentially, making our workflows smoother and more efficient. Let's explore some practical shell scripting tasks and learn how they can make our lives easier. ๐
Creating Directories Using Shell Script ๐
One of the most common tasks in DevOps is managing directories. Let's write a bash script called createDirectories.sh
that, when executed with three arguments (directory name, start number, and end number), creates a specified number of directories with dynamic names.
A bash shell script have parameters. These parameters start from $1 to $9.
When we pass arguments into the command line interface, a positional parameter is assigned to these arguments through the shell.
FOR LOOP :
You can use for loop in multiple ways . One of the way used is shown below
Syntax :
The syntax is as follows:
The syntax is as follows:
for (( EXP1; EXP2; EXP3 ))
do
shell-command-1
shell-command-2
done
Here's the bash script createDirectories.sh
:
OUTPUT :
Example 2: When the script is executed as
./
createDirectories.sh
Movie 20 50
then it creates 50 directories as Movie20 Movie21 Movie23 ...Movie50
Task 2: Back Up Your Work ๐พ
Backups are crucial in DevOps to prevent data loss. Let's create a script to back up all your work.
The Backup Script ๐
Here's a simple backup script:
source_dir
: This variable holds the path to the directory that contains the work you want to back up. Its input is been taken by the user+"%Y-%m-%d-%H-%M%-S"
: This format string tells date
to output the current date and time in the format YYYYMMDDHHMMSS
. For example, 2024-07-08-12-34-56
represents July 8, 2024, at 12:34:56 PM.
Task 3: Automate Backups with Cron ๐ฐ๏ธ
Automating your backup script ensures regular backups without manual intervention. We'll use cron
and crontab
for this.
Setting Up a Cron Job ๐
Edit your crontab file:
crontab -e
.Add the following line to run the backup script every day every minute
Below backup files are generated every minute as per cron job
Task 4: User Management ๐ฅ
In Linux, managing users is an essential skill. Users are entities that can perform various operations, including manipulating files and running programs. Let's write a bash script to automate the creation of two users and display their usernames.
The Script ๐
Here's the bash script createUsers.sh
:
Conclusion ๐
Congratulations on completing Day 5 of your DevOps journey! Today, you learned how to create directories dynamically using a shell script, back up your work, automate backups with cron
, and manage users in Linux. Keep experimenting and practicing these skills.
See you on Day 6! ๐
Feel free to reach out with any questions or thoughts in the comments below. Happy DevOps-ing! ๐