Last active
August 22, 2016 04:10
-
-
Save mathieu-aubin/6eee476ea310069286fc8d6189486bc6 to your computer and use it in GitHub Desktop.
Bash Typewritter-style demo script. Also serves a purpose, freeing up some ram on low-level servers.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# | |
# free-ram.sh | |
# | |
# Copyright 2016 Mathieu Aubin <[email protected]> | |
# | |
# This program is free software; you can redistribute it and/or modify | |
# it under the terms of the GNU General Public License as published by | |
# the Free Software Foundation; either version 2 of the License, or | |
# (at your option) any later version. | |
# | |
# This program is distributed in the hope that it will be useful, | |
# but WITHOUT ANY WARRANTY; without even the implied warranty of | |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
# GNU General Public License for more details. | |
# | |
# You should have received a copy of the GNU General Public License | |
# along with this program; if not, write to the Free Software | |
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, | |
# MA 02110-1301, USA. | |
# | |
# This is plain for fun... hahah.. Works tho. | |
# lots of sleep.. | |
# Command is: $> sync; echo 3 >/proc/sys/vm/drop_caches | |
# | |
# Change SPEED_VALUE to set the typewritter-effect speed | |
# | |
SPEED_VALUE=15; | |
# Quick root/sudo user check, else exit. | |
# EDIT: Moved up top, Used escaped values instead of ${MONITOR_XXXX} | |
function checkUser { | |
if [[ $EUID -ne 0 ]]; then | |
echo -e "\E[1mFREE-RAM\E[0m must be run as root, type: \E[32msudo !!\E[0m to sudo script." 1>&2; | |
exit 1; | |
fi | |
} | |
# Check if we are root/sudo... | |
checkUser; | |
# Define color/reset constants | |
MONITOR_BOLD="\E[1m"; | |
MONITOR_COLOR="\E[32m"; | |
MONITOR_RESET="\E[0m"; | |
# EDIT: Set the correct "ram headers" -- seems i wasnt paying attention | |
RAMHEADER_TXT=" Total Used Free Shared Buffers Cached"; | |
# The typewritter style function | |
function typeslow { | |
V=${1}; shift; shift; | |
for (( i=0; i<${#V}; i++ )); do | |
echo -n "${V:$i:1}"; | |
sleep $(printf 0."%02d" $((${RANDOM} % ${SPEED_VALUE} + 3))); | |
done | |
} | |
# Clears the screen | |
clear; | |
# Outputs the credit line | |
echo -e "${MONITOR_BOLD}"; | |
typeslow "FREE-RAM v$((${RANDOM} % 5000 + 483))+xYZ½ Created by Mathieu Aubin "; | |
echo -en "${MONITOR_RESET}${MONITOR_COLOR}"; | |
typeslow "<[email protected]>"; | |
echo -en "${MONITOR_RESET}${MONITOR_BOLD}"; | |
typeslow " email for fun!"; | |
echo -en "${MONITOR_RESET}\n"; | |
# Get the BEFORE ram status and sleep a bit... zzzZZzz | |
RAM_BEFORE=$(free -h | grep -o "Mem.*") && sleep 3; | |
# Quickly output the headers and typewrite the BEFORE ram status | |
echo -e "\n${MONITOR_BOLD}BEFORE RAM:${MONITOR_RESET}${MONITOR_COLOR}${RAMHEADER_TXT}${MONITOR_RESET}"; | |
typeslow "${RAM_BEFORE}"; | |
sleep 2; | |
# Outputs 'yeah we are capable...' | |
echo -e "${MONITOR_COLOR}"; | |
typeslow "Trying to free up as much RAM as possible..."; | |
# Sleep and then, magically write 'done.' | |
sleep 4; | |
echo -en "${MONITOR_RESET}${MONITOR_BOLD} done.${MONITOR_RESET}\n"; | |
# Actual ram clearing line | |
sync && echo 3 > /proc/sys/vm/drop_caches; | |
# Get the AFTER ram status and sleep a bit, makes it look hardcore... | |
RAM_AFTER=$(free -h | grep -o "Mem.*") && sleep 2; | |
# Quickly output the headers and typewrite the AFTER ram status | |
echo -e "\n${MONITOR_BOLD}AFTER RAM :${MONITOR_RESET}${MONITOR_COLOR}${RAMHEADER_TXT}${MONITOR_RESET}"; | |
typeslow "${RAM_AFTER}"; | |
sleep 2; | |
# Tell the user they are finally done with this... hahha | |
echo -e "\n${MONITOR_BOLD}"; | |
typeslow "Process terminated."; | |
sleep 1; | |
echo -en "${MONITOR_RESET}\n"; | |
exit; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment