Difference between revisions of "Bash"

From Blue-IT.org Wiki

(Environment)
Line 6: Line 6:
 
  export LC_TYPE="en_US.UTF-8"
 
  export LC_TYPE="en_US.UTF-8"
 
  export LANGUAGE="en_US.UTF-8
 
  export LANGUAGE="en_US.UTF-8
 +
 +
 +
== Singleton ==
 +
 +
The [http://en.wikipedia.org/wiki/Singleton_pattern singleton] pattern is a very handy one. You can realise it in bash like this:
 +
 +
if ps x | grep -v grep | grep -v $$ | grep $0 | grep -v subl | grep -v vi
 +
then
 +
        echo "$0 already running. Exiting"
 +
        exit 1
 +
else
 +
 +
################################################
 +
# PUT YOUR CODE HERE
 +
# vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
 +
 +
./run_me
 +
 +
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 +
################################################
 +
 
 +
fi
  
 
== Grep and Tail ==
 
== Grep and Tail ==

Revision as of 08:32, 8 January 2014

Environment

Put this in front of any script to avoid problems when running the script in other languages! The en_US locale should be avaiabel on all systems:

export LC_MESSAGES="en_US.UTF-8"
export LC_TYPE="en_US.UTF-8"
export LANGUAGE="en_US.UTF-8


Singleton

The singleton pattern is a very handy one. You can realise it in bash like this:

if ps x | grep -v grep | grep -v $$ | grep $0 | grep -v subl | grep -v vi
then
        echo "$0 already running. Exiting"
        exit 1
else

################################################
# PUT YOUR CODE HERE
# vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv

./run_me

# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
################################################ 
 
fi

Grep and Tail

tail -f -n 300 /var/log/syslog | stdbuf -o0 grep PATTERN