Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Use perldoc functions to see documentation for this file.
- :<<"=cut"
- =head1 NAME
- functions - Common functions for bash scripts
- =head1 DESCRIPTIONS
- Define some common bash scripts often used in Moria environment. To
- use it simply source this file in your script.
- =head2 SIDE EFFECTS
- Re-sets PATH to /bin:/usr/bin:/sbin:/usr/sbin. If you need more PATHs
- add them after sourcing this file.
- =head2 VARIABLES
- To exit cleanly from parent script functions rely on $PARENT
- variable. It must be set in caller script as PARENT=$$
- =head1 FUNCTIONS
- =head2 osbreed
- Returns OS name on STDOUT and sets variable $osbreed
- =cut
- osbreed () {
- export osbreed=$(awk 'NR==1 {print $1; exit}' < /etc/issue 2> /dev/null )
- echo $osbreed
- }
- :<<"=cut"
- =head2 am_i_root
- Usage:
- am_i_root
- Kills parent script if it is not run under root.
- =cut
- am_i_root () {
- test $(id -u) -eq 0 || { echo "Must be run as root"; exit 2; }
- }
- :<<"=cut"
- =head2 primary_ip
- Detects IP address for the interface which have default route on it.
- Returns IP on STDOUT and sets variable $primary_ip.
- =cut
- primary_ip () {
- local IF=$(netstat -r | awk '$0 ~ /^default/ {print $8}')
- export primary_ip=$(ifconfig ${IF} | awk '$0 ~ /inet addr:/ { print $2}' | cut -d: -f2)
- echo $primary_ip
- }
- :<<"=cut"
- =head2 check_dir
- Check that directory exists. Return expanded PATH of the directory on
- STDOUT. If not exit with a message and error.
- Can be used like:
- # dir=\`check_dir PATH\`
- =cut
- check_dir () {
- test -z "$1" && { echo >&2 "Directory name must be provided"; kill $PARENT; exit 2; }
- test -d $1 || { echo >&2 "Directory $1 does not exist"; kill $PARENT; exit 1; }
- (cd $1 && pwd)
- }
- :<<"=cut"
- =head2 check_file
- Check that file exists. Return expanded PATH of the file on
- STDOUT. If not exit with a message and error.
- Can be used like:
- file=\$(check_file PATH)
- =cut
- check_file () {
- test -z "$1" && { echo >&2 "File name must be provided"; kill $PARENT; exit 2; }
- test -f $1 || { echo >&2 "File $1 does not exist"; kill $PARENT; exit 1; }
- echo $1
- }
- # END OF FUNCTIONS
- export PATH=/bin:/usr/bin:/sbin:/usr/sbin
- set -e
- osbreed > /dev/null
Add Comment
Please, Sign In to add comment