A short note about shell scripting :
Shell scripting refers to the creation and execution of scripts that automate tasks or perform various operations using the command line interface of an operating system. In other words, shell scripts are programs written in a scripting language that interacts with the operating system and executes various commands and programs.
Shell scripts can be used to perform a wide range of tasks, including file management, system administration, software deployment, data processing, and more. They are commonly used in Unix-like operating systems such as Linux, macOS, and FreeBSD.
Advanced shell scripting involves the use of advanced features and techniques to create more complex and sophisticated scripts. This may include using conditional statements, loops, functions, variables, arrays, and other programming constructs to create scripts that are more flexible and powerful.
Advanced shell scripting may also involve the use of specialized tools and utilities such as sed, awk, grep, and other command-line tools to manipulate and process data more efficiently. Additionally, advanced shell scripting may involve integrating with other programming languages such as Python or Perl to create more complex applications and automate complex tasks.
* Write a bash script create 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 third is the end number of directories ) it creates a specified number of directories with a dynamic directory*
Here's an example of a bash script that creates a specified number of directories with a dynamic directory name based on the input arguments:
#!/bin/bash if [ $# -ne 3 ]; then echo "Usage: $0 <directory name> <start number> <end number>" exit 1 fi dir_name=$1 start_num=$2 end_num=$3 if [ $start_num -gt $end_num ]; then echo "Error: start number cannot be greater than end number" exit 1 fi for i in $(seq $start_num $end_num); do if [ $i -lt 10 ]; then dir_num="0$i" else dir_num="$i" fi dir="$dir_name$dir_num" mkdir $dir done echo "Directories created successfully."
Here's how the script works:
First, the script checks if it has been given exactly three input arguments. If not, it prints an error message and exits with a status code of 1.
Next, the script assigns the input arguments to variables for easier handling.
The script then checks if the start number is greater than the end number. If so, it prints an error message and exits with a status code of 1.
Finally, the script uses a for loop and the
seq
command to create a sequence of numbers from the start number to the end number. For each number in the sequence, the script checks if it is less than 10 and, if so, prepends a zero to it to ensure the directory name is of the same length. It then concatenates the directory name with the number to create the final directory name and uses themkdir
command to create the directory.Once all directories have been created, the script prints a success message.
To use this script, save it to a file named createDirectories.sh
, make it executable with the command chmod +x
createDirectories.sh
, and then run it with three arguments as follows:
./createDirectories.sh day 1 90
This will create ten directories named day01
, day02
, ..., day90
in the current directory.
Create a Script to back up all your work done till now
- Backups are an important part of DevOps Engineers' day to Day activities The video in References will help you to understand How a DevOps Engineer takes backups.
here's an example of a bash script that can backup all your work done till now:
#!/bin/bash
backup_dir="/path/to/backup/directory"
timestamp=$(date +"%Y%m%d_%H%M%S")
backup_file="backup_$timestamp.tar.gz"
echo "Creating backup of current directory..."
tar -czvf $backup_dir/$backup_file *
echo "Backup complete."
Here's how the script works:
First, the script assigns a variable
backup_dir
with the path to the backup directory where you want to store the backup file.The script then uses the
date
command to create a timestamp that will be used as part of the backup file name.The script concatenates the timestamp with a string
backup_
to create the backup file name.Next, the script creates a tar archive of the current directory using the
tar
command with the optionsczvf
. Thec
option creates a new archive,z
option compresses the archive with gzip,v
option shows the progress of the archive creation, andf
option specifies the name of the output file. Finally, the*
wildcard character is used to include all files and subdirectories in the current directory in the backup.The resulting backup file is stored in the backup directory specified in the
backup_dir
variable.Once the backup is complete, the script prints a message to the console.
To use this script, save it to a file named backup.sh
and make it executable with the command chmod +x
backup.sh
. Then, navigate to the directory you want to backup and run the script as follows:
./backup.sh
This will create a compressed backup file with a unique timestamp in the specified backup directory.
About Cron and Crontab, to automate the backup Script
Cron is a utility in Linux and Unix systems that allows users to schedule commands or scripts to run automatically at specific times or intervals. The scheduling is done using the "crontab" (cron table) file, which contains a list of commands to be executed and their scheduled times.
To automate the backup script, you can create a crontab entry that specifies when the backup should run. Here's an example:
- Open the crontab editor by running the following command in the terminal:
crontab -e
- This will open the crontab file in the default editor. In this file, add the following line to schedule the backup script to run daily at 2am:
0 2 * * * /path/to/backup_script.sh
This line specifies the schedule using the following format:
minute hour day_of_month month day_of_week command
In this example, the "minute" field is set to 0 (meaning the script will run at the start of the hour), the "hour" field is set to 2 (meaning the script will run at 2am), and the other fields are set to "*" (meaning the script will run every day of every month and every day of the week). The "command" field specifies the path to the backup script.
- Save the crontab file and exit the editor.
Now the backup script will run automatically at the specified time every day. You can adjust the schedule as needed by editing the crontab file again. Note that the "cron" daemon must be running for crontab entries to be executed, so make sure this is the case on your system.
Overview of user management
A user is an entity, in a Linux operating system, that can manipulate files and perform several other operations. Each user is assigned an ID that is unique for each user in the operating system. In this post, we will learn about users and commands which are used to get information about the users. After installation of the operating system, the ID 0 is assigned to the root user and the IDs 1 to 999 (both inclusive) are assigned to the system users hence the ids for local user begins from 1000 onwards.
User management in Linux involves creating, modifying, and deleting user accounts, as well as managing user permissions and groups.
Here are some commonly used commands for user management in Linux:
useradd
: This command is used to create a new user account. The syntax isuseradd [options] username
. Some commonly used options include-m
to create a home directory for the user,-s
to specify the default shell, and-G
to add the user to additional groups.userdel
: This command is used to delete a user account. The syntax isuserdel [options] username
. By default, this command does not remove the user's home directory, but this can be changed with the-r
option.passwd
: This command is used to change a user's password. The syntax ispasswd [options] username
. If run without any options, the command will prompt the user to enter a new password.su
: This command is used to switch to another user account. The syntax issu [options] username
. By default, runningsu
without any options will switch to the root user account.sudo
: This command is used to execute a command with elevated privileges. The syntax issudo command
. By default, this command will prompt the user for their password before executing the command with elevated privileges.chown
: This command is used to change the owner of a file or directory. The syntax ischown [options] user:group file
. By default, this command changes only the owner, but this can be changed with the-R
option to change ownership recursively.chmod
: This command is used to change the permissions of a file or directory. The syntax ischmod [options] mode file
. The mode can be specified using either numeric or symbolic notation.groupadd
: This command is used to create a new group. The syntax isgroupadd [options] groupname
. Some commonly used options include-r
to create a system group and-g
to specify the group ID.groupdel
: This command is used to delete a group. The syntax isgroupdel groupname
.
These are just a few of the most commonly used commands for user management in Linux. Depending on your needs, there may be other commands or options that are more appropriate in use case.
Create 2 users and just display their Usernames
To create two users and display their usernames, you can use the following commands:
sudo useradd user1 sudo useradd user2 echo "User 1: $(id -un user1)" echo "User 2: $(id -un user2)"
Here's what each command does:
sudo useradd user1
: This command creates a new user account named "user1".sudo useradd user2
: This command creates a new user account named "user2".id -un user1
: This command displays the username for the user account "user1". The-u
option specifies that the user ID should be displayed instead of the username, and the-n
option specifies that the name should be displayed instead of the user ID. Similarly,id -un user2
displays the username for the user account "user2".echo "User 1: $(id -un user1)"
: This command prints a message to the console that displays the username for "user1". The$()
syntax is used to execute theid -un user1
command and insert its output into the message.echo "User 2: $(id -un user2)"
: This command prints a message to the console that displays the username for "user2" in the same way as the previous command.