• Build logs, Tutorials and Hacks

    Tuesday, July 9, 2013

    Raspberry BASH terminal Custom Message


    I have recently started to work with the raspberry PI and have done a couple of projects with it. I am hoping to document some of the things I did with the PI in an attempt to share my experience. I hope that I get feedback because there is always room for improvement.

    This post is about how to get a custom message when you login in to your Raspberry pi or any linux system via terminal or SSH. I use the later more often because most of my PI implementations are headless. I use the PI, more as a linux based microcontroller development board and hence the login messages are useful because they give me a lot of information as soon as I login.

    [caption id="attachment_626" align="aligncenter" width="300"]Raspberry PI Standalone Raspberry PI Standalone[/caption]

    When a new terminal is created, the .bashrc file is invoked in the Raspbian distribution, it is advised to put the custom code in a file /home/pi/.bash_profile. At the time of this writing there is a similar article on the raspberry pi.org that details exactly this. The next thing that is needed is the content of the .bash_profile file.

    I started off with the script at the raspberry pi.org blog and added my own few lines. I wanted something like this..

    [code language="css"]
    let upSeconds="$(/usr/bin/cut -d. -f1 /proc/uptime)"
    let secs=$((${upSeconds}%60))
    let mins=$((${upSeconds}/60%60))
    let hours=$((${upSeconds}/3600%24))
    let days=$((${upSeconds}/86400))
    UPTIME=`printf "%d days, %02dh%02dm%02ds" "$days" "$hours" "$mins" "$secs"`

    # get the load averages
    read one five fifteen rest < /proc/loadavg

    echo "$(tput setaf 2)
    +-+-+-+-+-+-+-+ +-+-+-+-+-+-+-+ +-+-+-+-+-+-+-+
    |T|h|o|u|g|h|t| |P|r|o|c|e|s|s| |D|e|s|i|g|n|s|
    +-+-+-+-+-+-+-+ +-+-+-+-+-+-+-+ +-+-+-+-+-+-+-+
    .~~. .~~. `date +"%A, %e %B %Y, %r"`
    '. \ ' ' / .' `uname -srmo`$(tput setaf 1)
    .~ .~~~..~.
    : .~.'~'.~. : Uptime.............: ${UPTIME}
    ~ ( ) ( ) ~ Memory.............: `cat /proc/meminfo | grep MemFree | awk {'print $2'}`kB (Free) / `cat /proc/meminfo | grep MemTotal | awk {'print $2'}`kB (Total)
    ( : '~'.~.'~' : ) Load Averages......: ${one}, ${five}, ${fifteen} (1, 5, 15 min)
    ~ .~ ( ) ~. ~ Running Processes..: `ps ax | wc -l | tr -d " "`
    ( : '~' : ) IP Addresses.......: `/sbin/ifconfig eth0 | /bin/grep "inet addr" | /usr/bin/cut -d ":" -f 2 | /usr/bin/cut -d " " -f 1` and `wget -q -O - http://icanhazip.com/ | tail`
    '~ .~~~. ~' Free Disk Space....: `df -Pk | grep -E '^/dev/root' | awk '{ print $4 }' | awk -F '.' '{ print $1 }'`k on /dev/sda1
    Weather............: `curl -s "http://rss.accuweather.com/rss/liveweather_rss.asp?metric=1&locCode=205593" | sed -n '/Currently:/ s/.*: \(.*\): \([0-9]*\)\([CF]\).*/\2°\3, \1/p'`
    $(tput sgr0)"

    [caption id="attachment_628" align="aligncenter" width="300"]RPI Custom Message of the Day RPI Custom Message of the Day[/caption]

    The script is as follows:

    So here is my understand and side of the script. The up time is saved in variables and then printed. The load averages are obtained next for later printing. I added my touch on the logo which is displayed next. setaf sets the color of the text. I left that part as the original. I then go on to print the rest of the stuff. Getting the right weather is a bit tricky.

    I first visited the accuweather website for my city and the URL had the locCode used in the rss feed. For your implementation simply change the location code and you should be good.

    Thats about it. If there is anything that you would like to add please feel free to leave a comment and I will include it.

    No comments:

    Post a Comment