Skip to content

Instantly share code, notes, and snippets.

@lioneltchami
Created December 2, 2024 21:33
Show Gist options
  • Save lioneltchami/09c3fa9abf998da77dabeb908132b15c to your computer and use it in GitHub Desktop.
Save lioneltchami/09c3fa9abf998da77dabeb908132b15c to your computer and use it in GitHub Desktop.

Hey DevCloud Ninjas! πŸ‘‹ Time for a fun Bash scripting challenge that'll help you level up your cloud skills! πŸš€

Your mission, should you choose to accept it, is to create a Bash script called vm_info.sh that gives us some basic info about our virtual machine. Here's what I want you to do:

  1. Create a script that does the following:

    • Prints a welcome message
    • Checks and displays the number of CPU cores
    • Shows the total amount of RAM
    • Displays the available disk space
    • Lists the top 3 processes using the most CPU
  2. Use if-else statements to:

    • Check if the user running the script is root
    • If they are, print a warning message
  3. Implement a simple while loop that:

    • Asks the user if they want to see current CPU usage
    • If yes, show the CPU usage every 5 seconds until the user presses Ctrl+C
    • If no, exit the script with a goodbye message

Here's a template to get you started:

#!/bin/bash

echo "Welcome to VM Info Script!"

# Your code here

echo "Script finished. Keep rocking, DevCloud Ninjas!"

Remember to make your script executable with chmod +x vm_info.sh before running it.

Bonus points if you can add error handling or make the output colorful! 🌈

Drop your scripts in the comments when you're done. Let's see what you've got, Ninjas! And remember, if you get stuck, our community is here to help. Happy scripting! πŸ’»β˜οΈ

@nolunchbreaks
Copy link

nolunchbreaks commented Dec 3, 2024

#!/bin/bash

echo "Welcome to the system information script!"

Display number of CPU cores

cpu_cores=$(nproc)
echo "Number of CPU cores: $cpu_cores"

Display total amount of RAM

total_ram=$(free -h | awk '/^Mem:/{print $2}')
echo "Total RAM: $total_ram"

Display available disk space

disk_space=$(df -h / | awk 'NR==2 {print $4}')
echo "Available disk space: $disk_space"

List top 3 processes using the most CPU

echo "Top 3 processes using the most CPU:"
ps -eo pid,comm,%cpu --sort=-%cpu | head -n 4

Check if the user running the script is root

if [ "$EUID" -eq 0 ]; then
echo "Warning: You are running this script as root!"
else
echo "You are not running this script as root."
fi

Function to get the current CPU usage

get_cpu_usage() {
top -bn1 | grep "Cpu(s)" | awk '{print "Current CPU usage: " 100 - $8 "%"}'
}

while true; do

Prompt the user

read -p "Do you want to see current CPU usage? (yes/no): " answer
case $answer in
[Yy]* ) # If the user says yes
echo "Showing CPU usage every 5 seconds. Press Ctrl+C to stop."
while true; do
get_cpu_usage # Display CPU usage
sleep 5 # Wait for 5 seconds
done
;;
[Nn]* ) # If the user says no
echo "Goodbye!"
exit # Exit the script
;;
* ) # For any other input
echo "Please answer yes or no."
;;
esac
done

@nolunchbreaks
Copy link

Yo im not gonna lie, i got stuck in question three and i had a little outside help lol iykyk. The point is at least i understand it now!!!!

@Joseph-Ibeh
Copy link

Joseph-Ibeh commented Dec 7, 2024

https://github.com/Joseph-Ibeh/vm_info_bashscript_assignment

script
#!/bin/bash

Function to add color to output

color_text() {
echo -e "\e[$1m$2\e[0m"
}

Welcome message

color_text "32" "Welcome to VM Info Script!"

Check if the user is root

if [ "$EUID" -eq 0 ]; then
color_text "31" "Warning: You are running this script as root!"
else
color_text "34" "You are not running this script as root. Proceeding..."
fi

Display number of CPU cores

cpu_cores=$(nproc)
color_text "33" "Number of CPU cores: $cpu_cores"

Display total amount of RAM

ram_total=$(free -h | awk '/^Mem:/ {print $2}')
color_text "33" "Total RAM: $ram_total"

Display available disk space

