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.

OpenSUSE Linux Rants
Official OpenSUSE Linux Site

internal links:

categories:

SUSE Resources

search blog:

archives:

May 2008
S M T W T F S
« Apr   Jun »
 123
45678910
11121314151617
18192021222324
25262728293031

55 queries. 1.734 seconds