In this project, we will be creating a shell script that welcomes the logged in user and will display a certain set of information.
The below table contains the list of displayed information and the necessary Linux command for the same:
Usecase | Command |
display any information on screen | echo |
get current user | whoami |
get date | date |
information about how long the system has been running | uptime |
information about user logins and system reboots | last |
information about the system's memory usage in a human-readable format | free -h |
information about disk space usage in a human-readable format | df -h |
top CPU processes running | top |
Create a file with the extension .sh with the below contents
ubuntu@ip-10-0-0-219:~$ vim shell_script.sh
Write the shell script to display the required information
#!/bin/bash echo -e "...................................HELLO..................................................." echo -e "Hi $(whoami)!Welcome to my DevOps journey!" echo -e "..........................................................................................." #show the date and time echo -e "Current date and time is: $(date)" echo -e "____________________________________________________________________________________________" #show uptime and downtime echo -e "Uptime of the server is $(uptime) \n Last login details: \n$(last -a | head -3)" echo -e "______________________________________________________________________________________________" #show disc space and ram utilization echo -e "Disc space is $(df -h | xargs | awk '{ print $11 "/" $9 }')" echo -e "________________________________________________________________________________________________" #show RAM utilization echo -e "RAM utilization is $(free | xargs | awk '{print $8 "/" $10}')" echo -e "_________________________________________________________________________________________________" #show top CPU processes echo -e "\nTop CPU processes\n$(top -b -n 1 | tail -n +8 | head -n 10)"
Make the file executable with the chmod command
ubuntu@ip-10-0-0-219:~$ chmod 777 shell_script.sh
Run the shell script
ubuntu@ip-10-0-0-219:~$ ./shell_script.sh
The ouput for the above script will be as follows:
Thanks for reading my blog.