Introduction
In this project, you will learn how to retrieve and display system information on a Linux server. The getinfo.sh script you will create can be used to quickly gather important details about the server's hardware, software, and network configuration.
👀 Preview
$ sh getinfo.sh
## Example
cpu num: 8
memory total: 30 G
memory free: 10867 M
disk size: 20G
system bit: 64
process: 40
software num: 1389
ip: 1.32.X.X
🎯 Tasks
In this project, you will learn:
- How to retrieve the number of CPUs, total memory, available memory, disk size, system bit, number of running processes, number of installed software packages, and IP address of the server.
- How to use various Linux commands and tools, such as
grep,free,df,getconf,ps,dpkg-query, andip, to gather system information. - How to write a zsh script that combines these commands to provide a comprehensive overview of the server's state.
🏆 Achievements
After completing this project, you will be able to:
- Understand how to use shell scripting to automate the collection of system information.
- Gain familiarity with commonly used Linux commands and their applications.
- Create a reusable script that can be executed to quickly retrieve and display important server details.
- Apply your newfound knowledge to monitor and troubleshoot Linux systems more effectively.
Create the getinfo.sh Script
In this step, you will create the getinfo.sh script in the /home/labex/project directory.
Open a text editor and create a new file named
getinfo.shin the/home/labex/projectdirectory.Add the following shebang line at the beginning of the file:
#!/bin/zshThis line specifies that the script should be executed using the zsh shell.
Add the following comments to the script:
## getinfo.sh - Linux System Information Script ## This script retrieves CPU, memory, disk, and other information of a Linux server.These comments provide a brief description of the script's purpose.
Save the file.
Retrieve CPU Information
In this step, you will add a function to the getinfo.sh script to retrieve the number of CPUs.
Open the
getinfo.shscript in a text editor.Add the following function to the script:
## Function: Retrieve CPU information cpu_num=$(grep -c '^processor' /proc/cpuinfo)This function uses the
grepcommand to count the number of processor entries in the/proc/cpuinfofile, which represents the number of CPUs.Save the changes to the script.
Retrieve Memory Information
In this step, you will add functions to the getinfo.sh script to retrieve the total memory size and available memory size.
Open the
getinfo.shscript in a text editor.Add the following functions to the script:
## Function: Retrieve total memory size (in GB) memory_total=$(free -g | awk '/^Mem:/ {print $2}') ## Function: Retrieve available memory size (in MB) memory_free=$(free -m | awk '/^Mem:/ {print $4}')The first function uses the
freecommand with the-goption to retrieve the total memory size in gigabytes. The second function uses thefreecommand with the-moption to retrieve the available memory size in megabytes.Save the changes to the script.
Retrieve Disk Information
In this step, you will add a function to the getinfo.sh script to retrieve the total size of the file system mounted on the root directory.
Open the
getinfo.shscript in a text editor.Add the following function to the script:
## Function: Retrieve total disk size of the root filesystem (in GB) disk_size=$(df -h / | awk '/\// {print $2}')This function uses the
dfcommand with the-hoption to retrieve the total size of the file system mounted on the root directory (/), and then extracts the size value usingawk.Save the changes to the script.
Retrieve System Bit Information
In this step, you will add a function to the getinfo.sh script to retrieve the system bit information.
Open the
getinfo.shscript in a text editor.Add the following function to the script:
## Function: Retrieve system bit system_bit=$(getconf LONG_BIT)This function uses the
getconfcommand to retrieve the number of bits used by the system.Save the changes to the script.
Retrieve Process Information
In this step, you will add a function to the getinfo.sh script to retrieve the number of currently running processes.
Open the
getinfo.shscript in a text editor.Add the following function to the script:
## Function: Retrieve the number of currently running processes process=$(ps -ef | wc -l)This function uses the
pscommand to list all running processes and then counts the number of lines using thewccommand.Save the changes to the script.
Retrieve Software Information
In this step, you will add a function to the getinfo.sh script to retrieve the number of installed software packages.
Open the
getinfo.shscript in a text editor.Add the following function to the script:
## Function: Retrieve the number of installed software packages software_num=$(dpkg-query -f '${binary:Package}\n' -W | wc -l)This function uses the
dpkg-querycommand to list all installed software packages and then counts the number of lines using thewccommand.Save the changes to the script.
Retrieve IP Address Information
In this step, you will add a function to the getinfo.sh script to retrieve the IP address of the eth0 network interface.
Open the
getinfo.shscript in a text editor.Add the following function to the script:
## Function: Retrieve the IP address of eth0 ip=$(ip addr show eth0 | awk '/inet / {print $2}' | sed 's|/.*||')This function uses the
ipcommand to retrieve the IP address of the eth0 network interface and then extracts the IP address usingawkandsed.Save the changes to the script.
Output the System Information
In this final step, you will add the output section to the getinfo.sh script to display the system information.
Open the
getinfo.shscript in a text editor.Add the following output section to the script:
## Output information echo "cpu num: $cpu_num" echo "memory total: $memory_total G" echo "memory free: $memory_free M" echo "disk size: $disk_size" echo "system bit: $system_bit" echo "process: $((process - 1))" echo "software num: $software_num" echo "ip: $ip"This section uses the
echocommand to print the system information in the required format.Save the changes to the script.
Congratulations! You have now completed the getinfo.sh script. You can execute the script by running the following command in the terminal:
sh getinfo.sh
The script will output the system information as specified in the project requirements.
## Example
cpu num: 8
memory total: 30 G
memory free: 10867 M
disk size: 20G
system bit: 64
process: 40
software num: 1389
ip: 1.32.X.X
Summary
Congratulations! You have completed this project. You can practice more labs in LabEx to improve your skills.



