Day 5 - Advanced Linux Shell Scripting for DevOps Engineers with User management
Day-05 #90daysofDevops Challenge
Task 1: Create a Script to Create Directories with Dynamic Names
Here's the script createDirectories.sh
that creates directories based on the given arguments (name prefix, start, and end numbers):
#!/bin/bash
# Check if the correct number of arguments are provided
if [ $# -ne 3 ]; then
echo "Usage: $0 <prefix> <start> <end>"
exit 1
fi
# Assign arguments to variables
PREFIX=$1
START=$2
END=$3
# Validate that START and END are integers
if ! [[ $START =~ ^[0-9]+$ ]] || ! [[ $END =~ ^[0-9]+$ ]]; then
echo "Error: START and END must be integers."
exit 1
fi
# Validate that START is less than or equal to END
if [ $START -gt $END ]; then
echo "Error: START must be less than or equal to END."
exit 1
fi
# Loop to create directories
for i in $(seq $START $END); do
DIRECTORY_NAME="${PREFIX}${i}"
mkdir -p $DIRECTORY_NAME
done
echo "Directories created successfully."
To use this script, save it as createDirectories.sh
, give it execute permissions, and then run it with the required arguments. For example:
chmod +x createDirectories.sh
./createDirectories.sh day 1 90
This will create directories named day1
through day90
.
Task 2: Script to Backup Work
A simple script for backing up files can look like this:
#!/bin/bash
# Variables
BACKUP_DIR="backup_$(date +%Y%m%d_%H%M%S)" # Unique backup directory
SOURCE_DIR="/path/to/your/working/directory" # Replace with your working directory path
# Create backup directory
mkdir -p $BACKUP_DIR
# Copy files to backup directory
cp -r $SOURCE_DIR/* $BACKUP_DIR/
echo "Backup completed. Files saved in: $BACKUP_DIR"
To use this script, save it as backup.sh
, give it execute permissions, and run it:
chmod +x backup.sh
./backup.sh
Automating Backup with Cron
To automate the backup script, you can create a cron job to run it at a specific interval. Edit your crontab with crontab -e
, and add a line like the following to run the backup script every day at midnight:
0 0 * * * /path/to/backup.sh
This will run the backup script every day at midnight.
Task 3: Creating and Displaying Usernames
To create two users in Linux, you can use the adduser
command. Make sure to run it with superuser privileges. Here's an example:
# Create two users
sudo adduser user1
sudo adduser user2
# Display their usernames
echo "Created users:"
awk -F: '/user1|user2/ {print $1}' /etc/passwd
This snippet creates two users user1
and user2
, and then displays their usernames from the /etc/passwd
file.
Conclusion
You now have scripts for creating directories with dynamic names, a script for backing up your work, and examples of creating and displaying new user usernames. Consider creating a blog post to document your process and learnings from this task.
Thank you for reading our DevOps blog post. We hope you found it informative and helpful. If you have any questions or feedback, please don't hesitate to contact us.
I hope this helps!
Happy Learningโจ