LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Little help with a Bash script (https://www.linuxquestions.org/questions/linux-newbie-8/little-help-with-a-bash-script-4175502923/)

Enindu 04-25-2014 02:21 AM

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


Enindu 04-25-2014 02:52 AM

Bump

grail 04-25-2014 03:23 AM

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 :)

Glennzo 04-25-2014 03:38 AM

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.

pan64 04-25-2014 04:01 AM

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

Enindu 04-25-2014 04:23 AM

@grail and @pan64 - Got it. Thanks in advance.

grail 04-25-2014 05:44 AM

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 :)

Enindu 04-25-2014 08:02 AM

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?

Enindu 04-25-2014 09:06 AM

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

grail 04-25-2014 09:18 AM

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


Enindu 04-25-2014 09:28 AM

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}')

grail 04-25-2014 09:53 AM

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.

pan64 04-25-2014 10:15 AM

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


Enindu 04-25-2014 10:17 AM

But it did return my audio card twice.

Enindu 04-25-2014 10:23 AM

Quote:

Originally Posted by pan64 (Post 5159040)
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.

Enindu 04-25-2014 10:35 AM

How about this? Is this working?
Code:

#!/bin/bash

# Input colors
bold=$(tput bold)
regular=$(tput sgr0)
red=$(echo -en '\e[1;31m')
green=$(echo -en '\e[0;32m')

# Run as root script
if [ "$EUID" != 0 ]; then
        echo "${red}${bold}You must run this script as root.${regular}"; exit 1
fi

# Detect window manager
xfce=$(ps -e | grep -E '^.* xfce4-session$' > xfce)
gnome=$(ps -e | grep -E '^.* gnome-session$' > gnome)
kde=$(ps -e | grep -E '^.* kded4$' > kde)

# Clear screen
clear && sleep 1

# Display
echo "${green}${bold}System information${regular}"
echo "Hostname                  :" $(uname -n)
echo "Operating system          :" $(lsb_release -sirc)
echo "Kernel name and version  :" $(uname -srm)
echo "Total RAM                :" $(sed -n 's/MemTotal: *//gp' /proc/meminfo); echo
echo "${green}${bold}Machine information${regular}"
echo "Board manufacturer        :" $(dmidecode -t 2 | sed -n '/Manufacturer/s/^.*: //p')
echo "Model name                :" $(dmidecode -t 2 | sed -n '/Product Name/s/^.*: //p')
echo "BIOS vendor              :" $(dmidecode -t 0 | sed -n '/Vendor/s/^.*: //p')
echo "BIOS version              :" $(dmidecode -t 0 | sed -n '/Version/s/^.*: //p')
echo "Released date            :" $(dmidecode -t 0 | sed -n '/Release Date/s/^.*: //p'); echo
echo "${green}${bold}Processor information${regular}"
echo "Processor type            :" $(sed -rn '/model name/{s/^.*: |CPU//g;s/G/ &/p;q}' /proc/cpuinfo)
echo "Cache size                :" $(sed -n '/cache size/{s/^.*: //p;q}' /proc/cpuinfo); echo
echo "${green}${bold}Graphics information${regular}"
echo "Graphic card(s)          :" $(lspci | sed -n '/VGA/{s/^.*: //p;q}')
echo "Graphic vendor            :" $(glxinfo | sed -n '/GL vendor/s/^.*: //p')
echo "Direct rendering          :" $(glxinfo | sed -n '/direct /s/^.*: //p')
echo "Screen resolution        :" $(xdpyinfo | awk '/dimensions/{print $2}')
echo "GLX renderer              :" $(glxinfo | sed -n '/renderer/s/^.*: //p')
echo "GLX version              :" $(glxinfo | sed -n '/GL version/s/^.*: //p'); echo
echo "${green}${bold}Audio and network information${regular}"
echo "Audio card(s)            :" $(lspci | sed -n '/[Aa]udio controller/{s/^.*: //p;q}')
echo "Network card(s)          :" $(lspci | sed -n '/[Ee]thernet/{s/^.*: //p}')
echo "Network interface(s)      :" $(ip link | awk -F" *: *" 'NR>2 && /UP/{print $2}'); echo
echo "${green}${bold}Hard drive information${regular}"
echo "Root disk free space      :" $(df -h / | awk 'NR>1{print gensub(/(.)$/," \\1B",1,$4)}')
echo "Root disk mounted at      :" $(df -h / | awk 'NR>1{print $1}')
echo "SWAP partition mounted at :" $(swapon -s | awk 'NR>1{print $1}'); echo
echo "${green}${bold}Location information${regular}"
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) | awk -F"\"" '/countryFullName/{print $4}'); echo
echo "${green}${bold}Other information${regular}"
echo "Uptime                    :" $(uptime | awk '{print $3}' | sed 's/,//g') "(HH:MM)"

