All my function
All my function below:
root@utam-f9:/home/.env # cat function
#!/bin/bash
# —————————————————————————————————-
today () {
echo -ne “Today’s date is: “
echo -ne ‘\e[1m’
date +”%A, %B %-d, %Y”
echo -ne ‘\e[m’
}
# —————————————————————————————————-
x () {
if [ -f $1 ] ; then
case $1 in
*.tar.bz2) tar xjf $1 ;;
*.tar.gz) tar xzf $1 ;;
*.bz2) bunzip2 $1 ;;
*.rar) unrar x $1 ;;
*.gz) gunzip $1 ;;
*.tar) tar xf $1 ;;
*.tbz2) tar xjf $1 ;;
*.tgz) tar xzf $1 ;;
*.zip) unzip $1 ;;
*.Z) uncompress $1 ;;
*.7z) 7z x $1 ;;
*) echo “’$1’ cannot be extracted via extract()” ;;
esac
else
echo “’$1’ is not a valid file”
fi
}
# —————————————————————————————————-
vi () {
echo $@
for i in “$@” ; do
if [ -f “$i” -a ! -w “$i” ] ; then precommand=”sudo ” ; break ; fi
done
if [ “x$DISPLAY” = “x” ] ; then
${precommand}vim “$@”
else
${precommand}gedit “$@” &
echo “$? is “
if [ “$?” != “0” ] ; then setz ; ${precommand}vim “$@” ;fi
fi
}
# —————————————————————————————————-
startwm () {
if [ -z “$1” ]
then
echo Usage: startwm path [screen]
return 1
fi
SCREEN_NUM=$2
[ -z “$SCREEN_NUM” ] && SCREEN_NUM=”1”
xinit $1 — :${SCREEN_NUM}
}
# —————————————————————————————————-
function rv_color () {
if [ $? -eq 0 ]
then
color=’\033[32m’ # GREEN
else
color=’\033[31m’ # RED
fi
echo -e $color
}
# —————————————————————————————————-
cpx () {
if [ -z “$1” -o -z “$2” ]
then
echo Usage: cpx src dest
return 1
fi
tar cpf - $1 | (cd $2 && tar xvpBf -)
return $?
}
# —————————————————————————————————-
# Zombie ps
zomb_ps () {
ps hr -Nos | awk ‘$1==”Z” {print $1}’
}
# —————————————————————————————————-
# trunc: truncate string $1 to length $2, prefixed with ‘…’
trunc () {
if [ -z “$1” -o -z “$2” ]
then
echo Usage: trunc string length
return 1
fi
if [ ${#1} -le $2 ]
then
echo $1
else
local offset=$(( ${#1} - $2 + 3 ))
echo “…${1:$offset:$2}”
fi
}
# —————————————————————————————————-
# xtitle: Set caption of terminal-emulator title bar to args
xtitle () {
echo -e “\033]0;$*\007”
}
# —————————————————————————————————-
# xtrun: Invoke xtitle on args, then execute args
xtrun () {
xtitle $*
$*
}
# —————————————————————————————————-
# xrun: run a window manager (or any X app) in a new XServer session.
xrun () {
if [ -z “$1” ]
then
echo Usage: xrun path [screen]
return 1
fi
SCREEN_NUM=$2
[ -z “$SCREEN_NUM” ] && SCREEN_NUM=”1”
xinit $1 — :${SCREEN_NUM}
}
# —————————————————————————————————-
# battery_life : Echo the percentage of battery life remaining
battery_life () {
life=$(acpi -b | cut -d , -f 2)
# NOTE: the trailing % is stripped
echo ${life%\%}
}
# —————————————————————————————————-
# battery_color: Change console font based on battery life
battery_color () {
num=$(battery_life)
if [ $num -gt 25 ]
then
# 26-100% remaining : GREEN
color=’\033[32m’
elif [ $num -gt 10 ]
then
# 11-25% remaining : YELLOW
color=’\033[1;33m’
else
# 0-10% remaining : RED
color=’\033[31m’
fi
echo -e “${color}”
}
# —————————————————————————————————-
# uprompt: Build and echo (NOT return!) a user prompt.
# This echoes “user@host:path”, where path is truncated to
# 1/3 of the screen size (using the trunc() function).
# The entire string is colored RED if UID is 0.
uprompt () {
local prompt=’\u@\h:`trunc $PWD $((COLUMNS/3))`’
if [ $UID -eq 0 ]
then
prompt=”${RED}${prompt}${NC}”
fi
echo $prompt
}
# —————————————————————————————————-
# fixtty : reset TTY after user cat’ed a binary file
fixtty () {
stty sane
reset
}
# —————————————————————————————————-
# quick backup files ;)
bak () {
typeset file=”“
if [ ! -n “$1” ]
then
echo “Usage: bak file1 file2 etc”
exit 1
fi
for file in $@;
do
now=$(date +%Y%m%d-%H%M%S)
cp $file $file.$now
done
return 0
}
# —————————————————————————————————-
# quick link files ;)
ln2 () {
set -x
typeset DIR1 DIR2
if [ ! -n “$2” ]
then
echo “Usage: ln2 dir1 dir2”
return 1
fi
if [ -d ${1} ]
then
DIR1=$(cd ${1} && echo `pwd`)
else
echo “${1} is not a directory”
return 1
fi
if [ -d ${2} ]
then
DIR2=”${2}”
PTH2=$(cd $DIR2 && echo `pwd`)
else
DIR2=”${2}”
mkdir -p $2 || (echo “${2} is not a directory & cant be created” && return 1)
fi
ls -1 $DIR1 | awk “{ system("set -x; ln -s $DIR1/" \$1 " $DIR2/" \$1)} “
set +x
return 0
}