Guest User

Untitled

a guest
Nov 18th, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.14 KB | None | 0 0
  1. # Use perldoc functions to see documentation for this file.
  2.  
  3. :<<"=cut"
  4.  
  5. =head1 NAME
  6.  
  7. functions - Common functions for bash scripts
  8.  
  9. =head1 DESCRIPTIONS
  10.  
  11. Define some common bash scripts often used in Moria environment. To
  12. use it simply source this file in your script.
  13.  
  14. =head2 SIDE EFFECTS
  15.  
  16. Re-sets PATH to /bin:/usr/bin:/sbin:/usr/sbin. If you need more PATHs
  17. add them after sourcing this file.
  18.  
  19. =head2 VARIABLES
  20.  
  21. To exit cleanly from parent script functions rely on $PARENT
  22. variable. It must be set in caller script as PARENT=$$
  23.  
  24. =head1 FUNCTIONS
  25.  
  26. =head2 osbreed
  27.  
  28. Returns OS name on STDOUT and sets variable $osbreed
  29.  
  30. =cut
  31.  
  32. osbreed () {
  33. export osbreed=$(awk 'NR==1 {print $1; exit}' < /etc/issue 2> /dev/null )
  34. echo $osbreed
  35. }
  36.  
  37. :<<"=cut"
  38.  
  39. =head2 am_i_root
  40.  
  41. Usage:
  42.  
  43. am_i_root
  44.  
  45.  
  46. Kills parent script if it is not run under root.
  47.  
  48. =cut
  49.  
  50. am_i_root () {
  51. test $(id -u) -eq 0 || { echo "Must be run as root"; exit 2; }
  52. }
  53.  
  54. :<<"=cut"
  55.  
  56. =head2 primary_ip
  57.  
  58. Detects IP address for the interface which have default route on it.
  59.  
  60. Returns IP on STDOUT and sets variable $primary_ip.
  61.  
  62. =cut
  63.  
  64. primary_ip () {
  65. local IF=$(netstat -r | awk '$0 ~ /^default/ {print $8}')
  66. export primary_ip=$(ifconfig ${IF} | awk '$0 ~ /inet addr:/ { print $2}' | cut -d: -f2)
  67. echo $primary_ip
  68. }
  69.  
  70. :<<"=cut"
  71.  
  72. =head2 check_dir
  73.  
  74. Check that directory exists. Return expanded PATH of the directory on
  75. STDOUT. If not exit with a message and error.
  76.  
  77. Can be used like:
  78.  
  79. # dir=\`check_dir PATH\`
  80.  
  81. =cut
  82.  
  83. check_dir () {
  84. test -z "$1" && { echo >&2 "Directory name must be provided"; kill $PARENT; exit 2; }
  85. test -d $1 || { echo >&2 "Directory $1 does not exist"; kill $PARENT; exit 1; }
  86. (cd $1 && pwd)
  87. }
  88.  
  89.  
  90. :<<"=cut"
  91.  
  92. =head2 check_file
  93.  
  94. Check that file exists. Return expanded PATH of the file on
  95. STDOUT. If not exit with a message and error.
  96.  
  97. Can be used like:
  98.  
  99. file=\$(check_file PATH)
  100.  
  101. =cut
  102.  
  103. check_file () {
  104. test -z "$1" && { echo >&2 "File name must be provided"; kill $PARENT; exit 2; }
  105. test -f $1 || { echo >&2 "File $1 does not exist"; kill $PARENT; exit 1; }
  106. echo $1
  107. }
  108.  
  109. # END OF FUNCTIONS
  110.  
  111. export PATH=/bin:/usr/bin:/sbin:/usr/sbin
  112. set -e
  113. osbreed > /dev/null
Add Comment
Please, Sign In to add comment