OpenSUSE Linux Rants

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

My Resume  -  My LinkedIn Profile

May 23, 2008

Quick “what’s up?” alias for your .bashrc file

by @ 1:40 pm. Filed under bash, General Linux, SUSE Tips & Tricks

NOTE: Don’t use this, it has been updated. Go here for latest.

I was fooling around with an alias that would help someone know at a glance what machine they are on, who they are logged in as, their current path, the date, uptime, and some memory stats. This is something that I have found helpful when I have several remote servers open and logged into each one with several different accounts. It’s easy to know at a glance where I am doing what.

To implement this alias, pull open your ~/.bashrc file, and paste all of this at the end of it:

function memdisp {

MEM=`free -mot | head -n 2 | tail -n 1`
COUNT=1
for ITEM in $MEM
do
        if [ $COUNT -eq 2 ] ; then
                printf "  Total RAM:\t$ITEM Mb\n"
        fi

        if [ $COUNT -eq 3 ] ; then
                printf "  Used RAM:\t$ITEM Mb\n"

        fi

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

        COUNT=$[COUNT+1]
done

MEM=`free -mot | tail -n 2 | head -n 1`
COUNT=1
for ITEM in $MEM
do
        if [ $COUNT -eq 2 ] ; then
                printf "  Total SWAP:\t$ITEM Mb\n"

        fi

        if [ $COUNT -eq 3 ] ; then
                printf "  Used SWAP:\t$ITEM Mb\n"

        fi

        if [ $COUNT -eq 4 ] ; then
                printf "  Free SWAP:\t$ITEM Mb\n"

        fi

        COUNT=$[COUNT+1]
done

}

UPTIME=`uptime`
D_UP=${UPTIME:2}

alias sup="
printf '  my user:\t`whoami`\n'
printf '  my groups:\t`id`\n'
printf '  hostname:\t`hostname`\n'
printf '  domain:\t`dnsdomainname`\n'
printf '  date:\t\t`date`\n'
printf '  uptime:\t$D_UP\n'
printf '  kernel:\t`uname -a`\n'
memdisp
"

Then save the file, and run “source ~/.bashrc”. To use the alias, type ‘sup’ (short for “what’s up?”) and hit ENTER. You should see something like this:

[1457][scott@suse-linux:~]$ sup
  my user:      scott
  my groups:    uid=1000(scott) gid=100(users) groups=16(dialout),33(video),100(users)
  hostname:     suse-linux
  domain:       truenorth.local
  date:         Fri May 23 14:57:23 MDT 2008
  uptime:       2:57pm  up 5 days 18:35,  15 users,  load average: 0.17, 0.12, 0.13
  kernel:       Linux suse-linux 2.6.24-default #1 SMP Sat Jan 26 00:29:01 MST 2008 i686 i686 i386 GNU/Linux
  Total RAM:    1264 Mb
  Used RAM:     1234 Mb
  Free RAM:     30 Mb
  Total SWAP:   2055 Mb
  Used SWAP:    213 Mb
  Free SWAP:    1841 Mb
[1457][scott@suse-linux:~]$

This is great for when you come back to work from a long weekend, have 300 terminal windows open, logged into 32 servers with 43 different accounts.

If you wanted, you could also put this into the /etc/skel/.bashrc file so that all new users on your machine will automatically have this alias. Change to suit your taste.

If you do this, and an FBI satellite crashes into your new Porsche, it’s not my fault.

I am under no delusions of grandeur here. If you know of a better way to output the info, or more info that you’d like to output, or you modify/change it to make it better, please let us know. Suggestions, tips, tricks, comments, and even mild insults are welcome.

3 Responses to “Quick “what’s up?” alias for your .bashrc file”

  1. Richard Says:

    # sup
    [1] 671
    [2] 672
    printf: usage: printf [-v var] format [arguments]
    bash: apos: command not found
    bash: apos: command not found

  2. Michael Says:

    A more compact version using awk (with a somewhat tweaked output):

    alias sup=”( id; hostname; dnsdomainname; date; uptime; uname -a; free -mot ) | awk ‘\
    BEGIN {FS=\”[ =]\”};\
    (NR == 1) {FS=\” \”; print \” user : \” \$2 \”\n groups : \” \$6};\
    (NR == 2) {print \” hostname : \” \$0};\
    (NR == 3) {print \” domain : \” \$0};\
    (NR == 4) {print \” date : \” \$0};\
    (NR == 5) { NF=NF; print \” uptime : \” \$0};\
    (NR == 6) {print \” kernel : \” \$0};\
    (NR == 8) {print \” RAM : \” \$2 \” Mb Total, \” \$3 \” Mb Used, \” \$4 \” Mb Free\”};\
    (NR == 9) {print \” SWAP : \” \$2 \” Mb Total, \” \$3 \” Mb Used, \” \$4 \” Mb Free\”}'”

  3. Scott Morris Says:

    Michael, that totally rules. Thanks for sharing. Everyone, check that out. Thanks for stopping by.

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.674 seconds