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 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 05:58 PM.