Part 4: User Commands and Actual Use of Shell Scripting by DevOps Engineers
Continuing from the previous part, we will explore more advanced topics in shell scripting, starting with conditional statements, loops, and functions. These concepts are essential for DevOps engineers to automate tasks and improve efficiency.
If-Elif Statement in Shell Scripting
The if-elif
statement allows you to execute different blocks of code based on multiple conditions. Here's the syntax:
if [ condition ]; then
# Code to execute if condition is true
elif [ another_condition ]; then
# Code to execute if another_condition is true
else
# Code to execute if none of the conditions are true
fi
Example:
#!/bin/bash
read -p "Enter a number: " num
if [ $num -gt 10 ]; then
echo "The number is greater than 10."
elif [ $num -eq 10 ]; then
echo "The number is equal to 10."
else
echo "The number is less than 10."
fi
For Loops in Shell Scripting
A for
loop allows you to iterate over a sequence of items or files, making it ideal for repetitive tasks.
Example: Iterating Files
To create multiple files in a single command, you can use:
touch file-{2..10}.txt
Using a for
loop, you can perform operations on files. For instance, printing all files in the directory:
#!/bin/bash
for file in *; do
echo "$file"
done
Example: Finding Only .txt
Files
#!/bin/bash
for file in *.txt; do
echo "$file"
done
Functions in Shell Scripting
Functions allow you to encapsulate reusable blocks of code. Let's look at an example of adding a user to an Ubuntu system using a shell function.
Example: Adding a User
#!/bin/bash
add_user() {
read -p "Enter the username to add: " username
sudo useradd $username
echo "User $username added successfully."
}
add_user
To execute this script, you need superuser privileges. Run the script with the following command:
sudo ./add_user.sh
Viewing All Users
To see all users on the system, use:
cat /etc/passwd
Backing Up Files
Here's a script to back up files using tar
and gzip
, with comments explaining each line:
#!/bin/bash
# Define the directory to back up
backup_source="/path/to/source"
# Define the backup destination
backup_dest="/path/to/backup"
# Define the backup file name with a timestamp
backup_file="$backup_dest/backup_$(date +%Y%m%d%H%M%S).tar.gz"
# Create the backup directory if it doesn't exist
mkdir -p $backup_dest
# Create a tarball and compress it using gzip
tar -czf $backup_file $backup_source
# Print completion message
echo "Backup completed successfully. Backup file: $backup_file"
Explanation:
Define the directory to back up: Set the source directory path.
Define the backup destination: Set the target backup directory path.
Define the backup file name: Include a timestamp for unique and organized backups.
Create the backup directory if it doesn't exist: Use
mkdir -p
to ensure the directory is created.Create a tarball and compress it: Use
tar -czf
to archive and compress the files.Print completion message: Inform the user that the backup process is complete and display the backup file name.
These examples illustrate how DevOps engineers use shell scripting to perform tasks such as user management, file manipulation, and automation. Try these scripts in your environment to gain hands-on experience!