LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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


Reply
  Search this Thread
Old 04-25-2014, 03:21 AM   #1
Enindu
Member
 
Registered: Apr 2014
Location: Colombo, Sri Lanka.
Distribution: Arch Linux
Posts: 69

Rep: Reputation: 13
Little help with a Bash script


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

Last edited by Enindu; 04-25-2014 at 04:00 AM.
 
Old 04-25-2014, 03:52 AM   #2
Enindu
Member
 
Registered: Apr 2014
Location: Colombo, Sri Lanka.
Distribution: Arch Linux
Posts: 69

Original Poster
Rep: Reputation: 13
Bump
 
Old 04-25-2014, 04:23 AM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,256

Rep: Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686
A few comments, although very nice overall

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


Hope some of that helps
 
2 members found this post helpful.
Old 04-25-2014, 04:38 AM   #4
Glennzo
Member
 
Registered: May 2003
Location: Salem, Mass USA
Distribution: Fedora
Posts: 393

Rep: Reputation: 47
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.
 
Old 04-25-2014, 05:01 AM   #5
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 8,133

Rep: Reputation: 2273Reputation: 2273Reputation: 2273Reputation: 2273Reputation: 2273Reputation: 2273Reputation: 2273Reputation: 2273Reputation: 2273Reputation: 2273Reputation: 2273
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):
Code:
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')
can be simplified by one glxinfo and one single awk
 
1 members found this post helpful.
Old 04-25-2014, 05:23 AM   #6
Enindu
Member
 
Registered: Apr 2014
Location: Colombo, Sri Lanka.
Distribution: Arch Linux
Posts: 69

Original Poster
Rep: Reputation: 13
@grail and @pan64 - Got it. Thanks in advance.
 
Old 04-25-2014, 06:44 AM   #7
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,256

Rep: Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686
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
 
1 members found this post helpful.
Old 04-25-2014, 09:02 AM   #8
Enindu
Member
 
Registered: Apr 2014
Location: Colombo, Sri Lanka.
Distribution: Arch Linux
Posts: 69

Original Poster
Rep: Reputation: 13
Thanks for re-scripting it. I didn't really expect it. As I see linuxquestions.org is very helpful forum. Saying once again THANKS EVERYONE.

@grail - A bit a personal question. As I see you're using Manjaro. Am I know you?
 
Old 04-25-2014, 10:06 AM   #9
Enindu
Member
 
Registered: Apr 2014
Location: Colombo, Sri Lanka.
Distribution: Arch Linux
Posts: 69

Original Poster
Rep: Reputation: 13
Can I know output of this command in a computer like i5 or i7?
Code:
sed -n '/cache size/{s/^.*: //p;q}' /proc/cpuinfo
And this one too.
Code:
sed -rn '/model name/{s/^.*: |CPU//g;s/G/ &/p;q}' /proc/cpuinfo

Last edited by Enindu; 04-25-2014 at 10:08 AM.
 
Old 04-25-2014, 10:18 AM   #10
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,256

Rep: Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686
Quote:
@grail - A bit a personal question. As I see you're using Manjaro. Am I know you?
Not that I am aware of, but ... Hello all the same

I am running on an i7 so here is the output I receive:
Code:
$ sed -n '/cache size/{s/^.*: //p;q}' /proc/cpuinfo
8192 KB
$ sed -rn '/model name/{s/^.*: |CPU//g;s/G/ &/p;q}' /proc/cpuinfo
Intel(R) Core(TM) i7          950  @ 3.07 GHz
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?
Code:
$ sed -n '/cores/{s/^.*: //p;q}' /proc/cpuinfo
4
 
Old 04-25-2014, 10:28 AM   #11
Enindu
Member
 
Registered: Apr 2014
Location: Colombo, Sri Lanka.
Distribution: Arch Linux
Posts: 69

Original Poster
Rep: Reputation: 13
I'd like to keep that one. And another one question here. Why did you add same command twice in here? Is it typo?
Code:
Audio card(s)             : $(lspci | sed -n '/audio/{s/^.*: //p;q}') $(lspci | sed -n '/Audio/{s/^.*: //p;q}')
 
Old 04-25-2014, 10:53 AM   #12
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,256

Rep: Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686
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.
 
Old 04-25-2014, 11:15 AM   #13
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 8,133

Rep: Reputation: 2273Reputation: 2273Reputation: 2273Reputation: 2273Reputation: 2273Reputation: 2273Reputation: 2273Reputation: 2273Reputation: 2273Reputation: 2273Reputation: 2273
I would prefer this one:
Code:
lspci | sed -n '/[Aa]udio/{s/^.*: /Audio card(s)             : /p}'
and in several cases you can avoid $( ... ) and command repetition, that will speed this script up
Code:
# instead of:
        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')
# try this:
glxinfo | sed -n '/GL vendor/  s/^.*: /        Graphic vendor            : /p;
                  /direct /    s/^.*: /        Direct rendering          : /p;
                  /renderer/   s/^.*: /        GLX renderer              : /p;
                  /GL version/ s/^.*: /        GLX version               : /p;'

but you may need to reorganize that cat << EOF command
 
1 members found this post helpful.
Old 04-25-2014, 11:17 AM   #14
Enindu
Member
 
Registered: Apr 2014
Location: Colombo, Sri Lanka.
Distribution: Arch Linux
Posts: 69

Original Poster
Rep: Reputation: 13
But it did return my audio card twice.

Last edited by Enindu; 04-25-2014 at 11:18 AM.
 
Old 04-25-2014, 11:23 AM   #15
Enindu
Member
 
Registered: Apr 2014
Location: Colombo, Sri Lanka.
Distribution: Arch Linux
Posts: 69

Original Poster
Rep: Reputation: 13
Quote:
Originally Posted by pan64 View Post
I would prefer this one:
Code:
lspci | sed -n '/[Aa]udio/{s/^.*: /Audio card(s)             : /p}'
That's it. This one solve my problem. Thanks. I like to keep this script in $(...) way. It's easy to me.

Last edited by Enindu; 04-25-2014 at 11:28 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to end the bash script using commands in bash not manually by pressing ctrl+c Sanpreet Singh Linux - Newbie 1 07-03-2013 02:04 PM
[SOLVED] Converting Script from Linux (GNU) Bash 4 to Solaris Bash 2.05 - Any cheat sheet? oly_r Solaris / OpenSolaris 6 05-03-2013 09:25 AM
Variables and Mkvextract in a bash script and a good resource for bash help? gohmifune Linux - General 9 04-13-2011 09:37 AM
SSH connection from BASH script stops further BASH script commands tardis1 Linux - Newbie 3 12-06-2010 09:56 AM
[SOLVED] Using a long Bash command including single quotes and pipes in a Bash script antcore Linux - General 9 07-22-2009 12:10 PM


All times are GMT -5. The time now is 06:46 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration