OpenSUSE Linux Rants

OpenSUSE Linux Tips, tricks, how-tos, opinions, and news

My Resume  -  My LinkedIn Profile

May 28, 2008

“What’s up?” bash script redone, revised, and mo’ bettah

by @ 12:25 pm. Filed under bash, freebies

A couple of days ago, I put together a .bashrc alias. Well, it won’t work right. The commands in “ marks only execute when the shell opens, and then the variables they’re assigned to stay the same, even when you invoke the alias. Thus, the current working directory and the date and stuff that should change each time you invoke the alias, don’t.

To fix this, I changed it from an alias into a small bash script. To use it, just put it into your ~/bin folder and invoke it like you would any other command.

The contents are thus:

#!/bin/sh

# ORIGINALLY WRITTEN BY SCOTT MORRIS (http://www.suseblog.com/) on 2008-05-28

# DISPLAY THE MEMORY AND SWAP AVAILABLE FOR THE SYSTEM
function memdisp {

IFS=$' '

MEM=`free -mot | head -n 2 | tail -n 1`
COUNT=1

printf "  Memory:"

for ITEM in $MEM
do
        if [ $COUNT -eq 2 ] ; then
                printf "\tTotal: $ITEM Mb"
        fi

        if [ $COUNT -eq 3 ] ; then
                printf "\tUsed: $ITEM Mb"

        fi

        if [ $COUNT -eq 4 ] ; then
                printf "\tFree: $ITEM Mb\n"
        fi

        COUNT=$[COUNT+1]
done

MEM=`free -mot | tail -n 2 | head -n 1`

COUNT=1

printf "  Swap:\t"

for ITEM in $MEM
do
        if [ $COUNT -eq 2 ] ; then
                printf "\tTotal: $ITEM Mb"
        fi

        if [ $COUNT -eq 3 ] ; then
                printf "\tUsed: $ITEM Mb"

        fi

        if [ $COUNT -eq 4 ] ; then
                printf "\tFree: $ITEM Mb\n"
        fi

        COUNT=$[COUNT+1]
done

}

# DISPLAY THE IP ADDRESS OF ETH0
function ipaddr {

IFS=$' '
IPINF=`/sbin/ifconfig eth0 | head -n 2 | tail -n 1`
COUNT=1

printf "  IP (eth0):"
for ITEM in $IPINF
do
        if [ $COUNT -eq 2 ] ; then
#                 printf "$ITEM\n"
                
                IFS=$':'
                CT=1
                for DATA in $ITEM
                do
                	if [ $CT -eq 2 ] ; then
                		printf "\t$DATA\n"
                	fi
                	CT=$[CT+1]
                done
                
        fi
        COUNT=$[COUNT+1]
done
IFS=$'\n'

}

# COLLECT SOME INFO
IFS=$'\n'
UPTIME=`uptime`
D_UP=${UPTIME:1}
MYGROUPS=`id`
DATE=`date`
KERNEL=`uname -a`
CPWD=`pwd`

# OUTPUT THE DATA
printf "  user:\t\t"$USER" (uid:"$UID")\n"
printf "  groups:\t"$MYGROUPS"\n"
printf "  working dir:\t"$CPWD"\n"
printf "  home dir:\t"$HOME"\n"
printf "  hostname:\t"$HOSTNAME"\n"
ipaddr
printf "  date:\t\t"$DATE"\n"
printf "  uptime:\t"$D_UP"\n"
printf "  kernel:\t"$KERNEL"\n"
printf "  cpu:\t\t"$CPU"\n"

memdisp

If you copy and paste it, save it as ~/sup, and don’t forget to make it executable with chmod +x ~/sup.

Example output:

[1211][scott@tomahawk:~]$ sup
  user:         scott (uid:1000)
  groups:       uid=1000(scott) gid=100(users) groups=16(dialout),33(video),100(users)
  working dir:  /home/scott
  home dir:     /home/scott
  hostname:     tomahawk
  IP (eth0):    192.168.0.110
  date:         Wed May 28 12:11:58 MDT 2008
  uptime:       12:11pm  up   8:07,  7 users,  load average: 0.46, 0.43, 0.30
  kernel:       Linux tomahawk 2.6.24-default #1 SMP Sat Jan 26 21:54:20 MST 2008 x86_64 x86_64 x86_64 GNU/Linux
  cpu:          x86_64
  Memory:       Total: 940 Mb   Used: 756 Mb    Free: 183 Mb
  Swap:         Total: 1913 Mb  Used: 0 Mb      Free: 1913 Mb
[1211][scott@tomahawk:~]$

Here’s a link to the script: sup.tar.bz2

Download the script.

Run: tar -xvf sup.tar.bz2

Run: mv sup ~/bin

Run: sup

Enjoy.

8 Responses to ““What’s up?” bash script redone, revised, and mo’ bettah”

  1. Victor Ryden Says:

    ./sup: line 66: syntax error near unexpected token `(‘
    ./sup: line 66: `printf ” IP (eth0):”’

  2. Scott Morris Says:

    Grab it from the link to the archived version. That should work better for you. Thanks for the heads-up.

  3. Victor Ryden Says:

    Than worked – Thanks for all you work!

  4. Scott Morris Says:

    Victor,
    No problem, glad to help out! Thanks for stopping by.

  5. Lonnie Olson Says:

    I like your idea of a single script to grab lots of system stats. But I wanted to display all IP addresses on a machine from all NICs. When I started working on it, I discovered that some clever usage of awk could *greatly* simplify the code.

    This line will replace the entire ipaddr function:
    ip -o addr | awk ‘/inet /{print ” IP (” $2 “):\t” $4}’

    This command will replace the entire memdisp function:
    free -mot | awk ‘
    /Mem/{print ” Memory:\tTotal: ” $2 “Mb\tUsed: ” $3 “Mb\tFree: ” $4 “Mb”}
    /Swap/{print ” Swap:\t\tTotal: ” $2 “Mb\tUsed: ” $3 “Mb\tFree: ” $4 “Mb”}’

    Also, the first line should be /bin/bash not /bin/sh. If you depend on bash specific features, specify bash for compatibility. Most systems don’t link /bin/sh to /bin/bash.

  6. Lonnie Olson Says:

    Also, replacing
    MYGROUPS=`id`
    with
    MYGROUPS=`groups`

    Simplifies output greatly.

  7. Jay Says:

    Hi,

    Tried running it from konsole. I get an error message:

    user@linux-4a48:~> sup
    bash: /home/user/bin/sup: /bin/bash: bad interpreter: Permission denied
    user@linux-4a48:~>

    Same thing happens when I try it as ‘root’ (su-)

    Jay

  8. Scott Morris Says:

    Let’s see….. take a look at the output of the following command:
    /usr/bin/env sh

    If you just get a blank bash prompt, you’re in business.

    Replace everything after the “#!” in the script with “/usr/bin/env sh”.
    Make sure it’s in /bin .
    Make sure it’s executable, with this command:

    chmod +x /bin/sup

    Let me know if that does it for you. Perhaps I will change the script to execute that way.

    By the way, grab the new, updated version.

    Thanks for stopping by, and let everyone know how it goes.

OpenSUSE Linux Rants
Official OpenSUSE Linux Site

internal links:

categories:

SUSE Resources

search blog:

archives:

May 2021
S M T W T F S
« Feb    
 1
2345678
9101112131415
16171819202122
23242526272829
3031  

60 queries. 0.490 seconds