Skip to main content

Command Palette

Search for a command to run...

Advance shell scripting

Published
โ€ข3 min readโ€ขView as Markdown
Advance shell scripting

Table of contents

*Write a bash script directories. sh that when the script is executed with three given arguments (one is the directory name and second is the start number of directories and the third is the end number of directories ) it creates a specified number of directories with a dynamic directory name.

*Create a Script to backup the files

*Cron and Crontab, to automate the backup Script

* User Management

let's dive into the first task without any due. In this, we write a simple script that would create a specified number of directories with a dynamic directory name.

#!/bin/bash

dir_name="$1"
start_day="$2"
end_day="$3"

eval "mkdir -p ${dir_name}{$start_day..$end_day}"

Now, let's see a real-time example of using shell scripting in the organization. We will write a script to back up the data.

#!/bin/bash

src="/path/to/the /directory"
tgt="path/to/the/directory"

filename="backup.tar.gz"

tar -czvf "$tgt/$filename" "$src"

Here "src" is a variable to store the path of the source and "tgt" is the variable to store the path of the intended backup. Tar command to compress and archive the backup file.

A cron tab is a way to schedule tasks on a computer system to run automatically at specific times or intervals. It's like setting up a "to-do" list for your computer to follow without needing your manual intervention each time.

For example, let's say you have a script that backs up your important files, and you want it to run every minute You would create a cron job like this:

#!/bin/bash

src="/path/to/the/source /directory"
tgt="path/to/the/target/directory"
filename="backup.tar.gz"
timestamp=$(date +"%Y%m%d%H%M%S")
backup_filename="$filename.$timestamp"

tar -czvf "$tgt/$backup_filename" "$src"
crontab -e * * * * * bash /path/to/backup/script.sh"

The cron tab has two main parts:

Timing Information: This part tells your computer when to perform the task. It's divided into five components:

Minute: A specific minute of the hour (0 to 59).

Hour: A specific hour of the day (0 to 23).

Day of the Month: A specific day of the month (1 to 31).

Month: A specific month (1 to 12) or you can use names like "January" or "May".

Day of the Week: A specific day of the week (0 or 7 for Sunday, 1 for Monday, and so on) or you can use names like "Sunday" or "Thursday".

User management on Linux involves tasks related to creating, modifying, and managing user accounts on a Linux system. This process is crucial for maintaining security, access control, and proper administration of the system. Here's a guide on various user management tasks:

Creating a New User:

To create a new user, you can use the useradd command followed by the username.

sudo useradd username

Viewing User Information: You can view user information using the id command or by looking at the /etc/passwd file. The id command shows details about the user's UID, GID, and groups they belong to.

Managing Password Policies: You can set password policies using the /etc/login.defs file or tools like pam_pwquality. These policies include minimum password length, complexity requirements, and password expiration.

Remember to use these commands with caution, as they can have significant security implications. Always follow best practices and ensure you're adhering to your organization's security policies.

Create 2 users and just display their Usernames

#!/bin/bash

# Create the first user
sudo useradd -m user1
echo "User 1 created with username: user1"

# Create the second user
sudo useradd -m user2
echo "User 2 created with username: user2"

As we wrap up this journey through the world of shell scripting, remember that each line of code you write is a step towards automating tasks, boosting productivity, and unleashing your creativity.

Keep coding, keep experimenting, and keep pushing the boundaries of what you can achieve with the command line. Happy scripting!"

More from this blog

Untitled Publication

12 posts