Day 5 Task: Advanced Linux Shell Scripting for DevOps Engineers with User management
Table of contents
No headings in the article.
#Task1: Write a bash script createdirectories.sh. When the script is executed with given arguments (three rectory names, start number and end number) it creates a specified number of directories with a dynamic directory name.
#!/bin/bash
# Check if three arguments are provided
if [ "$#" -ne 3 ]; then
echo "Usage: $0 <directory_name> <start_number> <end_number>"
exit 1
fi
# Get the arguments
directory_name=$1
start_number=$2
end_number=$3
# Function to create directories
create_directories() {
for ((i=start_number; i<=end_number; i++)); do
dir_name="${directory_name}_${i}"
mkdir -p "$dir_name"
echo "Created directory: $dir_name"
done
}
# Call the function
create_directories
To run the script:
Copy the above script into a text editor.
Save the file with the name
createDirectories.sh
Open your terminal or command prompt.
Navigate to the directory where you saved the script.
Give execute permission to the script:
chmod 777 createDirectories.sh
- Run the script with three arguments (directory name, start number, and end number):
./createDirectories.sh Day 1 90
The script will create 90 directories with names like Day_1
, Day_2
, and so on, up to Day_90
. Each directory will be created in the current working directory.
#Task2: Create a Script to backup all your work done till now.
#!/bin/bash
# Check if the directory name is provided as an argument
if [ $# -ne 1 ]; then
echo "Usage: $0 <directory_name>"
exit 1
fi
# Extract the directory name from the argument
directory_name="$1"
# Validate if the directory exists
if [ ! -d "$directory_name" ]; then
echo "Error: Directory '$directory_name' not found."
exit 1
fi
# Create a backup directory with the current date and time
backup_dir="backup_$(date +%Y%m%d_%H%M%S)"
# Create the backup directory
mkdir "$backup_dir" || { echo "Failed to create backup directory."; exit 1; }
# Copy the content of the specified directory to the backup directory
cp -r "$directory_name"/* "$backup_dir" || { echo "Failed to create backup."; exit 1; }
echo "Backup created successfully at: $backup_dir"
Give execute permission to the script:
chmod 777 backup_script.sh
Run the script with the directory name you want to back up, for example:
./backup_script.sh my_directory
The script will create a backup of the specified directory, ensuring your important data is safe and secure.
#Task3: Read About Cron and Crontab, to automate the Script
Cron is the system's main scheduler for running jobs or tasks unattended. A command called crontab allows the user to submit, edit or delete entries to cron. A crontab file is a user file that holds the scheduling information.
You can use the following command to check the crontab list :-
crontab -l
If you want to create a new cron job you use the following command and enter in text editor to add a new cron entry.
crontab -e
It is possible to create jobs that you want to reoccur. This process is known as job scheduling. This process is handled by the cron service or a daemon called crond. and crontab -r
to remove the current crontab configuration.
#Task4: Read about User Management
User is an entity that can manipulate files and perform serveral other operations. Each user in a Linux operating system is assigned an id. After installation of the OS, ID 0 is assigned to the root user. ID 1–999 are assigned to system users and ID from 1000 onwards are assigned to local user.
1. Command to get id of a user id username
2.Command to add a user sudo useradd username
3.Command to assign password to a user passwd username
4.Command to access user configuration cat /etc/passwd
5.Command to delete a user userdel -r username
6.For switching user account su username
#Task5: Create 2 users and just display their Usernames
To create a user, we can use the following command:
Create the first user:
sudo adduser demo1
Create the second user:
sudo adduser demo2
Display the usernames:
echo "User 1: user1"
echo "User 2: user2"
Thank you for reading!
Contact me at :
linkedin: https://www.linkedin.com/in/sutish-pradhan/
E-mail: psutish@gmail.com