if [ -s xfce ]; then
        echo "Window manager            : Xfce" $(xfce4-session --version | grep xfce4-session | awk '{print $2}')
fi

if [ -s gnome ]; then
        echo "Window manager            : GNOME" $(gnome-session --version | awk '{print $2}')
fi

if [ -s kde ]; then
        echo "Window manager            : KDE" $(kded4 --version | grep -m 1 'KDE' | awk -F ':' '{print $2}' | awk '{print $1}')
fi

echo "Shell client              :" $SHELL

# End script
rm xfce gnome kde
echo; exit 0


pan64 04-25-2014 11:10 AM

no, at least there is a problem with it (what I found):
it creates 3 files and you ought to use full path for them: XFCE=/tmp/xfce or similar and use $XFCE in the script. Now the execution depends on the current directory.
Also instead ps -e | grep you can try pgrep

the line:
kded4 --version | grep -m 1 'KDE' | awk -F ':' '{print $2}' | awk '{print $1}'
is ugly, you can solve it with a single awk
kde4 --version | awk ' blabla '
Code:

(I have no kde, so cannot test it, but something like this should do the job)
kde4 --version | awk ' BEGIN { FS="[ :]"} /KDE/ {a++} a{print $2; exit} '

similar to xfce:
xfce4-session --version | awk '/xfce4-session/{print $2}'

grail 04-25-2014 11:36 AM

I would actually still urge against creating any files ... there does not seem to be any point when you could just as easily test a variable within the script as opposed to going
out to a file which you then have to clean up. Plus I find it invasive to have a script create files on my system.

I have taken pan64's suggestion about grouping to see if we could help the processing time. See if this helps:
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}"
}

