DEV Community

Cover image for Shell Scripting Mini Project
Amash Ansari
Amash Ansari

Posted on

Shell Scripting Mini Project

About

This project gives detailed information about the server who is the user, date and time, Server Uptime, last login activities, Disk utilization, RAM utilization, and Top CPU Processes Running. This project is made using Shell-Scripting, the script can be executed using the bash. I've also taken a backup of this script in the form of a zip file. Some extra commands are also used just to beautify the project.

Prerequisite

  1. Linux
  2. Shell Scripting

Things to Remember

Before diving into the project, there are a few commands that are required to be discussed just to have a simple understanding of the project.

  • whoami - allows Linux users to see the currently logged-in user.
  • echo - is used to display the text passed in as an argument.
  • echo -e - allows escape sequences within its operands.
  • date - displays the current time and date (of the system) in the given FORMAT.
  • xargs - converts the statements into arguments.
  • awk - a utility that enables a programmer to write tiny but effective programs in the form of statements
  • top - a commonly used tool for displaying system-performance information.
  • last - used to display a list of users who have previously logged in to the system.
  • uptime - an important metric that shows how long the system has been running or how much time has passed since the system last rebooted.
  • df -h - displays information about total space and available space on a file system.
  • free -h - displays the total amount of free and used physical and swap memory in the system, as well as the buffers used by the kernel.

Script

  • Create a shell script.
 vim server_details.sh
Enter fullscreen mode Exit fullscreen mode
  • Add production house at the top, which means mention the shell you'll be using for the execution of the script.
 #!/bin/bash
Enter fullscreen mode Exit fullscreen mode
  • Start writing the script in the way you like by not conflicting with the syntax.
  • The final script looks something like this. πŸ‘‡
#!/bin/bash

RED="31"
GREEN="32"
YELLOW="33"
MAGENTA="35"
GRAY="90"
WHITE="97"
ITALICRED="\e[3;${RED}m"
ITALICYELLOW="\e[3;${YELLOW}m"
ITALICWHITE="\e[3;${WHITE}m"
ITALICGREEN="\e[3;${GREEN}m"
BOLDGRAY="\e[1;${GRAY}m"
BOLDYELLOW="\e[1;${YELLOW}m"
BOLDMAGENTA="\e[1;${MAGENTA}m"
ENDCOLOR="\e[0m"


echo -e "${BOLDMAGENTA}\n---------------------------WELCOME----------------------------------\n${ENDCOLOR}"
echo -e "${BOLDYELLOW}\nHey $(whoami), Welcome to the server details corner :-)\n${ENDCOLOR}"

echo -e "${BOLDGRAY}----------------------------***----------------------------------${ENDCOLOR}"
echo -e "${ITALICWHITE}        Current date and Time is : $(date | xargs | awk '{print $3"/"$2"/"$6,$1,$4,$5}')${ENDCOLOR}"
echo -e "${BOLDGRAY}----------------------------***----------------------------------\n${ENDCOLOR}"

echo -e "${BOLDYELLOW}******************************************************************${ENDCOLOR}"
echo -e "${BOLDGRAY}Server Uptime is   : ${ENDCOLOR} ${ITALICWHITE} $(uptime) ${ENDCOLOR}"
echo -e  "${BOLDGRAY}\nLast login details :${ENDCOLOR} ${ITALICWHITE} \n$(last -a | head -5) ${ENDCOLOR}"
echo -e "${BOLDYELLOW}******************************************************************${ENDCOLOR}"

echo -e "${ITALICRED}\n###################################################################${ENDCOLOR}"
echo -e "${ITALICYELLOW}    Disk Space available :${ENDCOLOR} ${ITALICWHITE} $(df -h | xargs | awk '{print $11"/"$9}')${ENDCOLOR}"
echo -e "${BOLDGRAY}\n------------------------------------------------------------------\n${ENDCOLOR}"

echo -e "${ITALICYELLOW}    RAM utilization      :${ENDCOLOR} ${ITALICWHITE} $(free -h | xargs | awk '{print $10"/"$8}')${ENDCOLOR}"
echo -e "${ITALICRED}##################################################################${ENDCOLOR}"

echo -e "${BOLDMAGENTA}\n----------------------------***----------------------------------\n${ENDCOLOR}"
echo -e "${ITALICYELLOW}Top CPU Processes running : ${ENDCOLOR}${ITALICWHITE}"  && top -b | head -10
echo -e "${BOLDMAGENTA}\n----------------------------***---------------------------------\n${ENDCOLOR}"

Enter fullscreen mode Exit fullscreen mode

Note: I've used some variables here just to beautify the project, they're optional to use. "${}" is used for mentioning variables in arguments. If you want to learn more about styling the text in bash script then Click here.

Output πŸŽ‰

Image description

Top comments (1)

Collapse
 
jimmymcbride profile image
Jimmy McBride

Very nice! Love the stylish display. :)