Linux Shell Scripting With User Management

Linux Shell Scripting With User Management

#Kernel

The kernel is a computer program that is the core of a computer’s operating system, with complete control over everything in the system.

Kernel is the heart of the operating system. It is an interaction between software applications and hardware. It manages the operations of the computer and the hardware, most notably memory and CPU time. It is an integral part of any operating system.

#Shell?

Shell is an environment in which we can run our commands, programs, and shell scripts. It is a user interface for access to an operating system’s services.

A shell is a special user program that provides an interface to users to use operating system services. Shell accepts human readable commands from a user and converts them into something which kernel can understand. It is a command language interpreter that executes commands read from input devices such as keyboards or from files. The shell gets started when the user logs in or start the terminal.

#1. What is Linux Shell Scripting?

A shell script is a computer program designed to be run by a linux shell, a command-line interpreter. The various dialects of shell scripts are considered to be scripting languages. Typical operations performed by shell scripts include file manipulation, program execution, and printing text.

#2. What is #!/bin/bash?

#!/bin/bash is called a shebang line which is used to instruct the operating system to use bash as a command interpreter. It consists of a number sign and an exclamation point character (#!), followed by the full path to the interpreter such as /bin/bash.

#3. Can we write #!/bin/sh as well? !/bin/sh?

#!/bin/bash (Bourne Again Shell) states that this script is running on a Bash Shell interpreter. It is commonly used on Linux and MacOS. #!/bin/sh (Bourne Shell) is a commonly used shell in Unix-based operating systems.

#4. Write a Shell Script which prints I will complete #90DaysOofDevOps challenge

#5. Write a Shell Script to take user input, input from arguments and print the variables.

#6. Write an Example of If else in Shell Scripting number is odd or even.

User Management

Let's create the shell scripts for the various user management tasks

  1. Create Directories Script:

    This script creates a specified number of directories with a dynamic name.

#!/bin/bash

if [ $# -ne 3 ]; then
    echo "Usage: $0 <directory_name> <start_number> <end_number>"
    exit 1
fi

directory_name="$1"
start_number="$2"
end_number="$3"

for i in $(seq "$start_number" "$end_number"); do
    mkdir -p "${directory_name}_${i}"
done
  1. Backup Script: This script takes a backup of your work.
#!/bin/bash

backup_dir="/path/to/backup/directory"
backup_filename="backup_$(date +'%Y%m%d_%H%M%S').tar"

tar cvf "$backup_dir/$backup_filename" /path/to/your/work
  1. User Creation Script: This script creates a user account.
#!/bin/bash

read -p "Enter username: " username
read -s -p "Enter password: " password
echo

if id "$username" &>/dev/null; then
    echo "User $username already exists."
else
    useradd -m -p "$password" "$username"
    echo "User $username created successfully."
fi
  1. User Deletion Script: This script deletes a user account.
#!/bin/bash

read -p "Enter username to delete: " username

if id "$username" &>/dev/null; then
    userdel -r "$username"
    echo "User $username deleted."
else
    echo "User $username does not exist."
fi
  1. Password Reset Script: This script resets a user's password.
#!/bin/bash

read -p "Enter username: " username
read -s -p "Enter new password: " new_password
echo

if id "$username" &>/dev/null; then
    echo -e "$new_password\n$new_password" | passwd "$username"
    echo "Password for user $username changed."
else
    echo "User $username does not exist."
fi
  1. List User Accounts Script: This script lists all user accounts and their UIDs.
bashCopy code#!/bin/bash

cut -d: -f1,3 /etc/passwd
  1. Help Option Script: This script provides usage information and command-line options.
bashCopy code#!/bin/bash

show_help() {
    echo "Usage: $0 [options]"
    echo "Options:"
    echo "  -c, --create    Create a new user"
    echo "  -d, --delete    Delete a user"
    echo "  -r, --reset     Reset a user's password"
    echo "  -l, --list      List all user accounts"
}

if [ "$#" -eq 0 ]; then
    show_help
    exit 1
fi

while [[ $# -gt 0 ]]; do
    case "$1" in
        -c|--create)
            # Call user creation function
            shift
            ;;
        -d|--delete)
            # Call user deletion function
            shift
            ;;
        -r|--reset)
            # Call password reset function
            shift
            ;;
        -l|--list)
            # Call list user accounts function
            shift
            ;;
        -h|--help)
            show_help
            exit 0
            ;;
        *)
            echo "Unknown option: $1"
            show_help
            exit 1
            ;;
    esac
done

These scripts cover a range of user management tasks in a Linux environment. You can execute them as needed for specific tasks.

Thank you for reading this blog. I 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.

— Happy Learning !!!

Let’s connect !!!

Linkedin

Github

Mail

Medium