IFS=$'\n'
glx_info=($(glxinfo | sed -n '/GL vendor/  s/^.*: //p;
                                /direct /    s/^.*: //p;
                                /renderer/  s/^.*: //p;
                                /GL version/ s/^.*: //p'))

cpu_info=($(sed -rn '/model name/{s/^.*: |CPU//g;s/G/ &/p};
                    /cache size/{s/^.*: //p;q}' /proc/cpuinfo))

(( EUID == 0 )) && dmi_info=($(dmidecode -t 2,0 | sed -rn '/manufacturer|name|vendor|version|date/Is/^.*: //p')) || is_root=false
IFS=

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        : ${dmi_info[3]}
        Model name                : ${dmi_info[4]}
        BIOS vendor              : ${dmi_info[0]}
        BIOS version              : ${dmi_info[1]}
        Released date            : ${dmi_info[2]}
    ${green}${bold}Processor information${regular}
        Processor type            : ${cpu_info[0]}
        Cache size                : ${cpu_info[1]}
    ${green}${bold}Graphics information${regular}
        Graphic card(s)          : $(lspci | sed -n '/VGA/{s/^.*: //p;q}')
        Graphic vendor            : ${glx_info[1]}
        Direct rendering          : ${glx_info[0]}
        GLX renderer              : ${glx_info[2]}
        GLX version              : ${glx_info[3]}
        Screen resolution        : $(xdpyinfo | awk '/dimensions/{print $2}')
    ${green}${bold}Audio and network information${regular}
        Audio card(s)            : $(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

I did look at lspci and df but they may return multiple entries which would then add to the array an unknown amount ... hence I left them

Enindu 04-26-2014 03:47 AM

Thanks for the help everyone. I'll take it.

@pan64,
I don't have KDE either. So how can I check is it working or not? Anyway thanks for help.

grail 04-26-2014 04:18 AM

I would probably suggest downloading an iso and making a vm with all the different choices you wish to test for and away you go :)

Enindu 04-26-2014 04:35 AM

Quote:

Originally Posted by grail (Post 5159463)
I would probably suggest downloading an iso and making a vm with all the different choices you wish to test for and away you go :)

I don't wanna go that far.

Enindu 04-26-2014 04:40 AM

Quote:

Originally Posted by pan64 (Post 5159091)
no, at least there is a problem with it (what I found):
it creates 3 files and you ought to use full path for them: XFCE=/tmp/xfce or similar and use $XFCE in the script. Now the execution depends on the current directory.
Also instead ps -e | grep you can try pgrep

the line:
kded4 --version | grep -m 1 'KDE' | awk -F ':' '{print $2}' | awk '{print $1}'
is ugly, you can solve it with a single awk
kde4 --version | awk ' blabla '
Code:

(I have no kde, so cannot test it, but something like this should do the job)
kde4 --version | awk ' BEGIN { FS="[ :]"} /KDE/ {a++} a{print $2; exit} '

similar to xfce:
xfce4-session --version | awk '/xfce4-session/{print $2}'

I didn't understand that "XFCE=/tmp/xfce" stuff. Can you please describe it? And, you mean by "pgrep" is this?
Code:

pgrep xfce4-session
Did you mean something like this?
Code:

#!/bin/bash

pgrep xfce4-session > /tmp/xfce; XFCE=/tmp/xfce
pgrep gnome-session > /tmp/gnome; GNOME=/tmp/gnome
pgrep kde4 > /tmp/kde; KDE=/tmp/kde

if [ -s $XFCE ]; then
        echo "Window manager            : Xfce" $(xfce4-session --version | grep xfce4-session | awk '{print $2}')
fi

if [ -s $GNOME ]; then
        echo "Window manager            : GNOME" $(gnome-session --version | awk '{print $2}')
fi

if [ -s $KDE ]; then
        echo "Window manager            : KDE" $(kded4 --version | grep -m 1 'KDE' | awk -F ':' '{print $2}' | awk '{print $1}')
fi

# End script
echo; exit 0


joe_2000 04-26-2014 06:28 AM

Quote:

Originally Posted by Enindu (Post 5158991)
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

On my i7:
Code:

root@host:~$ sed -n '/cache size/{s/^.*: //p;q}' /proc/cpuinfo
6144 KB
root@host:~$ sed -rn '/model name/{s/^.*: |CPU//g;s/G/ &/p;q}' /proc/cpuinfo
Intel(R) Core(TM) i7-3610QM  @ 2.30 GHz

On my i5:
Code:

root@host:~$ sed -n '/cache size/{s/^.*: //p;q}' /proc/cpuinfo
6144 KB
root@host:~$ sed -rn '/model name/{s/^.*: |CPU//g;s/G/ &/p;q}' /proc/cpuinfo
Intel(R) Core(TM) i5-3570S  @ 3.10 GHz


Enindu 04-26-2014 07:15 AM

Thanks joe_2000.

Enindu 04-26-2014 10:03 AM

I made it with your help and suggestions. How about this? Any suggestions for this?
Code:

#!/bin/bash
# Input colors
bold=$(tput bold)
regular=$(tput sgr0)
red=$(echo -en '\e[1;31m')
green=$(echo -en '\e[0;32m')

# Run as root script
if [ "$(id -u)" != 0 ]; then
        echo "${red}${bold}You must run this script as root.${regular}"; exit 1
fi

# Detect window manager
pgrep xfce4-session > /tmp/xfce; XFCE=/tmp/xfce
pgrep gnome-session > /tmp/gnome; GNOME=/tmp/gnome
pgrep kde4 > /tmp/kde; KDE=/tmp/kde

# Clear screen
clear && sleep 1

# Display
echo ${green}${bold}System information${regular}
echo "Hostname                  :" $(uname -n)
echo "Operating system          :" $(lsb_release -sirc)
echo "Kernel name and version  :" $(uname -srm)
echo "Total RAM                :" $(sed -n 's/MemTotal: *//gp' /proc/meminfo); echo
echo ${green}${bold}Machine information${regular}
echo "Board manufacturer        :" $(dmidecode -t 2 | sed -n '/Manufacturer/s/^.*: //p')
echo "Model name                :" $(dmidecode -t 2 | sed -n '/Product Name/s/^.*: //p')
echo "BIOS vendor              :" $(dmidecode -t 0 | sed -n '/Vendor/s/^.*: //p')
echo "BIOS version              :" $(dmidecode -t 0 | sed -n '/Version/s/^.*: //p')
echo "Released date            :" $(dmidecode -t 0 | sed -n '/Release Date/s/^.*: //p'); echo
echo ${green}${bold}Processor information${regular}
echo "Processor type            :" $(sed -rn '/model name/{s/^.*: |CPU//g;s/G/ &/p;q}' /proc/cpuinfo)
echo "Cache size                :" $(sed -n '/cache size/{s/^.*: //p;q}' /proc/cpuinfo); echo
echo ${green}${bold}Graphics information${regular}
echo "Graphic card(s)          :" $(lspci | sed -n '/VGA/{s/^.*: //p;q}')
echo "Graphic vendor            :" $(glxinfo | sed -n '/GL vendor/s/^.*: //p')
echo "Direct rendering          :" $(glxinfo | sed -n '/direct /s/^.*: //p')
echo "Screen resolution        :" $(xdpyinfo | awk '/dimensions/{print $2}')
echo "GLX renderer              :" $(glxinfo | sed -n '/renderer/s/^.*: //p')
echo "GLX version              :" $(glxinfo | sed -n '/GL version/s/^.*: //p'); echo
echo ${green}${bold}Audio and network information${regular}
echo "Audio card(s)            :" $(lspci | sed -n '/[Aa]udio controller/{s/^.*: //p;q}')
echo "Network card(s)          :" $(lspci | sed -n '/[Ee]thernet/{s/^.*: //p}')
echo "Network interface(s)      :" $(ip link | awk -F" *: *" 'NR>2 && /UP/{print $2}'); echo
echo ${green}${bold}Hard drive information${regular}
echo "Root disk free space      :" $(df -h / | awk 'NR>1{print gensub(/(.)$/," \\1B",1,$4)}')
echo "Root disk mounted at      :" $(df -h / | awk 'NR>1{print $1}')
echo "SWAP partition mounted at :" $(swapon -s | awk 'NR>1{print $1}'); echo
echo ${green}${bold}Location information${regular}
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) | awk -F"\"" '/countryFullName/{print $4}'); echo
echo ${green}${bold}Other information${regular}
echo "Uptime                    :" $(uptime | awk '{print $3}' | sed 's/,//g') "(HH:MM)"
echo "Shell client              :" $SHELL

if [ -s $XFCE ]; then
        echo "Session                  : Xfce" $(xfce4-session --version | grep xfce4-session | awk '{print $2}')
fi

if [ -s $GNOME ]; then
        echo "Session                  : GNOME" $(gnome-session --version | awk '{print $2}')
fi

if [ -s $KDE ]; then
        echo "Session                  : KDE" $(kded4 --version | grep -m 1 'KDE' | awk -F ':' '{print $2}' | awk '{print $1}')
fi

# End script
echo; exit 0

As I saw "uptime -p" command won't work with Ubuntu and Fedora. So, I didn't change it.

Enindu 04-26-2014 11:17 AM

Bump

grail 04-26-2014 11:41 AM

Please don't 'bump' as everyone is volunteering help so it is perceived as impolite to try and force an answer.

I am still not sure why you persist with using files at all. Also, by using a specific word and not a random name you can hit an issue where you might overwrite a file on someone's
system, ie. if /tmp/xfce already exists and you call the following code:
Code:

pgrep xfce4-session > /tmp/xfce; XFCE=/tmp/xfce
This will have just wiped the file and replaced it with whatever you are doing.

As I have said several times, why not simply store the value in a variable and then test that:
Code:

XFCE=$(pgrep xfce4-session)

if [[ -n $XFCE ]]
...

As for uptime, I guess -p may not be standard, but your current solution does not work very well once your machine has been up for days and not just hours and minutes.

Enindu 04-26-2014 12:11 PM

Sorry for the "bump".

That's what I want to know. I wanted to store value in a variable too but I stuck at somewhere and I didn't know where it is. So I gave up it. As I see now, I didn't add "[[ .. ]]" before. That's it. Thanks for the help.

EDIT : As you see, I'm a noob in this Bash stuff. And I want to say, I didn't refer any Bash/Shell guide to make this so far. So, some of commands may be wrong and stupid. That's why I'm asking for help here. Thanks.

grail 04-26-2014 02:56 PM

Well here are a couple to help you investigate further:

http://tldp.org/LDP/abs/html/
http://mywiki.wooledge.org/TitleIndex

pan64 04-27-2014 02:32 AM

Quote:

Originally Posted by Enindu (Post 5159638)
That's what I want to know. I wanted to store value in a variable too but I stuck at somewhere and I didn't know where it is. So I gave up it. As I see now, I didn't add "[[ .. ]]" before. That's it. Thanks for the help.

No, you do not need to give it up just ask, how to do this or that, how to solve whatever ....
To store the result of a command in a variable use: VARIABLE=$(command) - [[ and ]] is used in comparison.

Quote:

Originally Posted by Enindu (Post 5159638)
As you see, I'm a noob in this Bash stuff. And I want to say, I didn't refer any Bash/Shell guide to make this so far. So, some of commands may be wrong and stupid. That's why I'm asking for help here. Thanks.

That is ok, there is no stupid question. Try, practice, and learn....


All times are GMT -5. The time now is 07:27 PM.