disk_space=$(df -h / | awk 'NR==2 {print $4}')
color_text "33" "Available disk space: $disk_space"

Display top 3 processes using the most CPU

color_text "33" "Top 3 processes by CPU usage:"
ps -eo pid,comm,%cpu --sort=-%cpu | head -n 4 | awk '{printf "%-10s %-20s %s\n", $1, $2, $3}'

Ask the user if they want to see current CPU usage

while true; do
echo -n "Do you want to see current CPU usage? (yes/no): "
read user_input

if [[ "$user_input" == "yes" ]]; then
    color_text "36" "Showing CPU usage every 5 seconds. Press Ctrl+C to exit."
    while true; do
        top -b -n 1 | grep "Cpu(s)" | awk '{print "CPU Usage: "$2"%"}'
        sleep 5
    done
elif [[ "$user_input" == "no" ]]; then
    color_text "32" "Goodbye! Thank you for using the VM Info Script."
    exit 0
else
    color_text "31" "Invalid input. Please enter 'yes' or 'no'."
fi

done

#output

vagrant@ubuntu-focal:~$ vim vm_info.sh

vagrant@ubuntu-focal:$ chmod +x vm_info.sh
vagrant@ubuntu-focal:
$ ./vm_info.sh
Welcome to VM Info Script!
You are not running this script as root. Proceeding...
Number of CPU cores: 2
Total RAM: 964Mi
Available disk space: 37G
Top 3 processes by CPU usage:
PID COMMAND %CPU
1 systemd 1.2
18 ksoftirqd/1 0.5
381 kworker/0:3-eve 0.4
Do you want to see current CPU usage? (yes/no): no
Goodbye! Thank you for using the VM Info Script.
vagrant@ubuntu-focal:~$

@davegermiquet
Copy link

davegermiquet commented Dec 7, 2024

#!/bin/bash
echo "Welcome to the bash shell"
echo "You have $(cat /proc/cpuinfo | grep processor | wc -l | tr '\n' ' ') Cores"
echo "You have a total of $(free -h | head -n 2 | tail -n 1 | sed 's/ */ /g'| cut -d' ' -f2)"
export sum="0"
for output in $(df | sed 's/ */ /g'| cut -d' ' -f4 | grep -v "Available" | tr '\n' ' ')
do
sum=$((output + sum))
done
sum=$((sum / 1024 / 1024))
echo "Total available disk space ${sum}GB"
echo "Top 3 processes running are:"
ps -eo pid,comm,%cpu --sort=-%cpu | head -n 4
currentuser=$(whoami | tr '\n' ' ' | sed 's/ //g')
if [ "$currentuser" == "root" ]; then
echo "warning: You are running as the root user"
else
echo "Running as $currentuser"
fi
read -p "Do you want to get current cpu usage press ctrl c to quit if yes please type yes: " answer
if [ "$answer" == "yes" ];then
while [ 1 ]
do
output=$(vmstat 1 1 | sed 's/ */ /g' | cut -d" " -f 16 | grep -v id | tr '\n' ' ' | sed 's/ *//g')
echo "Current cpu = $((100-output))%"
sleep 1
done
fi

@onlyfave
Copy link

#!/bin/bash

echo "Welcome to VM Info Script!"
sleep 2
echo "Number Of CPU Cores: $(nproc)"
nproc
echo " "
sleep 2

echo "Total Amount of RAM"
(free -h | awk '/^Mem:/ {print $2}')

echo " "
sleep 2

echo "Available Disk Space"
df -h
echo " "
sleep 2

echo "Top Three Processes Using The Most CPU"
top -n 1 | head -n 3
echo " "
sleep 3

echo "Check If user Running The Script Is Root"
if [ $(id -u) -eq 0 ]; then
echo "You are running the script as root."
else
echo "You are not running the script as root."
fi
echo " "

while true; do
read -p "Do you want to see current CPU usage? (yes/no): " yn
case $yn in
[Yy]* ) echo "Current CPU Usage: "; top -n 1 | grep "%Cpu(s):" ; break;;
[Nn]* ) echo "Exiting the script."; exit;;
* ) echo "Please answer yes or no.";;
esac
done

echo " "
echo -e "\e[1;45m Tried this, pink text highlight \e[0m"

echo "Script finished. Keep rocking, DevCloud Ninjas!"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment