Advanced Linux Shell Scripting for DevOps Engineers with User management.

Advanced Linux Shell Scripting for DevOps Engineers with User management.

📌Introduction:

In the last blog, we saw the basic of shell scripting now it's time to learn more and get our hands dirty in advanced topics.

In this blog, we are learning about Advanced scripting, how to use wildcards, how to take backups, what are cron and crontab, how to schedule jobs using crontab and many more.

📌How to create Multiple directories, using wild-card:

user@hostip:~$ mkdir Blog{1..0}

📌How to create Multiple directories, from passing arguments dynamically:

let's say you have to create 10 directories for storing your everyday data, also you do not want to write the same code multiple times to create a directory rather you want to pass the dir_name and how much dir you want,

user@hostip:~$ vim createDirectories.sh
## add below code in your script
#!/bin/bash
if [ $# -ne 3 ]; then
  echo "Usage: $0 <dir_name> <start_num> <end_num>"
  exit 1
fi
dir_name=$1
start_num=$2
end_num=$3
for ((i=start_num; i<=end_num; i++)); do
  sub_dir="${dir_name}${i}"
  mkdir "${sub_dir}"
  echo "Created directory: ${sub_dir}"
done
echo "All directories created successfully."

Explanation: Above code snippet contain a basic shell script which takes three arguments from the user like directory_name, start-num and end_num. Then create that many directories. let's go ahead and try to execute it.

🛑Note: Don't forget to pass the arg while executing the command otherwise it gives an error and also gives executable permission to your script.

user@hostip:~$  chmod +x createDirectories.sh
user@hostip:~$ ./createDirectories.sh Blog 1 10

✅Output:

📌What is cron and crontab:

Cron is a time-based job scheduler in Unix-like operating systems that allows you to schedule and automate the execution of tasks or scripts at specified intervals. Crontab is the configuration file used by Cron to define the schedule and commands to be executed.

📌Creating a simple backup script and printing its output in a backup.txt file every two minutes using cron job:

#!/bin/bash
# Define backup file
backup_file="/home/ubuntu/backup.txt"
# Get current timestamp
timestamp=$(date +"%Y-%m-%d %H:%M:%S")
# Execute the command and append output with timestamp to the backup file
echo "[$timestamp] $(echo "Learned crontab")" >> "$backup_file"

Make one script and add the above code in it, then by using the cron tab schedule the printing job,

user@hostip:~$ crontab -e
# m h  dom mon dow   command
 */2 * * * * bash /home/ubuntu/timestamp.sh

Then check whether your output of the script is stored in the backup file, the result looks like this,

📌About User Management in Linux:

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.

The id command displays the user and group information for the current user or a specified user. Simply run the id without any arguments to display the information for the current user. To check the details for a specific user, provide the username as an,

  • To check users' ids,
id                  # Display information for current user
id username         # Display information for a specific user

The /etc/passwd file stores the user account information, including the user ID (UID) and the default group ID (GID) associated with each user. Each line in the file represents a user account, and the fields are separated by colons (:). The third field represents the UID, and the fourth field represents the GID. You can use a text editor or the cat command to view the file:

cat /etc/passwd

The /etc/group file contains the group account information, including the group ID (GID) and the members of each group. Each line in the file represents a group, and the fields are separated by colons (:). The third field represents the GID. You can view the file using a text editor or the cat command:

user@hostip:~$ sudo groupadd <grp_name>
cat /etc/group

📌Create the users and check the details :

  • To create new users, without creating a user directory

      user@hostip:~$ sudo useradd <usr_name>
    
  • To create a user with the directory in /home,

user@hostip:~$ sudo useradd -m <usr_name>
  • ✅output:

🔁Conclusion:

Above mentioned commands are intermediate level, I encouraged you to try this on your own, you cant learn just by reading the block, you must have tried this then only you gain expertise. If you faced any doubt reach out to me via the comment section, I am happy to help you.

Thanks for reading..! Happy learning..!