You are not logged in.

#1 2009-06-03 20:48:48

deltaecho
Member
From: Seattle, Washington (USA)
Registered: 2008-08-06
Posts: 182

I've colorized the output of `emptyswap'

I've been experimenting with colorizing scripts lately, and decided to play around with the `emptyswap' script from the wiki; here is the result:

#!/bin/bash                                       

### >_ emptyswap ###############################################################
# A little script that helps free memory.                                      #
################################################################################

# Make sure the user has root privileges
if [ $UID -ne 0 ]; then                 
        echo -e ' \e[1;5;31m{!}\e[25m \e[1;37mYou must be root to run this script.\e[m' 1>&2
        exit 1                                                                              
fi                                                                                          

echo -e "\e[1;34m:: \e[1;37mTesting to make sure you have enough free RAM ...\e[m"

free_mem=$(free -b | awk '/Mem:/ {print $4}')
used_swap=$(free -b | awk '/Swap:/ {print $3}')

if (( free_mem < used_swap )); then
        echo -e ' \e[1;5;31m{!}\e[25m \e[1;37mYou do not have enough free RAM to perform this operation.\e[m' 1>&2
        exit 1
else
        echo -e " \e[1;34m>_\e[m good."
fi

# Free memory before ...
echo -e "\n\e[1;34m:: \e[1;37mMemory usage before:\e[m\n"
echo -e "$(free -o -m | sed -e 's|^|    |g' \
                            -e 's|   \([0-9]\+\)|\\e[1;32m\1 MB\\e[m|g' \
                            -e '1 s:\(.*\):\\e[1;37m\1\\e[m:g' \
                            -e 's|\(Mem:\)|\\e[1;37m\1\\e[m|' \
                            -e 's|\(Swap:\)|\\e[1;37m\1\\e[m|')"

# Sync the memory first
echo -e "\n\e[1;34m:: \e[1;37mSyncing RAM to HDD ...\e[m"
sync

echo -e " \e[1;34m>_\e[m done."
echo -e "\n\e[1;34m:: \e[1;37mClearing the cache ...\e[m"

# Empty the cache
echo 3 > /proc/sys/vm/drop_caches
echo 0 > /proc/sys/vm/drop_caches

echo -e " \e[1;34m>_\e[m done."
echo -e "\n\e[1;34m:: \e[1;37mClearing SWAP ...\e[m"

# Toggle the SWAP partitions off/on to clear their contents
swapoff -a && swapon -a

# Checking the retval ...
if (( ! $? )); then
        echo -e " \e[1;34m>_\e[m done."
else
        echo -e ' \e[1;5;31m{!}\e[25m \e[1;37mSomething horrible happened.\e[m' 1>&2
fi

# Free memory after ...
echo -e "\n\e[1;34m:: \e[1;37mMemory usage after:\e[m\n"
echo -e "$(free -o -m | sed -e 's|^|    |g' \
                            -e 's|   \([0-9]\+\)|\\e[1;32m\1 MB\\e[m|g' \
                            -e '1 s:\(.*\):\\e[1;37m\1\\e[m:g' \
                            -e 's|\(Mem:\)|\\e[1;37m\1\\e[m|' \
                            -e 's|\(Swap:\)|\\e[1;37m\1\\e[m|')"

echo -e "\n\e[1;34m:: \e[1;37mFinished.\e[m"

An example of its output (without color, of course):

# emptyswap
:: Testing to make sure you have enough free RAM ...
 >_ good.

:: Output of `free' before:

                 total       used       free     shared    buffers     cached
    Mem:       3949 MB    1384 MB    2565 MB       0 MB      29 MB     120 MB
    Swap:      4095 MB       0 MB    4095 MB

:: Syncing RAM to HDD ...
 >_ done.

:: Clearing the cache ...
 >_ done.

:: Clearing SWAP ...
 >_ done.

:: Output of `free' after:

                 total       used       free     shared    buffers     cached
    Mem:       3949 MB    1380 MB    2568 MB       0 MB      26 MB     102 MB
    Swap:      4095 MB       0 MB    4095 MB

:: All done.

(06/03/2009) Minor edit.
(06/03/2009) Another one -- I think I'm done now.
(06/03/2009) Maybe not...
(06/03/2009) Wrapped the text a little better tongue

Last edited by deltaecho (2009-06-03 21:26:26)


Dylon

Offline

Board footer

Powered by FluxBB