https://github.com/nikhilkumarmishra/linux-custom-commands
Linux Custom commands using the bash script
https://github.com/nikhilkumarmishra/linux-custom-commands
Last synced: 4 months ago
JSON representation
Linux Custom commands using the bash script
- Host: GitHub
- URL: https://github.com/nikhilkumarmishra/linux-custom-commands
- Owner: Nikhilkumarmishra
- Created: 2024-01-10T10:17:42.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2024-01-10T11:55:07.000Z (over 1 year ago)
- Last Synced: 2025-01-13T01:46:50.823Z (6 months ago)
- Size: 10.7 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
Attached all the screenshots needed
# SECTION A
#!/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

# 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
# Create a script file, let's call it internsctl, and make it executable:
touch internsctl
chmod +x internsctl# Add the following
#!/bin/bashcase "$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

# 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
fiif [ "$#" -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] "
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
# 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.




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