Advanced Linux Shell Scripting for DevOps Engineers with User management
90dayofdevops day 5
Task 1: Script to create multiple directories.
Used Virtual box, and Linux Ubuntu 20.04 for this task.
Created a file name “createdirectories.sh“ using the command
touch createdirectories.sh
Moved on to modify the script
vim createdirectories.sh
pressed “i“ to enable the write mode in the file.
Wrote the required code to accomplish the task
Code done
#!/bin/bash
dir_name=$1
start=$2
end=$3
for ((i=start; i<=end; i++))
do
mkdir "$dir_name$i"
done
Output results
Task 2: Script to backup all your work done till now.
touch backup-job.sh
vim backup-job.sh
pwd
vim backup-job.sh
chmod +x backup-job.sh
ls
./backup-job.sh
ls
clear
The script used in the backup-job.sh
#!/bin/bash
src_dir=/home/devbox/
tgt_dir=/home/devbox/
curr_timestamp=$(date "+%Y-%m-%d-%H-%M-%S")
backup_file=$tgt_dir/$curr_timestamp.tgz
echo "Taking backup on $curr_timestamp"
tar -cvzf $backup_file --absolute-names $src_dir
echo "Backup complete"
Task 3: What are Cron and Crontab
Cron is a tool that helps you schedule tasks to run automatically on your computer at specific times or intervals. You can use it to run programs, scripts, or other tasks without having to do them manually every time.
Crond is a program that checks the crontab file to see what tasks should be run and then runs them at the scheduled times.
Crontab is a command that you can use to create and edit the list of tasks that cron will run. You can set the time, date, and frequency of each task so that it runs exactly when you want it to.
The following are 5 basic crontab commands;
crontab -e: This command allows you to edit your crontab file. It opens the crontab file in your default text editor, where you can add, modify, or remove tasks.crontab -l: This command lists all the tasks currently scheduled in your crontab file. It is useful for checking that your tasks are scheduled correctly.crontab -r: This command removes all tasks from your crontab file. Use this with caution, as it will delete all your scheduled tasks.crontab -u username: This command allows you to view, edit, or delete the crontab file of another user on the system. Replace "username" with the name of the user whose crontab file you want to access.crontab file: This command allows you to install a new crontab file. Replace "file" with the path to the crontab file that you want to install. This can be useful if you have multiple crontab files or if you want to restore a previously saved crontab file.
Task 4: User Management in Linux
In Linux, user management involves creating and managing user accounts that can be used to log in to the system and perform various tasks. Here are some of the basic commands and concepts related to user management in Linux:
adduseroruseradd: Create a new user account.passwd: Change the password for a user account.usermod: Modify an existing user account.userdel: Delete a user account.sudo: Run commands with higher privileges.
Task 5: Creating users in linux and displaying Usernames
#command user to add user
sudo useradd dev-user1
#command user to add user
sudo useradd dev-user2
# extracts the last two lines of the /etc/passwd file, which lists information about user accounts on the system.
tail -n 2 /etc/passwd | cut -d: -f1
OR
# Get a list of usernames that match the username provided
cut -d: -f1 /etc/passwd | grep -E 'dev-user1|dev-user2'
Example - 1

Example - 2
