Skip to content

Instantly share code, notes, and snippets.

@rahulinux
rahulinux / checkprocess.sh
Last active December 21, 2015 21:29
Check if process is running or not ?
function checkprocess() {
local process=$1
[[ $# -ne 1 ]] && { echo "Usage: $0 process"; exit 1; }
if ps cax | grep -q $process; then
echo "Process is running."
else
echo "Process is not running."
fi
}
@rahulinux
rahulinux / CheckSpace.sh
Last active December 21, 2015 21:29
Check disk spce , if disk space (Example /home = 90 %) then return 1 if no partition specified then it will check each partition
function CheckSpace() {
local Partition=$1
awk -v p=$Partition 'BEGIN{
threshold=80
cmd = "LC_ALL=C df -Ph "p
while( cmd | getline ) {
used=$5 # Usage in % of 5th colume
@rahulinux
rahulinux / AppendIfnotExists.sh
Last active December 21, 2015 21:29
Append List of lines, it will check in input file and if it not exists then it will append .
function AppendIfnotExists() {
while read s
do
inputFile="$2"
# if starting content matched then comment them
local startStr=$( echo "${s}" | cut -d" " -f1 )
grep -q "^#${s}" $inputFile || sed -i "s/^$startStr/#$startStr/g" $inputFile
@rahulinux
rahulinux / Menu.Show.sh
Created August 28, 2013 16:48
Menu using whiptail or dialog
t(){ type "$1"&>/dev/null;}
function Menu.Show {
local DIA DIA_ESC
while :; do
t whiptail && DIA=whiptail && break
t dialog && DIA=dialog && DIA_ESC=-- && break
exec date +s"No dialog program found"
done
declare -A o="$1"; shift
@rahulinux
rahulinux / CommentAppend.sh
Created August 28, 2013 16:55
Comment Match tags in configuration files and append new one
# This Function will handal escaped character / also using sed
CommentAppend() {
# Comment line and append line below commented line
local comment="$( echo "$1" | sed 's/\(\/\)/\\\//g' )" # search this line and comment it
local append="$( echo "$2" | sed 's/\(\/\)/\\\//g' )" # Append this line below commented line
local InputFile="$3"
sed -i "s/^${comment}/#${comment}/g" $InputFile
# if string does not exists in input file then add
@rahulinux
rahulinux / CheckLoadavg.sh
Created September 2, 2013 15:18
check load average of server and return if load more than 6 or passed threshold to the function
CheckLoadadv() {
# Set threshold value
local Threshold=$1
# if threshold not specify then take deafault 6
(( $Threshold )) || Threshold=6
# get input values from /proc/loadavg file
local Current_loadadv="$(cut -d" " -f1 /proc/loadavg )"
# take first value from inputdata
local Cur_int_loadadv="${Current_loadadv/.*}"
@rahulinux
rahulinux / GetTopProcess.sh
Created September 2, 2013 15:38
Print Top 10 Process using high %CPU
GetTopProcess() {
ps --no-headers -eo "%cpu,%mem,cmd,etime" |
sort -t. -k1 -r |
head -10
}
@rahulinux
rahulinux / status_msg.sh
Last active December 22, 2015 04:09
Status Msg
status_msg() {
local status=$?
local failed="$(tput bold)$(tput setf 4)[failed]$(tput sgr0)"
local succeeded="$(tput bold)$(tput setf 2)[succeeded]$(tput sgr0)"
(( $status == 0 )) &&
echo $succeeded ||
echo $failed
}
@rahulinux
rahulinux / flotToint.sh
Created September 5, 2013 14:24
Convert float to integer in bash
flotToint() {
printf "%.0f\n" "$@"
}
@rahulinux
rahulinux / YesOrNo.sh
Last active September 11, 2016 06:56
User Confirmation function, this function will prompt again if user has hit blank enter,
YesOrNo() {
while :
do
read -p 'Do you want to Continue (yes/no?): ' answer
case "${answer}" in
[yY]|[yY][eE][sS]) exit 0 ;;
[nN]|[nN][oO]) exit 1 ;;
esac
done
}