# /etc/functions-system # # a collection of functions and variables for scripts source /etc/shell-colors # removable backup disk is a SATA drive, listed in the smartctl database DRIVEMODEL="WD7500AAKS" SMARTPARAMS="-a -d ata -o on -S on -n standby" Is_Backup_Drive_Available () { BACKUPDISK="" BACKUP_AVAIL="`lsscsi | grep $DRIVEMODEL | awk '{print$7}'`" if [ -n "$BACKUP_AVAIL" ]; then BACKUPDISK=$BACKUP_AVAIL fi } Script_Home () # this function determines a script's home directory from which it was launched { me=$0 # check if we are in the script directory if [ "x${me:0:2}" = "x./" ]; then me="$(pwd)${me:1}" fi # echo "Script name: $me" # echo "Script's home directory: $(dirname $me)" } Wait_For_Response () { if [ "$1" = "" ] ; then printf "${BOLDRED}Warning:${COLOR_RESET}\nNo question text was passed to the ${BOLDWHITE}/etc/system-functions/Wait_For_Response${COLOR_RESET} function. Exiting.\n" exit 1 fi allowedchars="YyNn" response= while ! [[ "${response:0:1}" =~ "[$allowedchars]" ]] ; do read -er -p "$1 (y/n): " response done } Proceed_From_Response () { if [ "$response" = "n" -o "$response" = "N" ] ; then echo echo -e "${BOLDYELLOW}Exiting.${COLOR_RESET}" echo exit 0 else echo echo "Continuing." echo fi } Interactive_Shell () { # Warning: The detection of $PS1 works only with a script declaration of !/bin/bash # and does not work with !/bin/sh. A declaration of !/bin/sh is for a Bourne shell # and not a Bourne-Again (bash) shell. Hence, the bash startup scripts are not read # when a script starts with the !/bin/sh declaration. # Using the $- variable is supposed to be more portable among different shells but not always. case "$-" in *i*) # manually running this script in interactive mode INTERACTIVE="true" ;; *) # running in non-interactive mode INTERACTIVE="false" ;; esac # The $- detection might be correct or might have failed. Next try the $PS1 method. if [ "$INTERACTIVE" = "false" ]; then if [ -z "$PS1" ]; then INTERACTIVE="false" else INTERACTIVE="true" fi fi # If the first two methods failed then try the $TERM variable. That variable will be # available in a console or terminal window only when a script is run directly from # the command line. Caveat: the at daemon assigns a value of "dumb" to the $TERM # variable. The variable will not appear in the script's environment variable list, # but a null value cannot be used as a test. if [ "$INTERACTIVE" = "false" ]; then if [ "$TERM" = "linux" ] || [ "$TERM" = "xterm" ]; then INTERACTIVE="true" else INTERACTIVE="false" fi fi # One last test. Use the tty command to determine if the script is being run directly # in a console or terminal. if [ "$INTERACTIVE" = "false" ]; then if $(tty -s) ; then INTERACTIVE="true" else INTERACTIVE="false" fi fi } Test_Directory () { if [ -z $1 ] ; then echo "No parameter was passed to the Test_Directory function. Exiting." exit 1 fi ls $1 &>/dev/null if [ "$?" != "0" ] ; then echo echo -e "The ${BOLDGREEN}$1${COLOR_RESET} directory does not exist. Exiting." echo exit 1 else echo "Okay." echo fi } mount_local () { if [ -n "$1" ]; then # NFS share points might also be listed in fstab. Use /dev as a criterion. MOUNT_POINT="`grep $1 /etc/fstab | grep '^\#' | grep '\/dev\/' | sed 's/\#[ ]*//'`" if [ -n "$MOUNT_POINT" ]; then FILETYPE="`echo $MOUNT_POINT | awk '{print $3}'`" PARTITION="`echo $MOUNT_POINT | awk '{print $1}'`" LOCALDIR="`echo $MOUNT_POINT | awk '{print $2}'`" OPTIONS="`echo $MOUNT_POINT | awk '{print $4}'`" # Is the partition already mounted? IS_MOUNTED="`grep $PARTITION /etc/mtab`" if [ -z "$IS_MOUNTED" ]; then # Local partitions might not be mounted often --- run fsck. /sbin/fsck -C -a $PARTITION echo "mount -t $FILETYPE $PARTITION $LOCALDIR -o $OPTIONS" mount -t $FILETYPE $PARTITION $LOCALDIR -o $OPTIONS else echo -e "${BOLDYELLOW}Seems $PARTITION is already mounted!${COLOR_RESET}" echo $IS_MOUNTED fi else echo -e "${BOLDWHITE}$0:${BOLDRED} No 'commented' ${BOLDGREEN}${1}${BOLDRED} partition found in /etc/fstab.${COLOR_RESET}" fi else echo "No mount point provided to mount_local in $0." fi } mount_remote () { if [ -n "$1" ]; then OPTIONS="defaults,soft,intr,bg,timeo=5,retry=1,rsize=32768,wsize=32768,noatime" # Is the partition already mounted? IS_MOUNTED="`grep $1 /etc/mtab`" if [ -z "$IS_MOUNTED" ]; then echo "mount -t nfs $NFS_SERVER:$1 $1" mount -t nfs $NFS_SERVER:$1 $1 -o $OPTIONS else echo -e "${BOLDYELLOW}Seems $1 is already mounted!${COLOR_RESET}" echo $IS_MOUNTED fi else echo "No mount point provided to mount_remote in $0." fi }