themactep

a collection of miscellaneous projects of Paul Philippov

Bash Useful Tips

How to copy from Xterm with a mouse

$ echo 'XTerm*selectToClipboard: true' >> ~/.Xresources

How to avoid duplicates in bash history

$ echo 'export HISTCONTROL=ignoredups' >> ~/.bashrc

to avoid both duplicates and leading spaces use

$ echo 'export HISTCONTROL=ignoreboth' >> ~/.bashrc

How to disable mouse in Midnight Commander

$ echo 'alias mc="mc -d"' >> ~/.bashrc

How to use either an argument or a default in a bash script

SHELL=${1-$"/bin/bash"}

is an equivalent to

if [ -z "$1" ];  then
    SHELL=$1
else
    SHELL="/bin/bash"
fi

more: http://tldp.org/LDP/abs/html/parameter-substitution.html

How to check is another copy of the script is already running

PID=$(pidof -x $0 -o $$)
if [ $? -eq 0 ]; then
    echo "Already running ($PID)"
    exit 1
fi

How to run make a script to run in background

if [ "x$1" != "x--" ]; then
    $0 -- 1> "out.log" 2> "error.log" &
    exit 0
fi