Shell/Scripts: moved all useful functions (for whom it makes sense to call them

from Emacs or Ranger) to separate scripts.
master
Pierre Neidhardt 2013-03-03 11:19:50 +01:00
parent 524aaf9042
commit ea81531077
24 changed files with 498 additions and 542 deletions

View File

@ -39,7 +39,7 @@ while getopts ":hfy" opt; do
;;
:)
echo "Missing argument."
echo "Use $0 -h for help."
_printhelp "$0"
exit 1
;;
esac
@ -49,7 +49,7 @@ shift $(($OPTIND - 1))
if [ $# -eq 0 ]; then
echo "Missing argument."
echo "Use $0 -h for help."
_printhelp "$0"
exit 1
fi

96
.scripts/archive Executable file
View File

@ -0,0 +1,96 @@
#!/usr/bin/zsh
## We need zsh because of array use here.
_printhelp()
{
cat <<EOF
Usage: ${1##*/} [-m METHOD] [-v] FILES|FOLDERS
Create an archive from a single or multiples files/folders.
-h: Display this help.
-m: Choose compression method.
* gz: gzip (default).
* xz: LZMA.
-v: Exclude VCS data. (GNU tar only.)
EOF
}
OPTION_VCS=""
OPTION_METHOD=""
while getopts ":hm:v" opt; do
case $opt in
h)
_printhelp "$0"
exit 1
;;
m)
OPTION_METHOD="${OPTARG}"
;;
v)
OPTION_VCS="--exclude-vcs"
;;
?)
_printhelp "$0"
exit 1
;;
:)
echo "Missing argument."
_printhelp "$0"
exit 1
;;
esac
done
ARCEXT=""
ARCOPT=""
case $OPTION_METHOD in
"gz")
ARCEXT="tar.gz"
ARCOPT="z"
;;
"xz")
ARCEXT="tar.xz"
ARCOPT="J"
;;
*)
ARCEXT="tar.gz"
ARCOPT="z"
;;
esac
shift $(($OPTIND - 1))
ARCPATH=""
ARCSOURCE=""
ARCNAME=""
if [ $# -eq 1 ]; then
ARCPATH="$(dirname $(readlink -f "$1"))"
ARCSOURCE="$(basename $(readlink -f "$1"))"
ARCNAME="${ARCSOURCE}-$(date +%y-%m-%d-%H%M%S).${ARCEXT}"
(cd "$ARCPATH" && \
tar -${ARCOPT} -cf "$ARCNAME" "$ARCSOURCE" ${OPTION_VCS})
else
ARCSOURCE="$(basename $PWD)"
ARCNAME="$(basename "$ARCSOURCE")-$(date +%y-%m-%d-%H%M%S).${ARCEXT}"
if [ $# = 0 ];then
(cd "$PWD/.." && \
tar -${ARCOPT} -cf "$ARCNAME" "$ARCSOURCE" ${OPTION_VCS})
else
## Note for zsh: do NOT initialize an array with "myarray=()".
for i; do
FILELIST=(${FILELIST[*]} "$i")
done
tar -${ARCOPT} -cf "$ARCNAME" ${FILELIST[*]} ${OPTION_VCS}
fi
fi

64
.scripts/asciify Executable file
View File

@ -0,0 +1,64 @@
#!/bin/sh
if [ $# -eq 0 ] || [ "$1" = "-h" ]; then
cat <<EOF
Usage: ${0##*/} FILES
Convert non-ASCII characters to their ASCII equivalent.
EOF
exit
fi
for i; do
sed -i \
-e 's/[áàâä]/a/g' \
-e 's/[éèêë]/e/g' \
-e 's/[íìîï]/i/g' \
-e 's/[óòôö]/o/g' \
-e 's/[úùûü]/u/g' \
-e 's/[ýỳŷÿ]/y/g' \
-e 's/[ÁÀÂÄ]/A/g' \
-e 's/[ÉÈÊË]/E/g' \
-e 's/[ÍÌÎÏ]/I/g' \
-e 's/[ÓÒÔÖ]/O/g' \
-e 's/[ÚÙÛÜ]/U/g' \
-e 's/[ÝỲŶŸ]/Y/g' \
-e 's/[ñ]/n/g' \
-e 's/[œ]/oe/g' \
-e 's/[Œ]/Oe/g' \
-e 's/[æ]/ae/g' \
-e 's/[Æ]/Ae/g' \
-e 's/[ç]/c/g' \
-e 's/[Ç]/C/g' \
-e 's/[ß]/ss/g' \
-e 's/[«»„“”‚‘’]/"/g' \
-e 's/[©]/(C)/g' \
-e 's/[®]/(R)/g' \
-e 's/[™]/(TM)/g' \
-e 's/[¥]/Y/g' \
-e 's/[Ð]/D/g' \
-e 's/[ŀ]/l/g' \
-e 's/[Ŀ]/L/g' \
-e 's/[€]/euro/g' \
-e 's/[¢]/cent/g' \
-e 's/[£]/pound/g' \
-e 's/[µ]/mu/g' \
-e 's/[²]/^2/g' \
-e 's/[³]/^3/g' \
-e 's/[¡]/!/g' \
-e 's/[¿]/?/g' \
-e 's/[]/-/g' \
-e 's/[…]/.../g' \
-e 's/[≤]/<=/g' \
-e 's/[≥]/>=/g' \
-e 's/[±]/+\/-/g' \
-e 's/[≠]/!=/g' \
-e 's/[⋅]/./g' \
-e 's/[×]/x/g' \
-e 's/[÷]/\//g' \
-e 's/[↓]/|/g' \
-e 's/[↑]/^/g' \
-e 's/[←]/<=/g' \
-e 's/[→]/=>/g' \
"$i"
done;

41
.scripts/blind-append Executable file
View File

@ -0,0 +1,41 @@
#!/usr/bin/env zsh
if [ $# -gt 2 ] || [ $# -lt 1 ] || [ "$1" = "-h" ]; then
cat <<EOF
Usage: ${1##*/} FILE [STRING]
Append to all STRING found in FILE a secret phrase being prompted. If STRING is
omitted, secret phrase will be appended to the end of the file. If FILE does
not exist, it will be created and secret phrase will be inserted. STRING will be
ignored.
This requires 'read -s'. It will not work for Bourne Shell.
EOF
exit
fi
FILE="$1"
STRING=""
DUMMY=""
if [ $# -eq 2 ]; then
STRING="$2"
fi
echo -n "Secret: "
read -s DUMMY
echo ""
if [ ! -e "$FILE" ] || [ -z "$STRING" ]; then
echo "$DUMMY" >> "$FILE"
echo "Secret appended to ${FILE} at the end."
exit
fi
if [ $# -eq 1 ]; then
echo "$DUMMY" >> "$FILE"
else
sed -i "s/${STRING}/${STRING}${DUMMY}/g" "${FILE}"
fi
echo "Secret appended to ${FILE}."

32
.scripts/crun Executable file
View File

@ -0,0 +1,32 @@
#!/bin/sh
if [ $# -lt 1 ]; then
echo "Usage: ${0##*/} FILE [GCC_OPTS]"
exit
fi
INPUT="$1"
shift
GCC_OPTS="-O0 -Wall -Wextra -Wshadow -pthread"
if [ $# -ne 0 ]; then
GCC_OPTS="$@"
fi
FILE=$(mktemp)
echo "==> gcc \"$INPUT\" -o \"$FILE\" $GCC_OPTS"
## Zsh compatibility. We need it otherwise word splitting of parameter GCC_OPTS
## will not work.
STATUS="$(set -o | grep 'shwordsplit' | awk '{print $2}')"
[ "$STATUS" = "off" ] && set -o shwordsplit
gcc "$INPUT" -o "$FILE" $GCC_OPTS
## Restore Zsh previous options. This will not turn off shwordsplit if it
## was on before calling the function.
[ "$STATUS" = "off" ] && set +o shwordsplit
echo "==> $FILE"
"$FILE"
rm "$FILE"

3
.scripts/ediff Executable file
View File

@ -0,0 +1,3 @@
#!/bin/sh
emacs -q -l ~/.emacs-light --eval "(ediff \"$1\" \"$2\")"

26
.scripts/extract Executable file
View File

@ -0,0 +1,26 @@
#!/bin/sh
[ -n "$(command -v atool)" ] && echo "You should use atool instead."
if [ -f "$1" ] ; then
case "$1" in
*.tar.bz2) tar xvjf "$1" ;;
*.tar.gz) tar xvzf "$1" ;;
*.tar.xz) tar xvJf "$1" ;;
*.bz2) bunzip2 "$1" ;;
*.rar) unrar x "$1" ;;
*.gz) gunzip "$1" ;;
*.tar) tar xvf "$1" ;;
*.tbz2) tar xvjf "$1" ;;
*.tgz) tar xvzf "$1" ;;
*.txz) tar xvJf "$1" ;;
*.zip) unzip "$1" ;;
*.Z) uncompress "$1" ;;
*.7z) 7z x "$1" ;;
*.xz) unxz "$1" ;;
*.exe) cabextract "$1" ;;
*) echo "[$1]: unrecognized file compression" ;;
esac
else
echo "[$1] is not a valid file"
fi

