[PC-BSD Commits] r19052 - pcbsd/current/src-sh/libsh

svn at pcbsd.org svn at pcbsd.org
Tue Sep 11 10:46:03 PDT 2012


Author: kris
Date: 2012-09-11 17:46:03 +0000 (Tue, 11 Sep 2012)
New Revision: 19052

Modified:
   pcbsd/current/src-sh/libsh/functions.sh
Log:

Add new functions to libsh



Modified: pcbsd/current/src-sh/libsh/functions.sh
===================================================================
--- pcbsd/current/src-sh/libsh/functions.sh	2012-09-11 17:45:55 UTC (rev 19051)
+++ pcbsd/current/src-sh/libsh/functions.sh	2012-09-11 17:46:03 UTC (rev 19052)
@@ -83,10 +83,140 @@
 
 # Exit with a error message
 exit_err() {
-        echo "ERROR: ${1}"
 	if [ -n "${LOGFILE}" ] ; then
-           echo "ERROR: ${1}" >> ${LOGFILE}
+           echo "ERROR: $*" >> ${LOGFILE}
 	fi
+  	echo >&2 "ERROR: $*"
         exit 1
 }
 
+
+### Print an error on STDERR and bail out
+printerror() {
+  exit_err $*
+}
+
+
+# Check if the target directory is on ZFS
+# Arg1 = The dir to check
+# Arg2 = If set to 1, don't dig down to lower level directory
+isDirZFS() {
+  local _chkDir="$1"
+  while :
+  do
+     # Is this dir a ZFS mount
+     mount | grep -w "on $_chkDir " | grep -qw "(zfs," && return 0
+      
+     # Quit if not walking down
+     if [ "$2" = "1" ] ; then return 1 ; fi
+  
+     if [ "$_chkDir" = "/" ] ; then break ; fi
+     _chkDir=`dirname $_chkDir`
+  done
+  
+  return 1
+}
+
+# Get the ZFS tank name for a directory
+# Arg1 = Directory to check
+getZFSTank() {
+  local _chkDir="$1"
+  while :
+  do
+     line=`mount | grep -w -e $_chkDir -e "(zfs,"`
+     mount | grep -qw -e $_chkDir -e "(zfs,"
+     if [ $? -eq 0 ] ; then
+        echo $line | cut -d '/' -f -1 | awk '{print $1}'
+        return 0
+     fi
+
+     if [ "$_chkDir" = "/" ] ; then return 1 ; fi
+     _chkDir=`dirname $_chkDir`
+  done
+
+  return 1
+}
+
+# Check if an address is IPv6
+isV6() {
+  echo ${1} | grep -q ":"
+  return $?
+}
+    
+# Is a mount point, or any of its parent directories, a symlink?
+is_symlinked_mountpoint()
+{
+        local _dir
+        _dir=$1
+        [ -L "$_dir" ] && return 0
+        [ "$_dir" = "/" ] && return 1
+        is_symlinked_mountpoint `dirname $_dir`
+        return $?
+}
+
+# Function to ask the user to press Return to continue
+rtn()
+{
+  echo -e "Press ENTER to continue\c";
+  read garbage
+};
+
+# Function to check if an IP address passes a basic sanity test
+check_ip()
+{
+  ip="$1"
+  
+  # If this is a V6 address, skip validation for now
+  isV6 "${ip}"
+  if [ $? -eq 0 ] ; then return ; fi
+
+  # Check if we can cut this IP into the right segments 
+  SEG="`echo $ip | cut -d '.' -f 1 2>/dev/null`"
+  echo $SEG | grep -E "^[0-9]+$" >/dev/null 2>/dev/null
+  if [ "$?" != "0" ]
+  then
+     return 1
+  fi
+  if [ $SEG -gt 255 -o $SEG -lt 0 ]
+  then
+     return 1
+  fi
+  
+  # Second segment
+  SEG="`echo $ip | cut -d '.' -f 2 2>/dev/null`"
+  echo $SEG | grep -E "^[0-9]+$" >/dev/null 2>/dev/null
+  if [ "$?" != "0" ]
+  then
+     return 1
+  fi
+  if [ $SEG -gt 255 -o $SEG -lt 0 ]
+  then
+     return 1
+  fi
+
+  # Third segment
+  SEG="`echo $ip | cut -d '.' -f 3 2>/dev/null`"
+  echo $SEG | grep -E "^[0-9]+$" >/dev/null 2>/dev/null
+  if [ "$?" != "0" ]
+  then
+     return 1
+  fi
+  if [ $SEG -gt 255 -o $SEG -lt 0 ]
+  then
+     return 1
+  fi
+  
+  # Fourth segment
+  SEG="`echo $ip | cut -d '.' -f 4 2>/dev/null`"
+  echo $SEG | grep -E "^[0-9]+$" >/dev/null 2>/dev/null
+  if [ "$?" != "0" ]
+  then
+     return 1
+  fi
+  if [ $SEG -gt 255 -o $SEG -lt 0 ]
+  then
+     return 1
+  fi
+
+  return 0
+};



More information about the Commits mailing list