Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Hello everyone.
First I'd like to say I'm not a programmer or something like that. I made a simple Bash script to get system information. It's working well with my computer [Manjaro] and few others. [Ubuntu and Fedora] So I wanna know is this working with other distributions as well? Anyone can help me? Suggestion are always welcome. Here's my script.
Thanks in advance.
Code:
#!/bin/bash
#
# ============
# Information
# ============
#
# Author - Enindu Alahapperuma (Jolly Roger)
# Email - enindu@gmail.com / enindu@yahoo.com
#
# Information is a simple bash script to get system information in Linux
# machines. Open "Information.sh" as root. Tested on Manjaro Linux,
# Ubuntu and Fedora only. See below for know about dependencies for each
# distribution.
#
# Arch and Manjaro - mesa-demos, curl, xorg-xdpyinfo, dmidecode
# Debian and Ubuntu - mesa-utils, curl, x11-utils, dmidecode
# Red Hat and Fedora - mesa-demos, curl, xorg-x11-utils, dmidecode
#
# Last updated on - 12:20 PM of 25/04/2014
bold=$(tput bold)
regular=$(tput sgr0)
if [ "$EUID" != 0 ]; then
echo -en "\e[1;31m${bold}You must run this script as root.${regular}\e[1;31m"; tput sgr0
echo; exit 1
fi
xfce=$(ps -e | grep -E '^.* xfce4-session$' > xfce)
gnome=$(ps -e | grep -E '^.* gnome-session$' > gnome)
kde=$(ps -e | grep -E '^.* kded4$' > kde)
clear && sleep 1
echo -en "\e[0;32m${bold}System information${regular}\e[0;32m"; tput sgr0; echo
echo "Hostname :" $(uname -n)
echo "Operating system :" $(lsb_release -sdrc)
echo "Kernel name and version :" $(uname -s) $(uname -r) $(uname -m)
echo "Total RAM :" $(cat /proc/meminfo | grep -i MemTotal | sed 's/MemTotal://g'); echo
echo -en "\e[0;32m${bold}Machine information${regular}\e[0;32m"; tput sgr0; echo
echo "Board manufacturer :" $(dmidecode -t 2 | grep -i Manufacturer | sed 's/Manufacturer://g')
echo "Model name :" $(dmidecode -t 2 | grep -i Product\ Name | sed 's/Product Name://g')
echo "BIOS vendor :" $(dmidecode -t 0 | grep -i Vendor | sed 's/Vendor://g')
echo "BIOS version :" $(dmidecode -t 0 | grep -i Version | sed 's/Version://g')
echo "Released date :" $(dmidecode -t 0 | grep -i Release\ Date | sed 's/Release Date://g'); echo
echo -en "\e[0;32m${bold}Processor information${regular}\e[0;32m"; tput sgr0; echo
echo "Processor type :" $(cat /proc/cpuinfo | grep -i model\ name | sed 's/model name ://g' | sed 's/CPU//g' | sed 's/GHz/ GHz/g' | sed '$!N; /^\(.*\)\n\1$/!P; D')
echo "Cache size :" $(cat /proc/cpuinfo | grep -i cache\ size | sed 's/cache size ://g' | sed '$!N; /^\(.*\)\n\1$/!P; D'); echo
echo -en "\e[0;32m${bold}Graphics information${regular}\e[0;32m"; tput sgr0; echo
echo "Graphic card(s) :" $(lspci | grep -i VGA\ compatible\ controller | sed 's/^.......//' | sed 's/VGA compatible controller://g')
echo "Graphic vendor :" $(glxinfo | grep -i OpenGL\ vendor\ string | sed 's/OpenGL vendor string://g')
echo "Direct rendering :" $(glxinfo | grep -i direct\ rendering | sed 's/direct rendering://g')
echo "Screen resolution :" $(xdpyinfo | grep -i dimensions | sed 's/dimensions://g' | sed 's/pixels//g' | awk '{print $1}')
echo "GLX renderer :" $(glxinfo | grep -i OpenGL\ renderer\ string | sed 's/OpenGL renderer string://g')
echo "GLX version :" $(glxinfo | grep -i OpenGL\ version\ string | sed 's/OpenGL version string://g'); echo
echo -en "\e[0;32m${bold}Audio and network information${regular}\e[0;32m"; tput sgr0; echo
echo "Audio card(s) :" $(lspci | grep -i Multimedia\ audio\ controller | sed 's/^.......//' | sed 's/Multimedia audio controller://g') $(lspci | grep -i Audio\ device | sed 's/^.......//' | sed 's/Audio device://g')
echo "Network card(s) :" $(lspci | grep -i Ethernet\ controller | sed 's/^.......//' | sed 's/Ethernet controller://g')
echo "Network interface(s) :" $(ip link show | tail -n+3 | sed 's/^...//' | grep -i state | sed "s/:.*//"); echo
echo -en "\e[0;32m${bold}Hard drive information${regular}\e[0;32m"; tput sgr0; echo
echo "Root disk free space :" $(df -h / | tail -n+2 | awk '{ for (i = 1; i <= NF; i += 3) { printf " %s", $i }; printf "\n" }' | sed 's/^...........//g' | sed 's/G/ GB/g')
echo "Root disk mounted at :" $(df -h / | tail -n+2 | cut -c 1-10)
echo "SWAP partition mounted at :" $(swapon -s | tail -n+2 | cut -c 1-10); echo
echo -en "\e[0;32m${bold}Location information${regular}\e[0;32m"; tput sgr0; echo
echo "External IP address :" $(curl -s ifconfig.co)
echo "Connection type :" $(curl -s ifconfig.co/connection)
echo "Country :" $(curl -s www.iptolatlng.com?ip=$(curl -s ifconfig.co) | grep -i countryFullName | cut -c 25-75 | sed 's/",//g'); echo
echo -en "\e[0;32m${bold}Other information${regular}\e[0;32m"; tput sgr0; echo
echo "Uptime :" $(uptime | awk '{print $3}' | sed 's/,//g') "(HH:MM)"
if [ -s xfce ]; then
echo "Desktop environment : Xfce" $(xfce4-session --version | grep xfce4-session | awk '{print $2}')
fi
if [ -s gnome ]; then
echo "Desktop environment : GNOME" $(gnome-session --version | awk '{print $2}')
fi
if [ -s kde ]; then
echo "Desktop environment : KDE" $(kded4 --version | grep -m 1 'KDE' | awk -F ':' '{print $2}' | awk '{print $1}')
fi
echo "Shell client :" $(echo $SHELL)
rm xfce gnome kde
echo; exit 0
1. Does not cater for those of us not using one of the DE's you have listed or maybe just a WM
2. See here for comments on not needing to use cat on any of the lines where you have, ie grep or sed can both read a file
3. You constantly call grep with -i to remove any case sensitivity, yet when calling the sed to remove the same item you make no such requirement. I would suggest that all references with grep to -i are
not needed
4. The use of escapes as opposed to simple quoting, ie Product\ Name or just 'Product Name'
5. As grep is not generally doing any heavy lifting, I would simply use sed to search for your item, eg
Code:
# current
cat /proc/meminfo | grep -i MemTotal | sed 's/MemTotal://g'
# alternative
sed -n 's/MemTotal: *//gp' /proc/meminfo
6. tail, awk, sed combo not required
Code:
# current
df -h / | tail -n+2 | awk '{ for (i = 1; i <= NF; i += 3) { printf " %s", $i }; printf "\n" }' | sed 's/^...........//g' | sed 's/G/ GB/g'
# alternative
df -h / | awk 'NR>1{print gensub(/(.)$/," \\1B",1,$4)}'
7. I am not sure I see the point behind creating temp files, which by the way you are assuming do not already exist and will not be clobbered by your script.
You also the capture the output of your command but never use it?? Maybe you could just test the variables you set if you return what you are looking for?
8. Instead of requiring root, which from a little testing I think (could be wrong) is only needed for the calls to dmidecode, maybe you could put in an if that if not root you get all but
dmidecode associated info otherwise you get the lot ... just a thought
9. I notice you have setup 'bold' and 'regular', but then you call the raw form for colours ... why not set that up in a variable to? This way someone else can change to preferred colour in one
spot instead of several times throughout your script
A few things that come to mind. lsb_release is not installed by default, at least on the Redhat based systems that I use. The same is true for dmidecode. The script will error out on these issues.
do not use cat filename | grep whatever, grep whatever filename will work too.
do not use sed pattern1 | sed pattern2 | sed patternX, it can be solved with one single sed
for example these lines (8 new processes):
Here is a quick change up of the same (assuming all applications are installed, as pointed out by Glennzo) and I am unable to change the DE section as I do not currently run
one, ie I use openbox, and don't have a VM with any on:
Code:
#!/bin/bash
bold=$(tput bold)
regular=$(tput sgr0)
red=$(echo -en '\e[1;31m')
green=$(echo -en '\e[0;32m')
not_root()
{
echo -e "${red}\t<=As you are not root the below information is not available=>${regular}"
}
(( EUID == 0 )) && is_root=true || is_root=false
clear && sleep 1
cat <<-EOF
${green}${bold}System information${regular}
Hostname : $(uname -n)
Operating system : $(lsb_release -sdrc)
Kernel name and version : $(uname -srm)
Total RAM : $(sed -n 's/MemTotal: *//gp' /proc/meminfo)
${green}${bold}Machine information${regular}$($is_root || not_root)
Board manufacturer : $($is_root && dmidecode -t 2 | sed -n '/Manufacturer/s/^.*: //p')
Model name : $($is_root && dmidecode -t 2 | sed -n '/Name/s/^.*: //p')
BIOS vendor : $($is_root && dmidecode -t 0 | sed -n '/Vendor/s/^.*: //p')
BIOS version : $($is_root && dmidecode -t 0 | sed -n '/Version/s/^.*: //p')
Released date : $($is_root && dmidecode -t 0 | sed -n '/Date/s/^.*: //p')
${green}${bold}Processor information${regular}
Processor type : $(sed -rn '/model name/{s/^.*: |CPU//g;s/G/ &/p;q}' /proc/cpuinfo)
Cache size : $(sed -n '/cache size/{s/^.*: //p;q}' /proc/cpuinfo)
${green}${bold}Graphics information${regular}
Graphic card(s) : $(lspci | sed -n '/VGA/{s/^.*: //p;q}')
Graphic vendor : $(glxinfo | sed -n '/GL vendor/s/^.*: //p')
Direct rendering : $(glxinfo | sed -n '/direct /s/^.*: //p')
GLX renderer : $(glxinfo | sed -n '/renderer/s/^.*: //p')
GLX version : $(glxinfo | sed -n '/GL version/s/^.*: //p')
Screen resolution : $(xdpyinfo | awk '/dimensions/{print $2}')
${green}${bold}Audio and network information${regular}
Audio card(s) : $(lspci | sed -n '/audio/{s/^.*: //p;q}') $(lspci | sed -n '/Audio/{s/^.*: //p;q}')
Network card(s) : $(lspci | sed -n '/Ether/{s/^.*: //p}')
Network interface(s) : $(ip link | awk -F" *: *" 'NR>2 && /UP/{print $2}')
${green}${bold}Hard drive information${regular}
Root disk free space : $(df -h / | awk 'NR>1{print gensub(/(.)$/," \\1B",1,$4)}')
Root disk mounted at : $(df -h / | awk 'NR>1{print $1}')
SWAP partition mounted at : $(swapon -s | awk 'NR>1{print $1}')
${green}${bold}Location information${regular}
External IP address : $(curl -s ifconfig.co)
Connection type : $(curl -s ifconfig.co/connection)
Country : $(curl -s www.iptolatlng.com?ip=$(curl -s ifconfig.co) | awk -F"\"" '/countryFullName/{print $4}')
${green}${bold}Other information${regular}
Uptime : $(uptime -p | cut -d' ' -f2-)
Shell client : $SHELL
EOF
exit 0
Obviously there are always many ways to do things, but hopefully this will give you some ideas
I wasn't sure if you wanted to keep the '950 @' part or not. It does also bring up the question on whether or not you should maybe also return the number of cores?
If you look closely you will see that one has a capital A. I was not sure what the first was supposed to return as on my machine your script returned nothing for the first audio item.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.