Lets do some handson on Shell Scripts !!


Task 1: Create Directories with the use of shell scripts!!
Write a bash script create directories.sh that when the script is executed with three given arguments (one is the directory name second is the start number of directories and third is the end number of directories ) it creates a specified number of directories with a dynamic directory name.
Please find the shell script:
#!/bin/bash
# Write a string
echo "Enter the directory string"
read string
# Write a range of directories!!
echo "Enter the start number"
read startnumber
echo "Enter the end number"
read endnumber
# Add a recursive for loop statement"
for ((i=startnumber; i<=endnumber; i++)); do
mkdir -p "${string}${i}"
done
In this way, we can create multiple directories using the shell script and I have given arguments that are user-defined.
There are multiple directories created using one shell script within a few seconds. So that is the power of scripting !!
Task 2: Create a Script to back up all your work done till now.
Data is an important and crucial part and hence backups are essential for safeguarding data, ensuring business continuity, and protecting against data loss and disaster recovery as well. If any data is lost then we cannot create huge amount of the data, so backups are used to create multiple copies and we can retrieve it if anything wrong happens.
Here I have written one shell script, which will create the backup of the directory in compressed format,
#!/bin/bash
source_file="/home/ubuntu/shell_script"
destination_file="/var/tmp/shell_scriptbkp"
timestamp=$(date +"%d-%m-%y-%H-%M-%s")
echo $timestamp
backup_dir="${destination_file}/backup_${timestamp}"
echo $backup_dir
tar -czf "${backup_dir}.tar.gz" "$source_file"
echo "Backup completed"
I have executed the above script and yeah, backup is done in the destination directory.
Crontab and Cron:
There are many times, we need to do repeated tasks, every time in server we cannot go and execute the commands in the specific time zone, so cron is a technique where we need to schedule the commands and scripts and it executes for us in the scheduled time.
Crontab (short for "cron table") is a file that contains the list of scheduled tasks or cron jobs for a user. Users can create and manage their cron jobs using the crontab
command-line tool.
The syntax for a crontab entry is composed of five fields that define when a task should run and a command to execute. Here's the basic syntax:
* * * * * command_to_run
- - - - -
| | | | |
| | | | +----- Day of the week (0 - 6) (Sunday = 0)
| | | +------- Month (1 - 12)
| | +--------- Day of the month (1 - 31)
| +----------- Hour (0 - 23)
+------------- Minute (0 - 59)
Run a command every day at midnight:
0 * * * command_to_run
Run a command every Sunday at 3:30 PM:
30 15 * * 0 command_to_run
Run a command every 15 minutes:
*/15 * * * * command_to_run
Run a command every day at 5:45 AM, but only during May and June:
45 5 * 5,6 * command_to_run
This is how we can schedule the job, in my case I need to run the backup which will be incremental and run every 30 minutes.
See the example,
Read about User Management
root@ip-172-31-44-218:~# groupadd shivani_community root@ip-172-31-44-218:~# root@ip-172-31-44-218:~# root@ip-172-31-44-218:~# useradd -m -d /home/shivani -G shivani_community -g shivani_community -s /bin/bash missshivani
In this above example, I have tried using various options for user addition commands,
-m: This option is used to create the user's home directory if it does not already exist. In this case, it will create the home directory at /home/shivani for the user missshivani.
-d /home/shivani: This option specifies the home directory for the user. It sets the home directory to /home/shivani.
-G shivani_community: This option adds the user to the supplementary group shivani_community. This means that the user missshivani will be a member of the shivani_community group.
-g shivani_community: This option sets the initial login group to shivani_community. The initial login group is typically the same as the username, but in this case, it's set to shivani_community.
-s /bin/bash: This option specifies the user's login shell, which is set to /bin/bash. It's the default shell used for interactive command-line access.
missshivani: This is the username you're creating. The user will be named missshivani.
Primary and Secondary Group,
Primary Group:The primary group is the user's default group.
When a new user is created, a primary group with the same name as the username is usually created by default.
Secondary Groups:
Secondary groups are additional groups that a user can be a member of.
Users can be members of multiple secondary groups, which allows them to have access to resources and files belonging to those groups.
Note : see in cat /etc/group
Note : groups misshivani
by default, my user group is created and the other is the secondary group to which my user belongs to!!
Subscribe to my newsletter
Read articles from Shivani Gadekar directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
