Advanced Linux Shell Scripting for DevOps Engineers with User management: (Day-5)
#90 days of DevOps challenge- Day5

I am continue learning variety of DevOps technologies, including AWS, Linux, Python, Shell Scripting, Docker, Terraform, Jenkins, Git/GitHub, and Computer Networking. I have a strong ability to troubleshoot and resolve issues, and are consistently motivated to expand the knowledge and skills through exploration of new technologies.
Let's start our first task:
Create a series of directories with the help of a shell script using run-time arguments.
Question: Why do we need to create these kinds of directories:
The answer is, suppose we got a request to create 100 directories in a particular location. It will be a very time taking task and maybe we made some mistakes right? so reduce the time and get the accuracy we used shell script.
Now let's jump to our terminal and create a shell script for the same.
$ sudo nano runtargs.sh
#!/bin/bash
# Check if the arguments are 3 or not.
if [ "$#" -ne 3 ]
then
echo "Usage: $0 dir_name start_num end_num "
exit 1
fi
# Define the arguments to variables
dir_name="$1"
start_num="$2"
end_num="$3"
#loop through by the range of directory numbers
for (( i="$start_num"; i<="$end_num"; i++ ));
do
mkdir "$dir_name$i"
done
echo "Your directories are created below"
ls
Now run the script.
$ ./runtargs.sh
Usage: ./runtargs.sh dir_name start_num end_num
Now pass the arg.
$ ./runtargs.sh file 1 10
Your directories are created below
file1 file10 file2 file3 file4 file5 file6 file7 file8 file9
Now let's move to our next task:
Create a Backup of my above work
We have a directory called "scripting" and want to take a backup of this directory and want to move it to another path for example I will use the target path of my backup "/backup" from "/prod/scripting" and I want to use the backup file name as today's date
Now let's jump to the terminal.
# create a bkp.sh file
sudo nano bkp.sh
Now write down below code
#!/bin/bash #these tags are script starting tag
src_dir=/prod/scripting #souce of the directory
tgt_dir=/backup #target/destination of backup file
# now get current time stamp by below commands
current_timestamp=$(date "+%Y"-"+%m"-"+%d"-"+%H"-"+%M"-"+%S")
#defining backup file name and taking backup
backup_file=$tgt_dir/$current_timestamp.tgz
echo "Taking backup on $current_timestamp"
echo "$backup_file"
#Now create a tar file to compress it.
tar czf $backup_file --absolute-names $src_dir
echo "Backup Completed"
Here is the output.
/backup# la
2023-+04-+01-+15-+09-+10.tgz
Now let's do our last task making these tasks automate by a shell script.
Automate these tasks
Before automating the task let's discuss what is cron and crontab.
CRON: Cron is a time-based job scheduler in Unix-like operating systems, which can automatically execute commands or scripts at specified times or intervals. Cron is commonly used for scheduling repetitive tasks, such as backups, system maintenance, and automated tasks.
CRONTAB: A crontab file is a simple text file that contains a list of commands meant to be run at specified times. The
crontabcommand is used to create, modify, and manage the crontab files for individual users. Each line of the crontab file specifies a command or script to run, along with a schedule for when it should be run. The syntax for each line is as follows:
* * * * * command to be executed every minute
- - - - -
| | | | |
| | | | +----- day of the week (0 - 6) (Sunday=0)
| | | +------- month (1 - 12)
| | +--------- day of the month (1 - 31)
| +----------- hour (0 - 23)
+------------- min (0 - 59)
Now let's automate both above tasks and jump to the terminal and crontab.
$ crontab -e
# paste below code at the of the file.
* * * * * /prod/scripting/bkp.sh
* * * * * /prod/scripting/runtargs.sh file 11 13
Here is the backup
/backup# ls 2023-+04-+01-+15-+09-+10.tgz
Here are the new directories.
file11 file12 file13
User Management:

In Linux, user management involves creating, modifying, and deleting user accounts and groups. Here are some commonly used commands for user management in Linux:
useradd: The "useradd" command is used to create a new user account.
passwd: The "passwd" command is used to set or change a user's password.
Here are some common options for the "passwd" command:
"-l": Lock the user's account.
"-u": Unlock the user's account.
"-d": Remove the user's password.
Create 2 users and just display their Usernames:
- To add a user to the Linux system:
useradd devops
- setting user password:
passwd devops
If we want to add a user by making a directory use the -m flag
sudo useradd devops -m
-d dir
Set the user’s home directory to be dir.
-s shell
Set the user’s login shell to be shell.
-u uid
Set the user’s ID to be uid. Unless you know what you’re doing, omit this option and accept the default.
-g group
Set the user’s initial (default) group to group, which can either be a numeric group ID or a group name, and which must already exist.
-G group1,group2,…
Make the user a member of the additional, existing groups group1, group2, and so on.
-m
Copy all files from your system skeleton directory, /etc/skel, into the newly created home directory. The skeleton directory traditionally contains minimal (skeletal) versions of initialization files, like ~/.bash_profile, to get new users started If you prefer to copy from a different directory, add the -k option (-k your_preferred_directory).
To check whether the user added or not if the user added where it should be present:
sudo cat /etc/passwd
Thank you for reading this Blog. Hope you learned something new today! If you found this blog helpful, please like, share, and follow me for more blog posts like this in the future.




