462 lines
12 KiB
Plaintext
462 lines
12 KiB
Plaintext
################################################################################
|
|
## Shell -- Functions.
|
|
## Date 2012-04-23
|
|
################################################################################
|
|
|
|
|
|
##==============================================================================
|
|
## Man Pager
|
|
##==============================================================================
|
|
man()
|
|
{
|
|
env \
|
|
LESS_TERMCAP_mb=$(printf "\e[1;31m") \
|
|
LESS_TERMCAP_md=$(printf "\e[1;31m") \
|
|
LESS_TERMCAP_me=$(printf "\e[0m") \
|
|
LESS_TERMCAP_se=$(printf "\e[0m") \
|
|
LESS_TERMCAP_so=$(printf "\e[1;44;33m") \
|
|
LESS_TERMCAP_ue=$(printf "\e[0m") \
|
|
LESS_TERMCAP_us=$(printf "\e[1;32m") \
|
|
man "$@"
|
|
}
|
|
|
|
##==============================================================================
|
|
## Arch Linux Functions
|
|
##==============================================================================
|
|
|
|
|
|
## Compare installed packages with list.
|
|
# Parameters:
|
|
# -m : compare only to foreign local (m)
|
|
# -q : see installed packages. Display only not installed packages by default. (\<)
|
|
# FILE1 [FILE2...] : file(s) containing package list.
|
|
pacman-diff()
|
|
{
|
|
OPTION_FOREIGN=""
|
|
OPTION_LOCAL=">"
|
|
|
|
pdhelp()
|
|
{
|
|
echo "Synopsis:"
|
|
echo -e " $1 [-m|-e] [-q] FILE"
|
|
echo
|
|
echo "Usage:"
|
|
echo -e " default:\tDisplay packages included in FILE but not installed."
|
|
echo -e " -e:\t\tCompare FILE to explicitly installed packages."
|
|
echo -e " -h:\t\tDisplay this help."
|
|
echo -e " -m:\t\tCompare FILE to foreign installed packages."
|
|
echo -e " -q:\t\tDisplay installed packages not included in FILE."
|
|
}
|
|
|
|
OPTION_EXPLICIT=""
|
|
OPTION_FOREIGN=""
|
|
OPTION_LOCAL=">"
|
|
|
|
while getopts ":hmqe" opt; do
|
|
case $opt in
|
|
e)
|
|
OPTION_EXPLICIT="e"
|
|
;;
|
|
h)
|
|
pdhelp "$0"
|
|
return 1
|
|
;;
|
|
m)
|
|
OPTION_FOREIGN="m"
|
|
;;
|
|
q)
|
|
OPTION_LOCAL="<"
|
|
;;
|
|
?)
|
|
pdhelp "$0"
|
|
return 1
|
|
;;
|
|
:)
|
|
echo "Missing argument."
|
|
echo "Use $0 -h for help."
|
|
return 1
|
|
;;
|
|
esac
|
|
done
|
|
|
|
shift $(($OPTIND - 1))
|
|
|
|
if [ $# -eq 0 ]; then
|
|
echo "Missing argument."
|
|
echo "Use $0 -h for help."
|
|
return 1
|
|
fi
|
|
|
|
if [ -e "/usr/bin/pacman" ]; then
|
|
diff <(pacman -Qq${OPTION_FOREIGN}${OPTION_EXPLICIT} | sort) <(cat "$*" | sort) | grep "${OPTION_LOCAL}" | cut -f2 -d" "
|
|
else
|
|
echo "ERROR: pacman is needed."
|
|
fi
|
|
}
|
|
|
|
## pacman-deps
|
|
# Parameters:
|
|
# PACKAGE: if found locally, everything is checked
|
|
# locally. Otherwise, everything is checked from the pacman index, which is much
|
|
# slower.
|
|
if [ -e "/usr/bin/pacman" ]; then
|
|
pacman-deps()
|
|
{
|
|
## Arguments
|
|
if [ $# -ne 1 ];then
|
|
echo "Requires only one single argument."
|
|
return
|
|
fi
|
|
|
|
## Bold High Intensty colors
|
|
BIBlack='\e[1;90m' # Black
|
|
BIRed='\e[1;91m' # Red
|
|
BIGreen='\e[1;92m' # Green
|
|
BIYellow='\e[1;93m' # Yellow
|
|
BIBlue='\e[1;94m' # Blue
|
|
BIPurple='\e[1;95m' # Purple
|
|
BICyan='\e[1;96m' # Cyan
|
|
BIWhite='\e[1;97m' # White
|
|
CReset='\e[0m' # Text Reset
|
|
ARROW="${BIBlue}==>${CReset}"
|
|
|
|
CMD="pacman -Qi"
|
|
|
|
APP_OUTPUT=$(eval "${CMD} $1" 2>&1)
|
|
if [ ! -z "$(echo "$APP_OUTPUT" | grep "^error:")" ];then
|
|
CMD="pacman -Si"
|
|
APP_OUTPUT=$(eval "${CMD} $1" 2>&1)
|
|
|
|
# Make sure package is available online.
|
|
if [ ! -z "$(echo "$APP_OUTPUT" | grep "^error:")" ];then
|
|
echo "Package not found!"
|
|
return
|
|
fi
|
|
fi
|
|
|
|
## List of dependencies. Note that it is stored as an ARRAY!
|
|
DEPLIST=($( echo "$APP_OUTPUT" | grep "Depends"| sed -e 's/Depends On.*: //g' | sed -e 's/[>=<][^ ]*//g'))
|
|
|
|
DEPLIST_INFO="$(eval "$CMD ${DEPLIST[*]}" 2>&1)"
|
|
SKIPPED_DEPS="$(echo "$DEPLIST_INFO" | awk '/^error:/ {print $3}')"
|
|
SIZE_ARRAY="$(echo "$DEPLIST_INFO" | awk '/^Installed Size/ {print $4}' | sed -e 's/\.[[:digit:]]*//g')"
|
|
|
|
echo -e "${ARROW} ${BIGreen}Deps size for ${BIYellow}$1${CReset}"
|
|
echo "$DEPLIST_INFO" | awk '/^Name/ {pkg=$3} /^Installed Size/ {print $4 " " $5,pkg}' | sort -n
|
|
|
|
TOTAL_SIZE=0
|
|
for i in $(echo "$SIZE_ARRAY"); do
|
|
TOTAL_SIZE=$(($TOTAL_SIZE+$i))
|
|
done
|
|
|
|
echo -e "${ARROW} ${BIGreen}Summary${CReset}"
|
|
echo "$APP_OUTPUT"
|
|
|
|
echo -e "${BIRed}Deps size${CReset} : $(echo "${TOTAL_SIZE}") KiB"
|
|
TOTAL_SIZE=$(($TOTAL_SIZE+$(echo "$APP_OUTPUT" | awk '/^Installed Size/ {print $4}'| sed -e 's/\.[[:digit:]]*//g')))
|
|
echo -e "${BIRed}Total size${CReset} : $(echo "${TOTAL_SIZE}") KiB"
|
|
|
|
if [ ! -z "$SKIPPED_DEPS" ]; then
|
|
echo -e "$ARROW${BIPurple} Warning!${CReset} Errors were ecountered on"
|
|
echo -e "$SKIPPED_DEPS"
|
|
fi
|
|
}
|
|
|
|
## TODO: completion does not work as is. It requires pacman arguments like -S or -Q.
|
|
## There should be a way to use it.
|
|
# if isShell "zsh" ; then
|
|
# compdef _pacman pacman-deps
|
|
# elif isShell "bash"; then
|
|
# complete -o default -o nospace -F _pacman pacman-deps
|
|
# fi
|
|
fi
|
|
|
|
##==============================================================================
|
|
## LaTeX functions
|
|
##==============================================================================
|
|
|
|
# Warning: use this function with caution. It may drastically improve
|
|
# compression of some PDF files, but in some case, the output filesize will be
|
|
# greater! You should not use it over PDF files embedding pictures as well.
|
|
|
|
pdfcompress ()
|
|
{
|
|
if [ $# -lt 1 -o $# -gt 2 ]; then
|
|
echo "Usage: pdfcompress PDFFILE [DESTFILE]"
|
|
return
|
|
fi
|
|
|
|
if [ ! -f "$1" ]; then
|
|
echo "$1 is not a valid PDF file!"
|
|
fi
|
|
INPUTFILE="$1"
|
|
|
|
if [ -z "$2" ]; then
|
|
gs -dBATCH -dNOPAUSE -q -sDEVICE=pdfwrite -sOutputFile="${INPUTFILE%%.*}-COMPRESSED.pdf" "${INPUTFILE}"
|
|
rm -rf "${INPUTFILE}"
|
|
mv "${INPUTFILE%%.*}-COMPRESSED.pdf" "${INPUTFILE}"
|
|
else
|
|
OUTPUTFILE="$2"
|
|
gs -dBATCH -dNOPAUSE -q -sDEVICE=pdfwrite -sOutputFile="${OUTPUTFILE}" "${INPUTFILE}"
|
|
fi
|
|
}
|
|
|
|
# This function will clean TeX/LaTeX project folders recursively.
|
|
function texclean ()
|
|
{
|
|
if [ -z "$1" ]; then
|
|
WORKDIR="$PWD"
|
|
else
|
|
WORKDIR="$1"
|
|
fi
|
|
|
|
echo "$(find "$WORKDIR" \
|
|
-name "*.aux" -o \
|
|
-name "*.idx" -o \
|
|
-name "*.ilg" -o \
|
|
-name "*.ind" -o \
|
|
-name "*.lof" -o \
|
|
-name "*.log" -o \
|
|
-name "*.nav" -o \
|
|
-name "*.out" -o \
|
|
-name "*.snm" -o \
|
|
-name "*.tns" -o \
|
|
-name "*.toc")"
|
|
|
|
rm -f $(find "$WORKDIR" \
|
|
-name "*.aux" -o \
|
|
-name "*.idx" -o \
|
|
-name "*.ilg" -o \
|
|
-name "*.ind" -o \
|
|
-name "*.lof" -o \
|
|
-name "*.log" -o \
|
|
-name "*.nav" -o \
|
|
-name "*.out" -o \
|
|
-name "*.snm" -o \
|
|
-name "*.tns" -o \
|
|
-name "*.toc")
|
|
}
|
|
|
|
|
|
|
|
##==============================================================================
|
|
## Project Archiving
|
|
##==============================================================================
|
|
|
|
archive-project ()
|
|
{
|
|
if [ $# -eq 1 ]; then
|
|
ARCPATH="$(dirname $(readlink -f "$1"))"
|
|
ARCSOURCE="$(basename $(readlink -f "$1"))"
|
|
ARCNAME="${ARCSOURCE}-$(date +%y-%m-%d-%H%M%S).tgz"
|
|
|
|
(cd "$ARCPATH" && \
|
|
tar cvzf "$ARCNAME" "$ARCSOURCE" --exclude-vcs)
|
|
|
|
else
|
|
ARCSOURCE="$(basename $PWD)"
|
|
ARCNAME="$(basename "$ARCSOURCE")-$(date +%y-%m-%d-%H%M%S).tgz"
|
|
|
|
if [ $# = 0 ];then
|
|
|
|
(cd "$PWD/.." && \
|
|
tar cvzf "$ARCNAME" "$ARCSOURCE" --exclude-vcs)
|
|
|
|
else
|
|
unset FILELIST
|
|
|
|
for i;do
|
|
FILELIST+="$i "
|
|
done
|
|
|
|
# FIXME: Does not work with zsh
|
|
tar cvzf "$ARCNAME" ${FILELIST} --exclude-vcs
|
|
|
|
fi
|
|
|
|
fi
|
|
}
|
|
|
|
|
|
##==============================================================================
|
|
## Network operations
|
|
##==============================================================================
|
|
|
|
if [ -e "/usr/bin/nmap" ]; then
|
|
function networkmap() {
|
|
echo "$1"
|
|
for SCANIP in $( nmap -sL "$1" | grep -i "\([[:digit:]]\+\.\)\{3,\}[[:digit:]]\+" | awk '{print $5}' ); do
|
|
sudo nmap -oX - -n -sT -T Normal -O -sR -I -F -P0 "$SCANIP"
|
|
echo
|
|
done
|
|
}
|
|
fi
|
|
|
|
##==============================================================================
|
|
## File & strings related functions
|
|
##==============================================================================
|
|
|
|
## Search
|
|
function search()
|
|
{
|
|
find . -name "*$1*"
|
|
}
|
|
|
|
## Find a file with a pattern in name:
|
|
function ff()
|
|
{ find . -type f -iname '*'$*'*' -ls ; }
|
|
|
|
## Find a file with pattern $1 in name and Execute $2 on it:
|
|
fe()
|
|
{
|
|
if [ $# -lt 2 ]; then
|
|
find . -name "*$1*" -exec $2 $3 {} \;
|
|
fi
|
|
}
|
|
# { find . -type f -iname '*'$1'*' -exec "${2:-file}" {} \; ; }
|
|
|
|
## Clean current folder
|
|
cleanvcs()
|
|
{
|
|
unset CHOICE
|
|
echo "This will clean current folder from all VCS control files."
|
|
echo -n "Proceed ? [y/N] "
|
|
while [ -z "$CHOICE" ];
|
|
do
|
|
# read 'Proceed [y/N] ' CHOICE
|
|
read CHOICE
|
|
done
|
|
|
|
CHOICE=$(echo $CHOICE | tr '[:upper:]' '[:lower:]')
|
|
if [ "$CHOICE" = "y" ]; then
|
|
find . -type d -name ".svn" -exec rm -rf {} \;
|
|
echo "VCS files cleaned."
|
|
fi
|
|
}
|
|
|
|
|
|
## Cut last n lines in file, 10 by default.
|
|
function cuttail()
|
|
{
|
|
nlines=${2:-10}
|
|
#sed -n -e :a -e "1,${nlines}!{P;N;D;};N;ba" $1
|
|
head $1 -n$(($(cat $1|wc -l) - $nlines))
|
|
}
|
|
|
|
## Cut first n lines in file, 10 by default.
|
|
function cuthead()
|
|
{
|
|
nlines=${2:-10}
|
|
tail $1 -n$(($(cat $1|wc -l) - $nlines))
|
|
}
|
|
|
|
## Move filenames to lowercase.
|
|
## Use ranger + bulkrename + vim/emacs (+ TwindleCase) for more interactivity.
|
|
function renamelower()
|
|
{
|
|
for file ; do
|
|
filename=${file##*/}
|
|
case "$filename" in
|
|
*/*)
|
|
dirname==${file%/*} ;;
|
|
*)
|
|
dirname=.;;
|
|
esac
|
|
|
|
nf=$(echo $filename | tr A-Z a-z)
|
|
newname="${dirname}/${nf}"
|
|
if [ "$nf" != "$filename" ]; then
|
|
mv "$file" "$newname"
|
|
echo "lowercase: $file --> $newname"
|
|
else
|
|
echo "lowercase: $file not changed."
|
|
fi
|
|
done
|
|
}
|
|
|
|
## Swap 2 filenames around.
|
|
function renameswap()
|
|
{
|
|
local TMPFILE=tmp.$$
|
|
mv "$1" $TMPFILE
|
|
mv "$2" "$1"
|
|
mv $TMPFILE "$2"
|
|
}
|
|
|
|
## Finds directory sizes and lists them for the current directory.
|
|
dirsize ()
|
|
{
|
|
du -shx * .[a-zA-Z0-9_]* 2> /dev/null | \
|
|
egrep '^ *[0-9.]*[MG]' | sort -n > /tmp/list
|
|
egrep '^ *[0-9.]*M' /tmp/list
|
|
egrep '^ *[0-9.]*G' /tmp/list
|
|
rm -rf /tmp/list
|
|
}
|
|
|
|
|
|
## Make dir and go into it.
|
|
mkcd () {
|
|
mkdir -p "$*"
|
|
cd "$*"
|
|
}
|
|
|
|
|
|
## Vim: search the vim reference manual for a keyword
|
|
## usage: :h <keyword>
|
|
if [ -e "/usr/bin/vim" ]; then
|
|
:h() { vim --cmd ":silent help $@" --cmd "only"; }
|
|
fi
|
|
|
|
## sanitize - set file/directory owner and permissions to normal values (644/755)
|
|
## usage: sanitize <file>
|
|
sanitize()
|
|
{
|
|
chmod -R u=rwX,go=rX "$@"
|
|
chown -R ${USER}:users "$@"
|
|
}
|
|
|
|
## sanitize - set file/directory owner and permissions to normal values (644/755)
|
|
## usage: sanitize <file>
|
|
asciify()
|
|
{
|
|
asciify_help()
|
|
{
|
|
echo
|
|
echo "Synopsis:"
|
|
echo -e "\tConvert non-ASCII characters to their ASCII equivalent."
|
|
echo
|
|
echo "Usage:"
|
|
echo -e "\t$1 FILES"
|
|
echo
|
|
}
|
|
|
|
if [ $# -eq 0 ]; then
|
|
echo "Missing arguments."
|
|
asciify_help $0
|
|
return
|
|
fi
|
|
|
|
for i; do
|
|
sed -i 's/[àáâä]/a/g' "$i"
|
|
sed -i 's/[éèêë]/e/g' "$i"
|
|
sed -i 's/[ïîí]/i/g' "$i"
|
|
sed -i 's/[öôó]/o/g' "$i"
|
|
sed -i 's/[ùúûü]/u/g' "$i"
|
|
sed -i 's/[À]/A/g' "$i"
|
|
sed -i 's/[ÉÈ]/E/g' "$i"
|
|
sed -i 's/[ñ]/n/g' "$i"
|
|
sed -i 's/[œ]/oe/g' "$i"
|
|
sed -i 's/[Œ]/Oe/g' "$i"
|
|
sed -i 's/[æ]/ae/g' "$i"
|
|
sed -i 's/[Æ]/Ae/g' "$i"
|
|
sed -i 's/[ç]/c/g' "$i"
|
|
sed -i 's/[Ç]/C/g' "$i"
|
|
done;
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|