Skip to content

Instantly share code, notes, and snippets.

@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 / 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 / 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 / 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 / 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
}