2012-07-03 17:21:48 +02:00
|
|
|
|
## -*- mode:sh -*- #
|
2012-06-15 16:54:02 +02:00
|
|
|
|
################################################################################
|
|
|
|
|
## Shell -- Functions.
|
2012-12-05 19:18:06 +01:00
|
|
|
|
## Date 2012-12-05
|
2012-06-15 16:54:02 +02:00
|
|
|
|
################################################################################
|
|
|
|
|
|
2012-08-13 18:18:56 +02:00
|
|
|
|
## Colored 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 "$@"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
## Make directories and change into the first one.
|
2012-06-21 17:34:18 +02:00
|
|
|
|
mkcd () {
|
2012-08-13 18:18:56 +02:00
|
|
|
|
mkdir -p "$@"
|
|
|
|
|
cd "$1"
|
2012-06-21 17:34:18 +02:00
|
|
|
|
}
|
2012-06-15 16:54:02 +02:00
|
|
|
|
|
2012-06-21 17:34:18 +02:00
|
|
|
|
## Quick search.
|
|
|
|
|
search()
|
2012-06-15 16:54:02 +02:00
|
|
|
|
{
|
2012-06-21 17:34:18 +02:00
|
|
|
|
find . -iname "*$1*"
|
2012-06-15 16:54:02 +02:00
|
|
|
|
}
|
|
|
|
|
|
2012-06-21 17:34:18 +02:00
|
|
|
|
## Cut last n lines in file, 10 by default.
|
|
|
|
|
cuttail()
|
2012-06-15 16:54:02 +02:00
|
|
|
|
{
|
2012-08-06 15:41:10 +02:00
|
|
|
|
local nlines=${2:-10}
|
2012-08-23 13:33:47 +02:00
|
|
|
|
sed -n -e ":begin ; 1,${nlines}!{P;N;D} ; N ; b begin" $1
|
2012-06-21 17:34:18 +02:00
|
|
|
|
}
|
2012-06-15 16:54:02 +02:00
|
|
|
|
|
2012-06-21 17:34:18 +02:00
|
|
|
|
## Cut first n lines in file, 10 by default.
|
|
|
|
|
cuthead()
|
|
|
|
|
{
|
2012-08-06 15:41:10 +02:00
|
|
|
|
local nlines=${2:-10}
|
2012-08-23 13:33:47 +02:00
|
|
|
|
sed -n -e "1,${nlines}!p" $1
|
2012-06-21 17:34:18 +02:00
|
|
|
|
}
|
2012-06-15 16:54:02 +02:00
|
|
|
|
|
2012-06-21 17:34:18 +02:00
|
|
|
|
## Move filenames to lowercase.
|
2012-08-23 13:33:47 +02:00
|
|
|
|
## Use ranger + bulkrename + vim/emacs (+ TwindleCase) for more flexibility.
|
2012-06-21 17:34:18 +02:00
|
|
|
|
renamelower()
|
|
|
|
|
{
|
2012-08-06 15:41:10 +02:00
|
|
|
|
local file
|
|
|
|
|
local filename
|
|
|
|
|
local dirname
|
|
|
|
|
|
2012-06-21 17:34:18 +02:00
|
|
|
|
for file ; do
|
|
|
|
|
filename=${file##*/}
|
2012-08-13 18:18:56 +02:00
|
|
|
|
|
2012-06-21 17:34:18 +02:00
|
|
|
|
case "$filename" in
|
2012-08-13 18:18:56 +02:00
|
|
|
|
*/*) dirname="${file%/*}" ;;
|
|
|
|
|
*) dirname="." ;;
|
2012-06-15 16:54:02 +02:00
|
|
|
|
esac
|
2012-08-13 18:18:56 +02:00
|
|
|
|
|
2012-08-06 15:41:10 +02:00
|
|
|
|
local nf=$(echo $filename | tr A-Z a-z)
|
|
|
|
|
local newname="${dirname}/${nf}"
|
2012-06-21 17:34:18 +02:00
|
|
|
|
if [ "$nf" != "$filename" ]; then
|
|
|
|
|
mv "$file" "$newname"
|
|
|
|
|
echo "lowercase: $file --> $newname"
|
|
|
|
|
else
|
|
|
|
|
echo "lowercase: $file not changed."
|
|
|
|
|
fi
|
|
|
|
|
done
|
|
|
|
|
}
|
2012-06-15 16:54:02 +02:00
|
|
|
|
|
2012-06-21 17:34:18 +02:00
|
|
|
|
## Swap 2 filenames around.
|
|
|
|
|
renameswap()
|
|
|
|
|
{
|
2012-08-13 18:18:56 +02:00
|
|
|
|
local TEMPFILE="$(mktemp)"
|
|
|
|
|
mv "$1" "$TEMPFILE"
|
2012-06-21 17:34:18 +02:00
|
|
|
|
mv "$2" "$1"
|
2012-08-13 18:18:56 +02:00
|
|
|
|
mv "$TEMPFILE" "$2"
|
2012-06-21 17:34:18 +02:00
|
|
|
|
}
|
2012-06-15 16:54:02 +02:00
|
|
|
|
|
2012-06-21 17:34:18 +02:00
|
|
|
|
## Network operations
|
2012-12-05 19:18:06 +01:00
|
|
|
|
if [ -n "$(command -v nmap)" ]; then
|
2012-09-11 11:34:11 +02:00
|
|
|
|
network-map()
|
|
|
|
|
{
|
2012-06-21 17:34:18 +02:00
|
|
|
|
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
|
|
|
|
|
|
2012-09-02 19:32:47 +02:00
|
|
|
|
netspeed ()
|
|
|
|
|
{
|
|
|
|
|
local INTERFACE="eth0"
|
|
|
|
|
if [ -n "$1" ]; then
|
|
|
|
|
INTERFACE="$1"
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
if [ ! -d "/sys/class/net/${INTERFACE}" ]; then
|
|
|
|
|
echo "Error: no such interface (${INTERFACE})"
|
|
|
|
|
return
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
local RX_BEFORE=$(cat /sys/class/net/${INTERFACE}/statistics/rx_bytes)
|
|
|
|
|
local TX_BEFORE=$(cat /sys/class/net/${INTERFACE}/statistics/tx_bytes)
|
|
|
|
|
sleep 1
|
|
|
|
|
local RX_AFTER=$(cat /sys/class/net/${INTERFACE}/statistics/rx_bytes)
|
|
|
|
|
local TX_AFTER=$(cat /sys/class/net/${INTERFACE}/statistics/tx_bytes)
|
|
|
|
|
|
|
|
|
|
local RX_RESULT=$(((${RX_AFTER:-0}-${RX_BEFORE:-0})/1024))
|
|
|
|
|
local TX_RESULT=$(((${TX_AFTER:-0}-${TX_BEFORE:-0})/1024))
|
|
|
|
|
|
|
|
|
|
echo "RX $RX_RESULT KiB/s"
|
|
|
|
|
echo "TX $TX_RESULT KiB/s"
|
|
|
|
|
}
|
|
|
|
|
|
2012-09-04 23:54:05 +02:00
|
|
|
|
## CPU usage
|
|
|
|
|
##
|
|
|
|
|
## A typical CPU array is (on Linux Kernel 3.0)
|
|
|
|
|
## cpu 158150 0 52354 18562746 1472 0 10198 0 0 0
|
|
|
|
|
##
|
|
|
|
|
## The meanings of the columns are as follows, from left to right:
|
|
|
|
|
##
|
|
|
|
|
## user: normal processes executing in user mode
|
|
|
|
|
## nice: niced processes executing in user mode
|
|
|
|
|
## system: processes executing in kernel mode
|
|
|
|
|
## idle: twiddling thumbs
|
|
|
|
|
## iowait: waiting for I/O to complete
|
|
|
|
|
## irq: servicing interrupts
|
|
|
|
|
## softirq: servicing softirqs
|
|
|
|
|
## ... (see 'man 5 proc' for further details)
|
|
|
|
|
##
|
|
|
|
|
## Only the first 4 values are interesting here.
|
2012-09-11 11:34:11 +02:00
|
|
|
|
cpuusage()
|
|
|
|
|
{
|
2012-09-04 23:54:05 +02:00
|
|
|
|
local cpuarray
|
|
|
|
|
local f1
|
|
|
|
|
local f2
|
|
|
|
|
local f3
|
|
|
|
|
local f4
|
|
|
|
|
|
|
|
|
|
cpuarray="$(grep 'cpu ' /proc/stat)"
|
|
|
|
|
f1=$(echo $cpuarray | cut -f3 -d' ')
|
|
|
|
|
f2=$(echo $cpuarray | cut -f4 -d' ')
|
|
|
|
|
f3=$(echo $cpuarray | cut -f5 -d' ')
|
|
|
|
|
f4=$(echo $cpuarray | cut -f6 -d' ')
|
|
|
|
|
|
|
|
|
|
local totalA=$(($f1+$f2+$f3+$f4))
|
|
|
|
|
local idleA=$f4
|
|
|
|
|
|
|
|
|
|
sleep 0.4
|
|
|
|
|
|
|
|
|
|
cpuarray="$(grep 'cpu ' /proc/stat)"
|
|
|
|
|
f1=$(echo $cpuarray | cut -f3 -d' ')
|
|
|
|
|
f2=$(echo $cpuarray | cut -f4 -d' ')
|
|
|
|
|
f3=$(echo $cpuarray | cut -f5 -d' ')
|
|
|
|
|
f4=$(echo $cpuarray | cut -f6 -d' ')
|
|
|
|
|
|
|
|
|
|
local totalB=$(($f1+$f2+$f3+$f4))
|
|
|
|
|
local idleB=$f4
|
|
|
|
|
|
|
|
|
|
local totaldiff=$((${totalB:-0}-${totalA:-0}))
|
|
|
|
|
|
|
|
|
|
if [ $totaldiff -eq 0 ]; then
|
|
|
|
|
echo 0
|
|
|
|
|
else
|
|
|
|
|
echo "$((100 - 100 * ($idleB-$idleA) / $totaldiff ))"
|
|
|
|
|
fi
|
|
|
|
|
}
|
|
|
|
|
|
2012-09-14 10:35:53 +02:00
|
|
|
|
|
2012-12-05 18:39:38 +01:00
|
|
|
|
## Wget batch DL
|
|
|
|
|
wget_batch ()
|
|
|
|
|
{
|
|
|
|
|
if [ $# -ne 2 ]; then
|
|
|
|
|
echo "Usage: $0 <ext> <uri>"
|
|
|
|
|
return
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
wget -r -l1 --no-parent -A$1 $2
|
|
|
|
|
}
|
|
|
|
|
|
2012-09-10 22:42:43 +02:00
|
|
|
|
## Webcam
|
|
|
|
|
webcam ()
|
|
|
|
|
{
|
|
|
|
|
## With mplayer.
|
|
|
|
|
mplayer -fps 30 -tv driver=v4l2:width=640:height=480:device=/dev/video0 tv:// -flip
|
|
|
|
|
}
|
|
|
|
|
|
2012-09-04 23:54:05 +02:00
|
|
|
|
|
2012-08-13 18:18:56 +02:00
|
|
|
|
## Vim-only: search the vim reference manual for a keyword.
|
|
|
|
|
## Usage: :h <keyword>
|
2012-12-05 19:18:06 +01:00
|
|
|
|
if [ -n "$(command -v vim)" ]; then
|
2012-06-21 17:34:18 +02:00
|
|
|
|
:h()
|
2012-06-15 16:54:02 +02:00
|
|
|
|
{
|
2012-06-21 17:34:18 +02:00
|
|
|
|
vim --cmd ":silent help $@" --cmd "only";
|
|
|
|
|
}
|
|
|
|
|
fi
|
2012-06-15 16:54:02 +02:00
|
|
|
|
|
2012-08-23 13:33:47 +02:00
|
|
|
|
## Set file/directory owner and permissions.
|
|
|
|
|
## Usage: sanitize [FOLDER]
|
2012-06-21 17:34:18 +02:00
|
|
|
|
sanitize()
|
|
|
|
|
{
|
2012-08-23 13:33:47 +02:00
|
|
|
|
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} {} \;
|
2012-06-21 17:34:18 +02:00
|
|
|
|
}
|
2012-06-15 16:54:02 +02:00
|
|
|
|
|
2012-06-21 17:34:18 +02:00
|
|
|
|
asciify()
|
|
|
|
|
{
|
2013-01-21 15:33:01 +01:00
|
|
|
|
_printhelp()
|
2012-06-21 17:34:18 +02:00
|
|
|
|
{
|
2013-01-21 15:33:01 +01:00
|
|
|
|
cat <<EOF
|
|
|
|
|
Usage: $1 FILES
|
|
|
|
|
|
|
|
|
|
Convert non-ASCII characters to their ASCII equivalent.
|
|
|
|
|
EOF
|
2012-06-21 17:34:18 +02:00
|
|
|
|
}
|
2012-06-15 16:54:02 +02:00
|
|
|
|
|
2012-06-21 17:34:18 +02:00
|
|
|
|
if [ $# -eq 0 ]; then
|
|
|
|
|
echo "Missing arguments."
|
2013-01-21 15:33:01 +01:00
|
|
|
|
_printhelp $0
|
2012-06-21 17:34:18 +02:00
|
|
|
|
return
|
|
|
|
|
fi
|
2012-06-15 16:54:02 +02:00
|
|
|
|
|
2012-06-21 17:34:18 +02:00
|
|
|
|
for i; do
|
2012-08-07 15:21:57 +02:00
|
|
|
|
sed -i \
|
2012-10-10 15:11:58 +02:00
|
|
|
|
-e 's/[áàâä]/a/g' \
|
2012-08-13 18:18:56 +02:00
|
|
|
|
-e 's/[éèêë]/e/g' \
|
2012-10-10 15:11:58 +02:00
|
|
|
|
-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' \
|
2012-08-13 18:18:56 +02:00
|
|
|
|
"$i"
|
2012-06-21 17:34:18 +02:00
|
|
|
|
done;
|
|
|
|
|
}
|
2012-06-15 16:54:02 +02:00
|
|
|
|
|
2012-07-04 13:21:01 +02:00
|
|
|
|
blind-append()
|
2012-06-21 17:34:18 +02:00
|
|
|
|
{
|
2013-01-21 15:33:01 +01:00
|
|
|
|
_printhelp()
|
2012-06-21 17:34:18 +02:00
|
|
|
|
{
|
2013-01-21 15:33:01 +01:00
|
|
|
|
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
|
2012-06-21 17:34:18 +02:00
|
|
|
|
}
|
2012-06-15 16:54:02 +02:00
|
|
|
|
|
2012-07-04 13:21:01 +02:00
|
|
|
|
if [ $# -gt 2 ] || [ $# -lt 1 ]; then
|
2012-06-21 17:34:18 +02:00
|
|
|
|
echo "Wrong number of arguments."
|
2013-01-21 15:33:01 +01:00
|
|
|
|
_printhelp $0
|
2012-06-21 17:34:18 +02:00
|
|
|
|
return
|
|
|
|
|
fi
|
2012-06-15 16:54:02 +02:00
|
|
|
|
|
2012-08-06 15:41:10 +02:00
|
|
|
|
local FILE="$1"
|
|
|
|
|
local STRING=""
|
|
|
|
|
local DUMMY
|
|
|
|
|
|
2012-07-04 13:21:01 +02:00
|
|
|
|
if [ $# -eq 2 ]; then
|
|
|
|
|
STRING="$2"
|
|
|
|
|
fi
|
2012-06-15 16:54:02 +02:00
|
|
|
|
|
2012-08-13 18:18:56 +02:00
|
|
|
|
echo -n "Secret: "
|
2012-06-21 17:34:18 +02:00
|
|
|
|
read -s DUMMY
|
2012-08-13 18:18:56 +02:00
|
|
|
|
echo ""
|
2012-07-04 13:21:01 +02:00
|
|
|
|
|
2012-09-02 19:32:47 +02:00
|
|
|
|
if [ ! -e "$FILE" ] || [ -z "$STRING" ]; then
|
2012-07-04 13:21:01 +02:00
|
|
|
|
echo "$DUMMY" >> "$FILE"
|
2012-08-13 18:18:56 +02:00
|
|
|
|
echo "Secret appended to ${FILE} at the end."
|
2012-07-04 13:21:01 +02:00
|
|
|
|
return
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if [ $# -eq 1 ]; then
|
|
|
|
|
echo "$DUMMY" >> "$FILE"
|
|
|
|
|
else
|
|
|
|
|
sed -i "s/${STRING}/${STRING}${DUMMY}/g" "${FILE}"
|
|
|
|
|
fi
|
2012-08-13 18:18:56 +02:00
|
|
|
|
echo "Secret appended to ${FILE}."
|
2012-07-04 13:21:01 +02:00
|
|
|
|
return
|
2012-06-21 17:34:18 +02:00
|
|
|
|
}
|
2012-06-15 16:54:02 +02:00
|
|
|
|
|
2012-07-12 20:08:34 +02:00
|
|
|
|
## Term properties
|
2012-08-16 20:10:46 +02:00
|
|
|
|
termcolors256()
|
|
|
|
|
{
|
2012-07-12 20:08:34 +02:00
|
|
|
|
x=$(tput op)
|
|
|
|
|
y=$(printf %76s)
|
|
|
|
|
for i in {0..256}; do
|
|
|
|
|
o=00$i
|
|
|
|
|
echo -e "${o:${#o}-3:3} $(tput setaf $i;tput setab $i)${y// /=}$x"
|
|
|
|
|
done
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-21 15:33:01 +01:00
|
|
|
|
## Will not work for Bourne Shell.
|
2012-08-16 20:10:46 +02:00
|
|
|
|
termcolors()
|
|
|
|
|
{
|
|
|
|
|
# The test text. Must be 3 letters.
|
|
|
|
|
T='Doh'
|
|
|
|
|
|
|
|
|
|
echo -e "\n 40m 41m 42m 43m\
|
|
|
|
|
44m 45m 46m 47m";
|
|
|
|
|
|
|
|
|
|
for FGs in ' m' ' 1m' ' 30m' '1;30m' ' 31m' '1;31m' ' 32m' \
|
|
|
|
|
'1;32m' ' 33m' '1;33m' ' 34m' '1;34m' ' 35m' '1;35m' \
|
|
|
|
|
' 36m' '1;36m' ' 37m' '1;37m';
|
|
|
|
|
do FG=${FGs// /}
|
|
|
|
|
echo -en " $FGs \033[$FG $T "
|
|
|
|
|
for BG in 40m 41m 42m 43m 44m 45m 46m 47m;
|
|
|
|
|
do echo -en "$EINS \033[$FG\033[$BG $T \033[0m";
|
|
|
|
|
done
|
|
|
|
|
echo;
|
|
|
|
|
done
|
|
|
|
|
echo
|
|
|
|
|
}
|
|
|
|
|
|
2012-07-12 20:08:34 +02:00
|
|
|
|
termprop()
|
|
|
|
|
{
|
|
|
|
|
infocmp -1 | sed -nu 's/^[ \000\t]*//;s/[ \000\t]*$//;/[^ \t\000]\{1,\}/!d;/acsc/d;s/=.*,//p'| column -c80
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
termsupport()
|
|
|
|
|
{
|
|
|
|
|
for T in `find "/usr/share/terminfo" -type f -printf '%f\n'`;do
|
|
|
|
|
echo "$T `tput -T $T colors`"
|
|
|
|
|
done | sort -nk2
|
|
|
|
|
}
|
|
|
|
|
|
2012-12-05 19:18:06 +01:00
|
|
|
|
if [ -n "$(command -v highlight)" ] ; then
|
2012-08-13 18:18:56 +02:00
|
|
|
|
hlt-viewer ()
|
2012-07-17 18:32:15 +02:00
|
|
|
|
{
|
2013-01-21 15:33:01 +01:00
|
|
|
|
_printhelp()
|
2012-08-13 18:18:56 +02:00
|
|
|
|
{
|
2013-01-21 15:33:01 +01:00
|
|
|
|
cat<<EOF
|
|
|
|
|
Usage: $1
|
|
|
|
|
|
|
|
|
|
Highlight themes viewer for 256-colors terminals.
|
|
|
|
|
|
|
|
|
|
highlight is a tool for listing printing. This script displays a preview for
|
|
|
|
|
every highlight's theme. Press ENTER to display next preview. Press anything
|
|
|
|
|
else then ENTER to quit.
|
|
|
|
|
EOF
|
2012-08-13 18:18:56 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if [ $# -gt 0 ]; then
|
2013-01-21 15:33:01 +01:00
|
|
|
|
_printhelp "$0"
|
2012-08-13 18:18:56 +02:00
|
|
|
|
return
|
|
|
|
|
fi
|
2012-07-17 18:32:15 +02:00
|
|
|
|
|
2013-01-21 15:33:01 +01:00
|
|
|
|
local SAMPLE="$(cat <<EOF
|
|
|
|
|
void function(char* pointer, int &ref)
|
2012-07-17 18:32:15 +02:00
|
|
|
|
{
|
|
|
|
|
int integer=3;
|
2013-01-21 15:33:01 +01:00
|
|
|
|
char* string="Hello!";
|
2012-07-17 18:32:15 +02:00
|
|
|
|
|
|
|
|
|
// Line comment
|
|
|
|
|
for ( i=0; i<10; i++)
|
|
|
|
|
{
|
2013-01-21 15:33:01 +01:00
|
|
|
|
printf("Sum: %d\n", i*( integer+14 ) );
|
2012-07-17 18:32:15 +02:00
|
|
|
|
}
|
2013-01-21 15:33:01 +01:00
|
|
|
|
}
|
|
|
|
|
EOF
|
|
|
|
|
)"
|
2012-07-17 18:32:15 +02:00
|
|
|
|
|
2012-08-13 18:18:56 +02:00
|
|
|
|
unset DUMMY
|
|
|
|
|
for i in $(highlight -w | grep -v "\s\|^$"); do
|
|
|
|
|
echo "******************** $i ********************"
|
2013-01-21 15:33:01 +01:00
|
|
|
|
highlight -S c -O xterm256 <(echo "$SAMPLE") -s "$i"
|
|
|
|
|
read DUMMY
|
2012-09-02 19:32:47 +02:00
|
|
|
|
if [ -n "$DUMMY" ]; then
|
2012-08-13 18:18:56 +02:00
|
|
|
|
return
|
|
|
|
|
fi
|
|
|
|
|
done
|
|
|
|
|
}
|
|
|
|
|
fi
|
2012-07-17 18:32:15 +02:00
|
|
|
|
|
2012-06-21 17:34:18 +02:00
|
|
|
|
## Extractor -- Useless when using 'atool'.
|
2012-12-05 19:18:06 +01:00
|
|
|
|
if [ ! -n "$(command -v atool)" ]; then
|
2012-09-11 11:34:11 +02:00
|
|
|
|
extract ()
|
|
|
|
|
{
|
2012-08-13 18:18:56 +02:00
|
|
|
|
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
|
2012-06-21 17:34:18 +02:00
|
|
|
|
}
|
|
|
|
|
fi
|
2012-06-15 16:54:02 +02:00
|
|
|
|
|
2012-12-09 13:36:43 +01:00
|
|
|
|
## 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.
|
2012-06-15 16:54:02 +02:00
|
|
|
|
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
|
2012-08-06 15:41:10 +02:00
|
|
|
|
local INPUTFILE="$1"
|
2012-06-15 16:54:02 +02:00
|
|
|
|
|
|
|
|
|
if [ -z "$2" ]; then
|
|
|
|
|
gs -dBATCH -dNOPAUSE -q -sDEVICE=pdfwrite -sOutputFile="${INPUTFILE%%.*}-COMPRESSED.pdf" "${INPUTFILE}"
|
|
|
|
|
rm -rf "${INPUTFILE}"
|
|
|
|
|
mv "${INPUTFILE%%.*}-COMPRESSED.pdf" "${INPUTFILE}"
|
|
|
|
|
else
|
2012-08-06 15:41:10 +02:00
|
|
|
|
local OUTPUTFILE="$2"
|
2012-06-15 16:54:02 +02:00
|
|
|
|
gs -dBATCH -dNOPAUSE -q -sDEVICE=pdfwrite -sOutputFile="${OUTPUTFILE}" "${INPUTFILE}"
|
|
|
|
|
fi
|
|
|
|
|
}
|
|
|
|
|
|
2012-12-09 13:36:43 +01:00
|
|
|
|
## Extract a range of pages from a PDF.
|
2012-09-26 16:28:56 +02:00
|
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
|
2012-12-09 13:36:43 +01:00
|
|
|
|
## 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
|
|
|
|
|
}
|
|
|
|
|
|
2012-07-30 12:59:20 +02:00
|
|
|
|
archive ()
|
2012-06-15 16:54:02 +02:00
|
|
|
|
{
|
2013-01-21 15:33:01 +01:00
|
|
|
|
_printhelp()
|
2012-07-27 19:25:27 +02:00
|
|
|
|
{
|
2013-01-21 15:33:01 +01:00
|
|
|
|
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
|
2012-07-27 19:25:27 +02:00
|
|
|
|
}
|
|
|
|
|
|
2012-11-03 16:44:09 +01:00
|
|
|
|
local OPTION_VCS=""
|
2012-08-06 15:41:10 +02:00
|
|
|
|
local OPTION_METHOD=""
|
2012-07-27 19:25:27 +02:00
|
|
|
|
|
|
|
|
|
while getopts ":hm:v" opt; do
|
|
|
|
|
case $opt in
|
|
|
|
|
h)
|
2013-01-21 15:33:01 +01:00
|
|
|
|
_printhelp "$0"
|
2012-07-27 19:25:27 +02:00
|
|
|
|
return 1
|
|
|
|
|
;;
|
|
|
|
|
m)
|
|
|
|
|
OPTION_METHOD="${OPTARG}"
|
|
|
|
|
;;
|
|
|
|
|
v)
|
2012-11-03 16:44:09 +01:00
|
|
|
|
OPTION_VCS="--exclude-vcs"
|
2012-07-27 19:25:27 +02:00
|
|
|
|
;;
|
|
|
|
|
?)
|
2013-01-21 15:33:01 +01:00
|
|
|
|
_printhelp "$0"
|
2012-07-27 19:25:27 +02:00
|
|
|
|
return 1
|
|
|
|
|
;;
|
|
|
|
|
:)
|
|
|
|
|
echo "Missing argument."
|
|
|
|
|
echo "Use $0 -h for help."
|
|
|
|
|
return 1
|
|
|
|
|
;;
|
|
|
|
|
esac
|
|
|
|
|
done
|
|
|
|
|
|
2012-08-06 15:41:10 +02:00
|
|
|
|
local ARCEXT
|
|
|
|
|
local ARCOPT
|
|
|
|
|
|
2012-07-27 19:25:27 +02:00
|
|
|
|
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))
|
|
|
|
|
|
2012-08-06 15:41:10 +02:00
|
|
|
|
local ARCPATH
|
|
|
|
|
local ARCSOURCE
|
|
|
|
|
local ARCNAME
|
|
|
|
|
|
2012-06-15 16:54:02 +02:00
|
|
|
|
if [ $# -eq 1 ]; then
|
|
|
|
|
ARCPATH="$(dirname $(readlink -f "$1"))"
|
|
|
|
|
ARCSOURCE="$(basename $(readlink -f "$1"))"
|
2012-07-27 19:25:27 +02:00
|
|
|
|
ARCNAME="${ARCSOURCE}-$(date +%y-%m-%d-%H%M%S).${ARCEXT}"
|
2012-06-15 16:54:02 +02:00
|
|
|
|
|
|
|
|
|
(cd "$ARCPATH" && \
|
2012-11-03 16:44:09 +01:00
|
|
|
|
tar -${ARCOPT} -cf "$ARCNAME" "$ARCSOURCE" ${OPTION_VCS})
|
2012-06-15 16:54:02 +02:00
|
|
|
|
|
|
|
|
|
else
|
|
|
|
|
ARCSOURCE="$(basename $PWD)"
|
2012-07-27 19:25:27 +02:00
|
|
|
|
ARCNAME="$(basename "$ARCSOURCE")-$(date +%y-%m-%d-%H%M%S).${ARCEXT}"
|
2012-06-15 16:54:02 +02:00
|
|
|
|
|
|
|
|
|
if [ $# = 0 ];then
|
|
|
|
|
|
|
|
|
|
(cd "$PWD/.." && \
|
2012-11-03 16:44:09 +01:00
|
|
|
|
tar -${ARCOPT} -cf "$ARCNAME" "$ARCSOURCE" ${OPTION_VCS})
|
2012-06-15 16:54:02 +02:00
|
|
|
|
|
|
|
|
|
else
|
2012-09-11 11:34:11 +02:00
|
|
|
|
## Note for zsh: do NOT initialize an array with "myarray=()".
|
|
|
|
|
local FILELIST
|
|
|
|
|
|
2012-06-21 17:34:18 +02:00
|
|
|
|
for i; do
|
|
|
|
|
FILELIST=(${FILELIST[*]} "$i")
|
2012-06-15 16:54:02 +02:00
|
|
|
|
done
|
2012-09-11 11:34:11 +02:00
|
|
|
|
|
2012-11-03 16:44:09 +01:00
|
|
|
|
tar -${ARCOPT} -cf "$ARCNAME" ${FILELIST[*]} ${OPTION_VCS}
|
2012-06-15 16:54:02 +02:00
|
|
|
|
fi
|
|
|
|
|
fi
|
|
|
|
|
}
|
|
|
|
|
|
2012-08-16 20:38:21 +02:00
|
|
|
|
## Git compare
|
|
|
|
|
git-compare()
|
2012-07-30 12:59:20 +02:00
|
|
|
|
{
|
2012-08-16 20:38:21 +02:00
|
|
|
|
if [ $# -ne 1 ]; then
|
|
|
|
|
echo "Wrong number of argument."
|
|
|
|
|
return
|
|
|
|
|
fi
|
|
|
|
|
|
2013-02-27 14:26:38 +01:00
|
|
|
|
comm -3 <(git ls-files | sort) <(find "$1" -type f ! -path "*.git/*" | sed 's/^[^\/]*\///g' | sort)
|
2012-08-16 20:38:21 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
## This function will clean TeX/LaTeX project folders recursively.
|
2012-09-11 11:34:11 +02:00
|
|
|
|
texclean ()
|
|
|
|
|
{
|
2012-07-30 12:59:20 +02:00
|
|
|
|
if [ -z "$1" ]; then
|
2012-08-16 20:38:21 +02:00
|
|
|
|
WORKDIR="$PWD"
|
2012-07-30 12:59:20 +02:00
|
|
|
|
else
|
2012-08-16 20:38:21 +02:00
|
|
|
|
WORKDIR="$1"
|
2012-07-30 12:59:20 +02:00
|
|
|
|
fi
|
|
|
|
|
|
2012-11-17 12:19:57 +01:00
|
|
|
|
## WARNING: removing .idx files is messy in a .git folder.
|
2012-07-30 12:59:20 +02:00
|
|
|
|
find "$WORKDIR" -type f \( \
|
|
|
|
|
-name "*.aux" -o \
|
2012-11-12 21:16:41 +01:00
|
|
|
|
-name "*.glg" -o \
|
|
|
|
|
-name "*.glo" -o \
|
|
|
|
|
-name "*.gls" -o \
|
2012-07-30 12:59:20 +02:00
|
|
|
|
-name "*.ilg" -o \
|
|
|
|
|
-name "*.ind" -o \
|
|
|
|
|
-name "*.lof" -o \
|
|
|
|
|
-name "*.log" -o \
|
2013-02-01 14:36:14 +01:00
|
|
|
|
-name "*.maf" -o \
|
|
|
|
|
-name "*.mtc" -o \
|
|
|
|
|
-name "*.mtc?" -o \
|
2012-07-30 12:59:20 +02:00
|
|
|
|
-name "*.nav" -o \
|
|
|
|
|
-name "*.out" -o \
|
|
|
|
|
-name "*.snm" -o \
|
2013-02-01 14:36:14 +01:00
|
|
|
|
-name "*.synctex" -o \
|
|
|
|
|
-name "*.synctex.gz" -o \
|
2012-07-30 12:59:20 +02:00
|
|
|
|
-name "*.tns" -o \
|
2012-11-12 21:16:41 +01:00
|
|
|
|
-name "*.toc" -o \
|
|
|
|
|
-name "*.xdy" \) \
|
2012-07-30 12:59:20 +02:00
|
|
|
|
-print \
|
|
|
|
|
-delete
|
|
|
|
|
}
|
|
|
|
|
|
2012-06-15 16:54:02 +02:00
|
|
|
|
## Clean current folder
|
2012-07-30 12:59:20 +02:00
|
|
|
|
vcsclean()
|
2012-06-15 16:54:02 +02:00
|
|
|
|
{
|
2012-08-06 15:41:10 +02:00
|
|
|
|
local WORKDIR
|
2012-07-30 12:59:20 +02:00
|
|
|
|
if [ -z "$1" ]; then
|
|
|
|
|
WORKDIR="$PWD"
|
|
|
|
|
else
|
|
|
|
|
WORKDIR="$1"
|
|
|
|
|
fi
|
2012-08-06 15:41:10 +02:00
|
|
|
|
|
|
|
|
|
local CHOICE
|
2013-01-21 15:33:01 +01:00
|
|
|
|
unset CHOICE
|
2012-06-15 16:54:02 +02:00
|
|
|
|
echo "This will clean current folder from all VCS control files."
|
|
|
|
|
echo -n "Proceed ? [y/N] "
|
2013-02-27 14:26:38 +01:00
|
|
|
|
read CHOICE
|
2012-06-15 16:54:02 +02:00
|
|
|
|
|
|
|
|
|
CHOICE=$(echo $CHOICE | tr '[:upper:]' '[:lower:]')
|
|
|
|
|
if [ "$CHOICE" = "y" ]; then
|
2013-02-27 14:26:38 +01:00
|
|
|
|
|
|
|
|
|
## First clean the folders.
|
|
|
|
|
find "$WORKDIR" \( \
|
|
|
|
|
-path "*/.git/*" -o \
|
|
|
|
|
-path "*/.bzr/*" -o \
|
|
|
|
|
-path "*/.cvs/*" -o \
|
|
|
|
|
-path "*/.hg/*" -o \
|
|
|
|
|
-path "*/.svn/*" \) \
|
2012-07-30 12:59:20 +02:00
|
|
|
|
-delete
|
|
|
|
|
|
2013-02-27 14:26:38 +01:00
|
|
|
|
## 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."
|
2012-06-15 16:54:02 +02:00
|
|
|
|
fi
|
|
|
|
|
}
|
|
|
|
|
|
2012-06-29 18:13:47 +02:00
|
|
|
|
## LSOF stats. Print number of open files per process.
|
|
|
|
|
lsofstat()
|
|
|
|
|
{
|
2012-08-06 15:41:10 +02:00
|
|
|
|
local LSOFOUT=$(lsof|awk '{print $1}')
|
2012-06-29 18:13:47 +02:00
|
|
|
|
|
|
|
|
|
for i in $(echo $LSOFOUT |sort -u);do
|
|
|
|
|
echo -n "$i "
|
|
|
|
|
echo $LSOFOUT | grep ^$i | wc -l
|
|
|
|
|
done
|
|
|
|
|
}
|
2012-06-15 16:54:02 +02:00
|
|
|
|
|
2013-01-08 16:58:05 +01:00
|
|
|
|
## Executable info
|
|
|
|
|
xinf()
|
|
|
|
|
{
|
|
|
|
|
if [ $# -ne 1 ]; then
|
|
|
|
|
echo "Give an executable in PATH as parameter."
|
|
|
|
|
return
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
local xpath="$(type $1 | cut -d' ' -f3)"
|
|
|
|
|
ls -l "$xpath"
|
|
|
|
|
file "$xpath" | cut -d':' -f2 | cut -b 2-
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-26 19:08:29 +01:00
|
|
|
|
## BAD CODE. This function will try to find an input binary file in the content
|
|
|
|
|
## of binary files in current folder. It does not work on big file because
|
|
|
|
|
## 'find' and 'grep' have an argument length limit.
|
2013-01-08 16:58:05 +01:00
|
|
|
|
bindatasearch()
|
|
|
|
|
{
|
2013-01-26 19:08:29 +01:00
|
|
|
|
echo "Deprecated function."
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
if [ $# -ne 2 ] -; then
|
|
|
|
|
echo "Usage: $0 FILECHUNK BIGFILE"
|
2013-01-08 16:58:05 +01:00
|
|
|
|
return
|
|
|
|
|
fi
|
|
|
|
|
|
2013-01-26 19:08:29 +01:00
|
|
|
|
if [ $(du -b Cover.jpg | awk '{print $1}') -gt 1048576 ]; then
|
|
|
|
|
echo "[$1] file size must not exceed 1MB."
|
|
|
|
|
return
|
2013-01-08 16:58:05 +01:00
|
|
|
|
fi
|
2013-01-26 19:08:29 +01:00
|
|
|
|
|
2013-01-08 16:58:05 +01:00
|
|
|
|
## The awk command suppresses first column, spaces and line breaks.
|
2013-01-26 19:08:29 +01:00
|
|
|
|
local SOURCE
|
|
|
|
|
SOURCE=$(od -tx8 "$1" | awk '($1="")1 gsub ( /\s/,"") { printf ("%s", $0) }')
|
|
|
|
|
|
|
|
|
|
local FILEDUMP
|
|
|
|
|
FILEDUMP=$(od -tx8 "$1" | awk '($1="")1 gsub ( /\s/,"") { printf ("%s", $0) }')
|
|
|
|
|
|
|
|
|
|
# grep -q "$SOURCE" <(echo "$FILEDUMP")
|
2013-01-08 16:58:05 +01:00
|
|
|
|
|
|
|
|
|
## This special, not well-known syntax allows us to execute a set of
|
|
|
|
|
## functions upon the result of find. Since everything MUST be between
|
|
|
|
|
## simple quotes, we cannot use external variables directly. So we pass the
|
|
|
|
|
## $SOURCE variable as argument. The underscore at the end allows the use of
|
2013-01-26 19:08:29 +01:00
|
|
|
|
## arguments. Note it will not work
|
|
|
|
|
# find "$TARGET" -type f -exec sh -c '
|
|
|
|
|
# # FILEDUMP=$(od -tx8 "$1" | awk '"'"'($1="")1 gsub ( /\s/,"") { printf ("%s", $0) }'"'"')
|
|
|
|
|
# # grep -q "$2" <(echo $FILEDUMP)
|
2013-01-08 16:58:05 +01:00
|
|
|
|
|
2013-01-26 19:08:29 +01:00
|
|
|
|
# # [ $? -eq 0 ] && echo "$1"
|
|
|
|
|
# ' _ {} "$SOURCE" \;
|
2013-01-08 16:58:05 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
## Xrandr
|
|
|
|
|
xmulti ()
|
|
|
|
|
{
|
|
|
|
|
if [ $# -ne 1 ]; then
|
|
|
|
|
echo "Wrong number of argument."
|
|
|
|
|
return
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
xrandr --output "$1" --auto --right-of $(xrandr | sed -n "/current/{n;p;q}" | cut -f1 -d" ")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
xmultioff ()
|
|
|
|
|
{
|
|
|
|
|
if [ $# -ne 1 ]; then
|
|
|
|
|
echo "Wrong number of argument."
|
|
|
|
|
return
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
xrandr --output "$1" --off
|
|
|
|
|
}
|
|
|
|
|
|
2012-12-05 19:18:06 +01:00
|
|
|
|
## Format C using 'indent'. Alternative to indent: astyle, uncrustify.
|
2012-07-25 13:16:21 +02:00
|
|
|
|
formatc()
|
|
|
|
|
{
|
2013-02-20 17:49:07 +01:00
|
|
|
|
if [ -z "$(command -v indent)" ]; then
|
2012-08-01 15:45:33 +02:00
|
|
|
|
echo "Please install 'indent'"
|
2012-07-25 13:16:21 +02:00
|
|
|
|
return
|
|
|
|
|
fi
|
|
|
|
|
|
2013-02-24 11:48:09 +01:00
|
|
|
|
_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
|
2012-08-01 15:45:33 +02:00
|
|
|
|
fi
|
2012-07-25 13:16:21 +02:00
|
|
|
|
|
2013-02-24 11:48:09 +01:00
|
|
|
|
for i ; do
|
|
|
|
|
echo "[$i]"
|
2012-07-25 13:16:21 +02:00
|
|
|
|
|
2013-02-24 11:48:09 +01:00
|
|
|
|
if [ -d "$i" ]; then
|
|
|
|
|
_formatc_dir "$i"
|
|
|
|
|
else
|
|
|
|
|
indent -i4 -ppi4 -bli0 -cli4 -nut "$i"
|
|
|
|
|
rm -f "$i~" "$i~[0-9]*~"
|
|
|
|
|
fi
|
|
|
|
|
done
|
2013-01-24 16:57:10 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
## LaTeX quick compiler. It adds the preambule -- and the \end{document} --
|
|
|
|
|
## automatically.
|
|
|
|
|
ltx()
|
|
|
|
|
{
|
|
|
|
|
if [ $# -ne 1 ]; then
|
|
|
|
|
echo "Usage: $0 FILE"
|
|
|
|
|
return
|
|
|
|
|
fi
|
2012-08-01 15:45:33 +02:00
|
|
|
|
|
2013-01-24 16:57:10 +01:00
|
|
|
|
local PREAMBLE
|
|
|
|
|
local END
|
2012-08-01 15:45:33 +02:00
|
|
|
|
|
2013-01-24 16:57:10 +01:00
|
|
|
|
## 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'
|
2012-08-01 15:45:33 +02:00
|
|
|
|
|
2013-01-24 16:57:10 +01:00
|
|
|
|
END='\end{document}'
|
|
|
|
|
|
|
|
|
|
pdflatex -file-line-error-style -interaction nonstopmode -jobname="${1%.tex}" "$PREAMBLE" "$1" "$END"
|
2012-07-25 13:16:21 +02:00
|
|
|
|
}
|
|
|
|
|
|
2013-02-17 11:58:49 +01:00
|
|
|
|
ediff()
|
|
|
|
|
{
|
|
|
|
|
emacs -q -l ~/.emacs-light --eval "(ediff \"$1\" \"$2\")"
|
|
|
|
|
}
|
|
|
|
|
|
2013-02-19 23:23:25 +01:00
|
|
|
|
crun()
|
|
|
|
|
{
|
2013-02-20 12:33:10 +01:00
|
|
|
|
if [ $# -lt 1 ]; then
|
|
|
|
|
echo "Usage: $0 FILE [GCC_OPTS]"
|
2013-02-19 23:23:25 +01:00
|
|
|
|
return
|
|
|
|
|
fi
|
|
|
|
|
|
2013-02-20 12:33:10 +01:00
|
|
|
|
local INPUT
|
|
|
|
|
local GCC_OPTS
|
|
|
|
|
local FILE
|
|
|
|
|
|
|
|
|
|
INPUT="$1"
|
|
|
|
|
|
|
|
|
|
shift
|
|
|
|
|
GCC_OPTS="-O0 -Wall -Wextra -Wshadow -pthread"
|
|
|
|
|
if [ $# -ne 0 ]; then
|
|
|
|
|
GCC_OPTS="$@"
|
|
|
|
|
fi
|
|
|
|
|
|
2013-02-19 23:23:25 +01:00
|
|
|
|
FILE=$(mktemp)
|
2013-02-20 12:33:10 +01:00
|
|
|
|
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"
|
2013-02-19 23:23:25 +01:00
|
|
|
|
"$FILE"
|
|
|
|
|
rm "$FILE"
|
|
|
|
|
}
|
|
|
|
|
|
2013-02-22 22:43:22 +01:00
|
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
|
2012-06-21 17:34:18 +02:00
|
|
|
|
##==============================================================================
|
2013-01-20 23:25:29 +01:00
|
|
|
|
## Pacman functions
|
2012-06-21 17:34:18 +02:00
|
|
|
|
##==============================================================================
|
2012-06-15 16:54:02 +02:00
|
|
|
|
|
2013-01-08 16:58:05 +01:00
|
|
|
|
[ -n "$(command -v pacman)" ] && . "${SHELL_DIR}/funs_pacman"
|
2012-11-01 14:47:35 +01:00
|
|
|
|
|
|
|
|
|
##==============================================================================
|
2013-01-20 23:25:29 +01:00
|
|
|
|
## FreeBSD functions
|
2012-11-01 14:47:35 +01:00
|
|
|
|
##==============================================================================
|
|
|
|
|
|
|
|
|
|
[ "$(uname)" = FreeBSD ] && . "${SHELL_DIR}/funs_freebsd"
|
2012-06-15 16:54:02 +02:00
|
|
|
|
|
2012-07-30 12:59:20 +02:00
|
|
|
|
##==============================================================================
|
|
|
|
|
## TeXlive functions
|
|
|
|
|
##==============================================================================
|
|
|
|
|
|
|
|
|
|
## TeXlive
|
|
|
|
|
if [ -d "/usr/local/texlive" ]; then
|
2012-12-05 19:18:06 +01:00
|
|
|
|
## WARNING: it is important to set umask properly, otherwise users might
|
2012-09-07 19:54:29 +02:00
|
|
|
|
## not have authorization to use the packages.
|
|
|
|
|
|
2013-01-10 11:05:43 +01:00
|
|
|
|
## This is bad pratice to write args without double quotes, but since we
|
|
|
|
|
## are sure there is no space in TeX Live packages, it is OK here.
|
2012-09-07 19:54:29 +02:00
|
|
|
|
|
2012-08-23 14:20:30 +02:00
|
|
|
|
tli ()
|
2012-07-30 12:59:20 +02:00
|
|
|
|
{
|
2013-01-10 11:05:43 +01:00
|
|
|
|
sudo sh -c 'umask 022 && tlmgr install $0 $@' "$@"
|
2012-08-23 14:20:30 +02:00
|
|
|
|
}
|
|
|
|
|
|
2012-09-07 19:54:29 +02:00
|
|
|
|
tlr ()
|
|
|
|
|
{
|
2013-01-10 11:05:43 +01:00
|
|
|
|
sudo sh -c 'umask 022 && tlmgr remove $0 $@' "$@"
|
2012-09-07 19:54:29 +02:00
|
|
|
|
}
|
2012-07-30 12:59:20 +02:00
|
|
|
|
fi
|
2013-01-20 23:25:29 +01:00
|
|
|
|
|
|
|
|
|
##==============================================================================
|
|
|
|
|
## Transcoding functions
|
|
|
|
|
##==============================================================================
|
|
|
|
|
|
|
|
|
|
[ -n "$(command -v ffmpeg)" ] && . "${SHELL_DIR}/funs_transcode"
|