Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Nikhilkumarmishra/Linux-Custom-commands

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 

Repository files navigation

Attached all the screenshots needed

SECTION A

Screenshot (370)

#!/bin/bash

here i am Defining the version

VERSION="v0.1.0"

here i am definig the Function to display the manual page

display_manual() { echo "internsctl(1) - Custom Linux Command" echo echo "NAME" echo " internsctl - Perform operations as per user's requirements" echo echo "SYNOPSIS" echo " internsctl [OPTIONS] [ARGUMENTS]" echo echo "DESCRIPTION" echo " internsctl is a custom Linux command that performs operations as specified by the user." echo echo "OPTIONS" echo " --help Display this help message" echo " --version Display the version of internsctl" echo echo "EXAMPLES" echo " internsctl --help" echo " internsctl --version" echo }

Function to display help message

display_help() { echo "Usage: internsctl [OPTIONS] [ARGUMENTS]" echo "Perform operations as per user's requirements." echo echo "OPTIONS:" echo " --help Display this help message" echo " --version Display the version of internsctl" echo echo "EXAMPLES:" echo " internsctl --help" echo " internsctl --version" echo }

Function to display version

display_version() { echo "internsctl $VERSION" }

Main script logic

case "$1" in --help) display_help ;; --version) display_version ;; *) echo "Unknown option. Use 'internsctl --help' for usage information." exit 1 ;; esac

SECTION B

Screenshot (371)

Create a script file, let's call it internsctl, and make it executable:

touch internsctl chmod +x internsctl

#!/bin/bash

case "$1" in "cpu") case "$2" in "getinfo") lscpu ;; *) echo "Invalid subcommand for 'cpu'. Usage: internsctl cpu getinfo" ;; esac ;; "memory") case "$2" in "getinfo") free -h ;; *) echo "Invalid subcommand for 'memory'. Usage: internsctl memory getinfo" ;; esac ;; *) echo "Invalid command. Usage: internsctl {cpu|memory} {getinfo}" ;; esac

Save the file and run your commands:

./internsctl cpu getinfo ./internsctl memory getinfo

These commands will provide output similar to lscpu and free -h, respectively

part 2 | Intermediate

Screenshot (372)

Create a script file, let's call it internsctl, and make it executable:

touch internsctl chmod +x internsctl

Add the following

#!/bin/bash

case "$1" in "user") case "$2" in "create") if [ -z "$3" ]; then echo "Error: Please provide a username." else sudo useradd -m "$3" echo "User $3 created successfully." fi ;; "list") if [ "$3" == "--sudo-only" ]; then grep -E 'sudo|admin' /etc/group | cut -d: -f4 | tr ',' '\n' else getent passwd | grep -E '/bin/(bash|sh)$' | cut -d: -f1 fi ;; *) echo "Invalid subcommand for 'user'. Usage: internsctl user {create | list [--sudo-only]}" ;; esac ;; *) echo "Invalid command. Usage: internsctl {user}" ;; esac

This script uses a case statement to handle different commands and subcommands for user management. It checks whether the first argument is "user" and then processes the provided subcommands accordingly.

Save and run your command

./internsctl user create ./internsctl user list ./internsctl user list --sudo-only

These commands should create a user, list all regular users, and list users with sudo permissions, respectively.

Part 3 | Advance Level

Screenshot (373)

You can extend the internsctl script to handle file information with options

#!/bin/bash

print_file_info() { local file=$1 local size=$(stat -c %s "$file") local permissions=$(stat -c %A "$file") local owner=$(stat -c %U "$file") local last_modified=$(stat -c %y "$file")

echo "File: $file"
echo "Access: $permissions"
echo "Size(B): $size"
echo "Owner: $owner"
echo "Modify: $last_modified"

}

while [[ "$#" -gt 0 ]]; do case "$1" in "file") shift case "$1" in "getinfo") shift file_name="$1" shift if [ ! -e "$file_name" ]; then echo "Error: File '$file_name' does not exist." exit 1 fi

                if [ "$#" -eq 0 ]; then
                    print_file_info "$file_name"
                else
                    while [[ "$#" -gt 0 ]]; do
                        case "$1" in
                            "--size" | "-s")
                                stat -c %s "$file_name"
                                ;;
                            "--permissions" | "-p")
                                stat -c %A "$file_name"
                                ;;
                            "--owner" | "-o")
                                stat -c %U "$file_name"
                                ;;
                            "--last-modified" | "-m")
                                stat -c %y "$file_name"
                                ;;
                            *)
                                echo "Invalid option: $1"
                                exit 1
                                ;;
                        esac
                        shift
                    done
                fi
                ;;
            *)
                echo "Invalid subcommand for 'file'. Usage: internsctl file getinfo [options] <file-name>"
                exit 1
                ;;
        esac
        ;;
    *)
        echo "Invalid command. Usage: internsctl {file}"
        exit 1
        ;;
esac
shift

done

Now we can use the command as described:

For general file information

./internsctl file getinfo hello.txt

For specific information using options

./internsctl file getinfo --size hello.txt ./internsctl file getinfo --permissions hello.txt ./internsctl file getinfo --owner hello.txt ./internsctl file getinfo --last-modified hello.txt

Screenshot (375)

In this file i have tried to add all the codes necessary for executing all the 3 task . and rest of details is attached in the Readme file . Due to time contraint other wise i can elaborate it more with more detials.

Screenshot (374) Screenshot (373) Screenshot (372) Screenshot (371) Screenshot (370)

Diagram , seprate folder for diagram is also there in the repository image

About

Linux Custom commands using the bash script

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published