38
.scripts/formatc Executable file
View File

@ -0,0 +1,38 @@
#!/bin/sh
## Format C using 'indent'. Alternative to indent: astyle, uncrustify.
if [ -z "$(command -v indent)" ]; then
echo "Please install 'indent'"
exit
fi
_formatc_dir()
{
find "$1" -type f \
-name "*.[ch]" \
-print \
-exec indent -i4 -ppi4 -bli0 -cli4 -nut {} \;
## Remove backup files.
find "$1" -type f \( \
-name "*.[ch]~" -o \
-name "*.[ch]~[0-9]*~" \) \
-delete
}
if [ $# -eq 0 ]; then
echo "Working on current dir"
_formatc_dir "$PWD"
exit
fi
for i ; do
echo "[$i]"
if [ -d "$i" ]; then
_formatc_dir "$i"
else
indent -i4 -ppi4 -bli0 -cli4 -nut "$i"
rm -f "$i~" "$i~[0-9]*~"
fi
done

9
.scripts/git-check Executable file
View File

@ -0,0 +1,9 @@
#!/bin/sh
while IFS= read -r FOLDER; do
if [ -z "$(cd "${FOLDER%/*}" && git status -uno | grep "nothing to commit")" ]; then
echo "${FOLDER%/*}"
fi
done <<EOF
$(find . -type d -name ".git")
EOF

16
.scripts/git-compare Executable file
View File

@ -0,0 +1,16 @@
#!/bin/sh
if [ $# -ne 1 ]; then
echo "Usage: ${0##*/} FOLDER."
echo "Compare current git repo with FOLDER."
exit
fi
REPO="$(mktemp)"
FOLDER="$(mktemp)"
git ls-files | sort > "$REPO"
find "$1" -type f ! -path "*.git/*" | sed 's/^[^\/]*\///g' | sort > "$FOLDER"
rm -f "$REPO" "$FOLDER"
## Zsh version.
# comm -3 <(git ls-files | sort) <(find "$1" -type f ! -path "*.git/*" | sed 's/^[^\/]*\///g' | sort)

16
.scripts/ltx Executable file
View File

@ -0,0 +1,16 @@
#!/bin/sh
if [ $# -ne 1 ]; then
echo "Usage: ${0##*/} FILE"
echo "LaTeX quick compiler. It adds the preambule -- and the
\end{document} -- automatically."
exit
fi
## One line is mandatory.
PREAMBLE='\documentclass[10pt,a4paper]{article}\usepackage[utf8]{inputenc}\usepackage[T1]{fontenc}\usepackage{amsmath,amssymb,amsfonts}\usepackage{geometry}\usepackage{lmodern}\usepackage{marvosym}\usepackage{textcomp}\DeclareUnicodeCharacter{20AC}{\EUR{}}\DeclareUnicodeCharacter{2264}{\leqslant}\DeclareUnicodeCharacter{2265}{\geqslant}\begin{document}\input'
END='\end{document}'
pdflatex -file-line-error-style -interaction nonstopmode -jobname="${1%.tex}" "$PREAMBLE" "$1" "$END"

View File

@ -50,7 +50,7 @@ shift $(($OPTIND - 1))
if [ $# -eq 0 ]; then
echo "Missing argument."
echo "Use $0 -h for help."
_printhelp "$0"
exit 1
fi

26
.scripts/pdfcompress Executable file
View File

@ -0,0 +1,26 @@
#!/bin/sh
if [ $# -lt 1 ] || [ $# -gt 2 ] || [ "$1" = "-h" ]; then
cat <<EOF
Usage: ${0##*/} PDFFILE [DESTFILE]
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.
EOF
exit
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

13
.scripts/pdfextract Executable file
View File

@ -0,0 +1,13 @@
#!/bin/sh
if [ $# -ne 4 ]; then
echo "Usage: ${0##*/} PDFFILE FIRSTPAGE LASTPAGE DESTFILE"
echo "Extract a range of pages from a PDF."
exit
fi
if [ ! -f "$1" ]; then
echo "$1 is not a valid PDF file!"
fi
gs -sDEVICE=pdfwrite -dNOPAUSE -dSAFER -dSAFER -dFirstPage=$2 -dBATCH -dLastPage=$3 -sOutputFile="$4" "$1"

10
.scripts/pdfresize Executable file
View File

@ -0,0 +1,10 @@
#!/bin/sh
if [ $# -ne 2 ]; then
echo "Usage: ${0##*/} INPUT OUTPUT"
echo "Resize input PDF to an A4 output PDF."
exit
fi
gs -q -o "$2" -sDEVICE=pdfwrite -sPAPERSIZE=a4 -dFIXEDMEDIA -dPDFFitPage -dCompatibilityLevel=1.4 "$1"

View File

@ -33,7 +33,7 @@
## Variables
OLDPATH="$(pwd)"
cd "$HOME"
SCRIPTPATH="$(readlink -f $(dirname "$0"))"
SCRIPTPATH="${0##*/}"
HOST=$(hostname)
## Arch Linux

15
.scripts/sanitize Executable file
View File

@ -0,0 +1,15 @@
#!/bin/sh
## Set file/directory owner and permissions.
## Usage: sanitize [FOLDER]
WORKDIR="$PWD"
if [ $# -eq 1 ]; then
WORKDIR="$1"
fi
FMASK=$(umask -S | sed 's/x//g')
DMASK=$(umask -S)
find "$WORKDIR" -exec chown -R ${UID}:${GID} {} \;
find "$WORKDIR" -type d -exec chmod ${DMASK} {} \;
find "$WORKDIR" -type f -exec chmod ${FMASK} {} \;

View File

@ -51,7 +51,7 @@ TC_AUDIO_DEST="/media/data1/Musics/"
_printhelp ()
{
cat <<EOF
Usage: $1 [OPTIONS] FILE
Usage: ${1##*/} [OPTIONS] FILE
Options:
-p : preview (do not change file)
@ -85,10 +85,10 @@ Default output file:
Examples:
Set the 'artist' tag and reencode:
$1 -a 'Franz Lizst' file.mp3
${1##*/} -a 'Franz Lizst' file.mp3
Set 'artist' to be 'composer', and 'title' to be preceeded by 'artist', do not reencode:
$1 -s -a '\$COMPOSER' -t '\$ARTIST - \$TITLE' file.ogg
${1##*/} -s -a '\$COMPOSER' -t '\$ARTIST - \$TITLE' file.ogg
IMPORTANT: you *must* use single quotes when using variables.
@ -148,7 +148,7 @@ while getopts ":a:b:d:g:l:n:t:hps" opt; do
;;
:)
echo "Missing argument."
echo "Use $0 -h for help."
_printhelp "$0"
exit 1
;;
esac

View File

@ -60,7 +60,7 @@ fi
_printhelp()
{
cat <<EOF
Usage: $1 [OPTIONS] FILES|FOLDERS
Usage: ${1##*/} [OPTIONS] FILES|FOLDERS
Transcode FILES or files found in FOLDERS to .mkv with x264 and ogg. Output
files are the same as the original, with time appended. You can customize
@ -97,7 +97,7 @@ while getopts ":fhs" opt; do
;;
:)
echo "Missing argument."
echo "Use $0 -h for help."
_printhelp "$0"
exit 1
;;
esac

32
.scripts/texclean Executable file
View File

@ -0,0 +1,32 @@
#!/bin/sh
## This function will clean TeX/LaTeX project folders recursively.
if [ -z "$1" ]; then
WORKDIR="$PWD"
else
WORKDIR="$1"
fi
## WARNING: removing .idx files is messy in a .git folder.
find "$WORKDIR" -type f \( \
-name "*.aux" -o \
-name "*.glg" -o \
-name "*.glo" -o \
-name "*.gls" -o \
-name "*.ilg" -o \
-name "*.ind" -o \
-name "*.lof" -o \
-name "*.log" -o \
-name "*.maf" -o \
-name "*.mtc" -o \
-name "*.mtc?" -o \
-name "*.nav" -o \
-name "*.out" -o \
-name "*.snm" -o \
-name "*.synctex" -o \
-name "*.synctex.gz" -o \
-name "*.tns" -o \
-name "*.toc" -o \
-name "*.xdy" \) \
-print \
-delete

36
.scripts/vcsclean Executable file
View File

@ -0,0 +1,36 @@
#!/bin/sh
if [ -z "$1" ]; then
WORKDIR="$PWD"
else
WORKDIR="$1"
fi
unset CHOICE
echo "This will clean current folder from all VCS control files."
echo -n "Proceed ? [y/N] "
read CHOICE
CHOICE=$(echo $CHOICE | tr '[:upper:]' '[:lower:]')
if [ "$CHOICE" = "y" ]; then
## First clean the folders.
find "$WORKDIR" \( \
-path "*/.git/*" -o \
-path "*/.bzr/*" -o \
-path "*/.cvs/*" -o \
-path "*/.hg/*" -o \
-path "*/.svn/*" \) \
-delete
## Remove the empty vcs folder.
find "$WORKDIR" -type d \( \
-name ".bzr" -o \
-name ".cvs" -o \
-name ".git" -o \
-name ".hg" -o \
-name ".svn" \) \
-delete
echo "VCS files cleaned."
fi

8
.scripts/wget-batch Executable file
View File

@ -0,0 +1,8 @@
#!/bin/sh
if [ $# -ne 2 ]; then
echo "Usage: ${0##*/} EXT URI"
exit
fi
wget -r -l1 --no-parent -A$1 "$2"

View File

@ -112,6 +112,7 @@ alias xres='xrandr -s $(xrandr | awk '"'"'/^ / {print $1;exit}'"'"')'
if [ -f /bin/em ]; then
alias ema='emacs -q -l ~/.emacs-light'
alias emacs-reload="emacsclient -e '(kill-emacs)' >/dev/null 2>&1; emacs --daemon"
[ -f ~/todo.org ] && alias todo='em ~/todo.org'
fi
##==============================================================================

View File

@ -1,9 +1,12 @@
## -*- mode:sh -*- #
################################################################################
## Shell -- Functions.
## Date 2012-12-05
## Date 2013-03-03
################################################################################
## Functions that can be useful outside a shell (like in Emacs or Ranger) should
## be written in stand-alone script instead.
## Colored man pager.
man()
{
@ -171,18 +174,6 @@ cpuusage()
fi
}
## Wget batch DL
wget_batch ()
{
if [ $# -ne 2 ]; then
echo "Usage: $0 <ext> <uri>"
return
fi
wget -r -l1 --no-parent -A$1 $2
}
## Webcam
webcam ()
{
@ -190,7 +181,6 @@ webcam ()
mplayer -fps 30 -tv driver=v4l2:width=640:height=480:device=/dev/video0 tv:// -flip
}
## Vim-only: search the vim reference manual for a keyword.
## Usage: :h <keyword>
if [ -n "$(command -v vim)" ]; then
@ -200,145 +190,6 @@ if [ -n "$(command -v vim)" ]; then
}
fi
## Set file/directory owner and permissions.
## Usage: sanitize [FOLDER]
sanitize()
{
local WORKDIR="$PWD"
if [ $# -eq 1 ]; then
WORKDIR="$1"
fi
local FMASK=$(umask -S | sed 's/x//g')
local DMASK=$(umask -S)
find "$WORKDIR" -exec chown -R ${UID}:${GID} {} \;
find "$WORKDIR" -type d -exec chmod ${DMASK} {} \;
find "$WORKDIR" -type f -exec chmod ${FMASK} {} \;
}
asciify()
{
_printhelp()
{
cat <<EOF
Usage: $1 FILES
Convert non-ASCII characters to their ASCII equivalent.
EOF
}
if [ $# -eq 0 ]; then
echo "Missing arguments."
_printhelp $0
return
fi
for i; do
sed -i \
-e 's/[áàâä]/a/g' \
-e 's/[éèêë]/e/g' \
-e 's/[íìîï]/i/g' \
-e 's/[óòôö]/o/g' \
-e 's/[úùûü]/u/g' \
-e 's/[ýỳŷÿ]/y/g' \
-e 's/[ÁÀÂÄ]/A/g' \
-e 's/[ÉÈÊË]/E/g' \
-e 's/[ÍÌÎÏ]/I/g' \
-e 's/[ÓÒÔÖ]/O/g' \
-e 's/[ÚÙÛÜ]/U/g' \
-e 's/[ÝỲŶŸ]/Y/g' \
-e 's/[ñ]/n/g' \
-e 's/[œ]/oe/g' \
-e 's/[Œ]/Oe/g' \
-e 's/[æ]/ae/g' \
-e 's/[Æ]/Ae/g' \
-e 's/[ç]/c/g' \
-e 's/[Ç]/C/g' \
-e 's/[ß]/ss/g' \
-e 's/[«»„“”‚‘’]/"/g' \
-e 's/[©]/(C)/g' \
-e 's/[®]/(R)/g' \
-e 's/[™]/(TM)/g' \
-e 's/[¥]/Y/g' \
-e 's/[Ð]/D/g' \
-e 's/[ŀ]/l/g' \
-e 's/[Ŀ]/L/g' \
-e 's/[€]/euro/g' \
-e 's/[¢]/cent/g' \
-e 's/[£]/pound/g' \
-e 's/[µ]/mu/g' \
-e 's/[²]/^2/g' \
-e 's/[³]/^3/g' \
-e 's/[¡]/!/g' \
-e 's/[¿]/?/g' \
-e 's/[]/-/g' \
-e 's/[…]/.../g' \
-e 's/[≤]/<=/g' \
-e 's/[≥]/>=/g' \
-e 's/[±]/+\/-/g' \
-e 's/[≠]/!=/g' \
-e 's/[⋅]/./g' \
-e 's/[×]/x/g' \
-e 's/[÷]/\//g' \
-e 's/[↓]/|/g' \
-e 's/[↑]/^/g' \
-e 's/[←]/<=/g' \
-e 's/[→]/=>/g' \
"$i"
done;
}
blind-append()
{
_printhelp()
{
cat <<EOF
Usage: $1 FILE [STRING]
Append to all STRING found in FILE a secret phrase being prompted. If STRING is
omitted, secret phrase will be appended to the end of the file. If FILE does
not exist, it will be created and secret phrase will be inserted. STRING will be
ignored.
This requires 'read -s'. It will not work for Bourne Shell.
EOF
}
if [ $# -gt 2 ] || [ $# -lt 1 ]; then
echo "Wrong number of arguments."
_printhelp $0
return
fi
local FILE="$1"
local STRING=""
local DUMMY
if [ $# -eq 2 ]; then
STRING="$2"
fi
echo -n "Secret: "
read -s DUMMY
echo ""
if [ ! -e "$FILE" ] || [ -z "$STRING" ]; then
echo "$DUMMY" >> "$FILE"
echo "Secret appended to ${FILE} at the end."
return
fi
if [ $# -eq 1 ]; then
echo "$DUMMY" >> "$FILE"
else
sed -i "s/${STRING}/${STRING}${DUMMY}/g" "${FILE}"
fi
echo "Secret appended to ${FILE}."
return
}
## Term properties
termcolors256()
{
@ -432,269 +283,6 @@ EOF
}
fi
## Extractor -- Useless when using 'atool'.
if [ ! -n "$(command -v atool)" ]; then
extract ()
{
if [ -f $1 ] ; then
case $1 in
*.tar.bz2) tar xvjf $1 ;;
*.tar.gz) tar xvzf $1 ;;
*.tar.xz) tar xvJf $1 ;;
*.bz2) bunzip2 $1 ;;
*.rar) unrar x $1 ;;
*.gz) gunzip $1 ;;
*.tar) tar xvf $1 ;;
*.tbz2) tar xvjf $1 ;;
*.tgz) tar xvzf $1 ;;
*.zip) unzip $1 ;;
*.Z) uncompress $1 ;;
*.7z) 7z x $1 ;;
*.xz) unxz $1 ;;
*.exe) cabextract $1 ;;
*) echo "\`$1': unrecognized file compression" ;;
esac
else
echo "\`$1' is not a valid file"
fi
}
fi
## 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
local 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
local OUTPUTFILE="$2"
gs -dBATCH -dNOPAUSE -q -sDEVICE=pdfwrite -sOutputFile="${OUTPUTFILE}" "${INPUTFILE}"
fi
}
## Extract a range of pages from a PDF.
pdfextract ()
{
if [ $# -ne 4 ]; then
echo "Usage: $0 PDFFILE FIRSTPAGE LASTPAGE DESTFILE"
return
fi
if [ ! -f "$1" ]; then
echo "$1 is not a valid PDF file!"
fi
gs -sDEVICE=pdfwrite -dNOPAUSE -dSAFER -dSAFER -dFirstPage=$2 -dBATCH -dLastPage=$3 -sOutputFile=$4 $1
}
## Resize input PDF to an A4 output PDF.
pdfresize ()
{
if [ $# -ne 2 ]; then
echo "Usage: $0 <input> <output>"
return
fi
gs -q -o $2 -sDEVICE=pdfwrite -sPAPERSIZE=a4 -dFIXEDMEDIA -dPDFFitPage -dCompatibilityLevel=1.4 $1
}
archive ()
{
_printhelp()
{
cat <<EOF
Usage: $1 [-m METHOD] [-v] FILES|FOLDERS
Create an archive from a single or multiples files/folders.
-h: Display this help.
-m: Choose compression method.
* gz: gzip (default).
* xz: LZMA.
-v: Exclude VCS data. (GNU tar only).
EOF
}
local OPTION_VCS=""
local OPTION_METHOD=""
while getopts ":hm:v" opt; do
case $opt in
h)
_printhelp "$0"
return 1
;;
m)
OPTION_METHOD="${OPTARG}"
;;
v)
OPTION_VCS="--exclude-vcs"
;;
?)
_printhelp "$0"
return 1
;;
:)
echo "Missing argument."
echo "Use $0 -h for help."
return 1
;;
esac
done
local ARCEXT
local ARCOPT
case $OPTION_METHOD in
"gz")
ARCEXT="tar.gz"
ARCOPT="z"
;;
"xz")
ARCEXT="tar.xz"
ARCOPT="J"
;;
*)
ARCEXT="tar.gz"
ARCOPT="z"
;;
esac
shift $(($OPTIND - 1))
local ARCPATH
local ARCSOURCE
local ARCNAME
if [ $# -eq 1 ]; then
ARCPATH="$(dirname $(readlink -f "$1"))"
ARCSOURCE="$(basename $(readlink -f "$1"))"
ARCNAME="${ARCSOURCE}-$(date +%y-%m-%d-%H%M%S).${ARCEXT}"
(cd "$ARCPATH" && \
tar -${ARCOPT} -cf "$ARCNAME" "$ARCSOURCE" ${OPTION_VCS})
else
ARCSOURCE="$(basename $PWD)"
ARCNAME="$(basename "$ARCSOURCE")-$(date +%y-%m-%d-%H%M%S).${ARCEXT}"
if [ $# = 0 ];then
(cd "$PWD/.." && \
tar -${ARCOPT} -cf "$ARCNAME" "$ARCSOURCE" ${OPTION_VCS})
else
## Note for zsh: do NOT initialize an array with "myarray=()".
local FILELIST
for i; do
FILELIST=(${FILELIST[*]} "$i")
done
tar -${ARCOPT} -cf "$ARCNAME" ${FILELIST[*]} ${OPTION_VCS}
fi
fi
}
## Git compare
git-compare()
{
if [ $# -ne 1 ]; then
echo "Wrong number of argument."
return
fi
comm -3 <(git ls-files | sort) <(find "$1" -type f ! -path "*.git/*" | sed 's/^[^\/]*\///g' | sort)
}
## This function will clean TeX/LaTeX project folders recursively.
texclean ()
{
if [ -z "$1" ]; then
WORKDIR="$PWD"
else
WORKDIR="$1"
fi
## WARNING: removing .idx files is messy in a .git folder.
find "$WORKDIR" -type f \( \
-name "*.aux" -o \
-name "*.glg" -o \
-name "*.glo" -o \
-name "*.gls" -o \
-name "*.ilg" -o \
-name "*.ind" -o \
-name "*.lof" -o \
-name "*.log" -o \
-name "*.maf" -o \
-name "*.mtc" -o \
-name "*.mtc?" -o \
-name "*.nav" -o \
-name "*.out" -o \
-name "*.snm" -o \
-name "*.synctex" -o \
-name "*.synctex.gz" -o \
-name "*.tns" -o \
-name "*.toc" -o \
-name "*.xdy" \) \
-print \
-delete
}
## Clean current folder
vcsclean()
{
local WORKDIR
if [ -z "$1" ]; then
WORKDIR="$PWD"
else
WORKDIR="$1"
fi
local CHOICE
unset CHOICE
echo "This will clean current folder from all VCS control files."
echo -n "Proceed ? [y/N] "
read CHOICE
CHOICE=$(echo $CHOICE | tr '[:upper:]' '[:lower:]')
if [ "$CHOICE" = "y" ]; then
## First clean the folders.
find "$WORKDIR" \( \
-path "*/.git/*" -o \
-path "*/.bzr/*" -o \
-path "*/.cvs/*" -o \
-path "*/.hg/*" -o \
-path "*/.svn/*" \) \
-delete
## Remove the empty vcs folder.
find "$WORKDIR" -type d \( \
-name ".bzr" -o \
-name ".cvs" -o \
-name ".git" -o \
-name ".hg" -o \
-name ".svn" \) \
-delete
echo "VCS files cleaned."
fi
}
## LSOF stats. Print number of open files per process.
lsofstat()
{
@ -763,7 +351,7 @@ bindatasearch()
xmulti ()
{
if [ $# -ne 1 ]; then
echo "Wrong number of argument."
echo "Usage: $0 VIDEOPORT"
return
fi
@ -773,127 +361,13 @@ xmulti ()
xmultioff ()
{
if [ $# -ne 1 ]; then
echo "Wrong number of argument."
echo "Usage: $0 VIDEOPORT"
return
fi
xrandr --output "$1" --off
}
## Format C using 'indent'. Alternative to indent: astyle, uncrustify.
formatc()
{
if [ -z "$(command -v indent)" ]; then
echo "Please install 'indent'"
return
fi
_formatc_dir()
{
find "$1" -type f \
-name "*.[ch]" \
-print \
-exec indent -i4 -ppi4 -bli0 -cli4 -nut {} \;
## Remove backup files.
find "$1" -type f \( \
-name "*.[ch]~" -o \
-name "*.[ch]~[0-9]*~" \) \
-delete
}
if [ $# -eq 0 ]; then
echo "Working on current dir"
_formatc_dir "$PWD"
return
fi
for i ; do
echo "[$i]"
if [ -d "$i" ]; then
_formatc_dir "$i"
else
indent -i4 -ppi4 -bli0 -cli4 -nut "$i"
rm -f "$i~" "$i~[0-9]*~"
fi
done
}
## LaTeX quick compiler. It adds the preambule -- and the \end{document} --
## automatically.
ltx()
{
if [ $# -ne 1 ]; then
echo "Usage: $0 FILE"
return
fi
local PREAMBLE
local END
## One line is mandatory.
PREAMBLE='\documentclass[10pt,a4paper]{article}\usepackage[utf8]{inputenc}\usepackage[T1]{fontenc}\usepackage{amsmath,amssymb,amsfonts}\usepackage{geometry}\usepackage{lmodern}\usepackage{marvosym}\usepackage{textcomp}\DeclareUnicodeCharacter{20AC}{\EUR{}}\DeclareUnicodeCharacter{2264}{\leqslant}\DeclareUnicodeCharacter{2265}{\geqslant}\begin{document}\input'
END='\end{document}'
pdflatex -file-line-error-style -interaction nonstopmode -jobname="${1%.tex}" "$PREAMBLE" "$1" "$END"
}
ediff()
{
emacs -q -l ~/.emacs-light --eval "(ediff \"$1\" \"$2\")"
}
crun()
{
if [ $# -lt 1 ]; then
echo "Usage: $0 FILE [GCC_OPTS]"
return
fi
local INPUT
local GCC_OPTS
local FILE
INPUT="$1"
shift
GCC_OPTS="-O0 -Wall -Wextra -Wshadow -pthread"
if [ $# -ne 0 ]; then
GCC_OPTS="$@"
fi
FILE=$(mktemp)
echo "==> gcc \"$INPUT\" -o \"$FILE\" $GCC_OPTS"
## Zsh compatibility. We need it otherwise word splitting of parameter like
## TC_SAMPLE will not work.
local STATUS
STATUS="$(set -o | grep 'shwordsplit' | awk '{print $2}')"
[ "$STATUS" = "off" ] && set -o shwordsplit
gcc "$INPUT" -o "$FILE" $GCC_OPTS
## Restore Zsh previous options. This will not turn off shwordsplit if it
## was on before calling the function.
[ "$STATUS" = "off" ] && set +o shwordsplit
echo "==> $FILE"
"$FILE"
rm "$FILE"
}
git-check()
{
while IFS= read -r FOLDER; do
if [ -z "$(cd "${FOLDER%/*}" && git status -uno | grep "nothing to commit")" ]; then
echo "${FOLDER%/*}"
fi
done <<EOF
$(find . -type d -name ".git")
EOF
}
## Extended pgrep -a.
pgex ()
{