Last active
July 27, 2022 15:38
-
-
Save rdmarsh/e60f8de3da853c493518 to your computer and use it in GitHub Desktop.
standard shell script functions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
######################################## | |
### | |
### This code is under version control | |
### See the following link before making changes | |
### https://gist.github.com/rdmarsh/e60f8de3da853c493518 | |
### | |
######################################## | |
######################################## | |
### | |
### Description of this code | |
### | |
######################################## | |
######################################## | |
### | |
### Exit codes | |
### | |
### 0: normal exit | |
### 63: exit code not set | |
### 127: internal script error or other binary problem | |
### | |
######################################## | |
######################################## | |
### | |
### 19990909:rdmarsh:what changed | |
### | |
######################################## | |
######################################## | |
### collect command line var before 'sets' | |
xfer_type="${1}" | |
xfer_site="${2}" | |
######################################## | |
### fail if errors or unset vars | |
set -e #exit immediately if a simple command exits with a non-zero status | |
set -u #treat unset variables as an error when performing parameter expansion | |
######################################## | |
### | |
### global vars | |
### | |
######################################## | |
#scriptname (#strips off everything before the last "/") | |
scriptname="${0##*/}" | |
#projectname | |
projectname="${scriptname%.*}" | |
#dirname scripname resides (#strips off everything after the last "/") | |
dirname="${0%/*}" | |
#grab pid for log file | |
PID="$$" | |
#find our current host | |
fqdn="$(/bin/hostname)" | |
#strip off everything after the first "." | |
host="${fqdn%%.*}" | |
#keep same date across script | |
datetime="$(/bin/date "+%Y%m%dT%H%M%S")" | |
#set the no string | |
nostr="$(locale nostr)" | |
#who's running this show? | |
whoami="$(/usr/bin/whoami)" | |
#who's expected to run script | |
correct_user="root" | |
#umask | |
umask 022 | |
#temp dir | |
tempdir="$(/bin/mktemp -d "${projectname}.XXXXXXXXXX")" | |
#lock file | |
lockfile="/var/lock/${projectname}.lock" | |
#be more verbose | |
verbose=0 #set to 0 to be verbose, 1 to not be verbose | |
#todo trap | |
#trap 'rm -rf "$TMPDIR"' EXIT INT HUP TERM | |
#allowed command line args, pipe delim | |
allowed_types='prod|test' | |
allowed_sites='sitea|siteb' | |
######################################## | |
### | |
### script functions | |
### | |
######################################## | |
######################################## | |
### generic functions | |
_log() { | |
#write to the standard log file | |
local _msg="${1:-'no message provided'}" | |
if ! /usr/bin/logger -i -t "${scriptname} (${PID})" -- "${_msg}" ; then | |
echo "problem with ${scriptname} script on ${host}: can't write to log. non-fatal, continuing (message was: ${_msg})" | |
fi | |
} | |
_pipe_to_log() { | |
#input for this func is pipe. | |
#we do this so we can pass files and comand output, | |
# other methods remove the crlfs. uses read to loop through the piped lines | |
while read _msg ; do | |
if ! /usr/bin/logger -i -t "${scriptname} (${PID})" -- "${_msg}" ; then | |
echo "problem with ${scriptname} script on ${host}: can't write to log. non-fatal, continuing (message was: ${_msg})" | |
fi | |
((verbose)) || echo "${_msg}" | |
done | |
return 0 | |
} | |
_verbose_log() { | |
#write to the standard log file as well as to stdout if $verbose is set | |
local _msg="${1:-'no message provided'}" | |
((verbose)) || echo "${_msg}" | |
_log "${_msg}" | |
} | |
_log_file_contents() { | |
#logs contents of file | |
local _file="${1:?'file name not supplied'}" | |
if _can_read_file "${_file}" ; then | |
_log "file ${_file} content follows" | |
_log "-----" | |
#useless use of cat, find a better way | |
cat "${_file}" | _pipe_to_log | |
_log "-----" | |
return 0 | |
else | |
_log "file ${_file} content cannot be logged" | |
return 1 | |
fi | |
} | |
_log_file_md5sum() { | |
#logs md5sum of file | |
local _file="${1:?'file name not supplied'}" | |
if _can_read_file "${_file}" ; then | |
_log "file ${_file} md5sum follows" | |
#should error check this | |
/usr/bin/md5sum "${_file}" | _pipe_to_log | |
return 0 | |
else | |
_log "file ${_file} md5sum cannot be logged" | |
return 1 | |
fi | |
} | |
_start() { | |
#make a start message | |
_log "starting script ${scriptname} on ${host} as ${whoami}" | |
return 0 | |
} | |
_finish() { | |
#make a finish message | |
_log "finished script ${scriptname} on ${host} as ${whoami}" | |
return 0 | |
} | |
_abort() { | |
#put an aborted message into the log if failed | |
#todo put a trap here | |
local _sts="${1:-63}" | |
local _msg="${2:-'no message provided'}" | |
_log "aborted msg ${_msg}" | |
_log "aborted script ${scriptname} on ${host} with exit code ${_sts}" | |
if ! _remove_lock "${lockfile}" ; then | |
_log "lockfile cant or wont attempt to remove" || exit 127 | |
fi | |
#remove temp file if any? | |
rm -rf -- "${tempdir}" 2>/dev/null | |
exit "${_sts}" | |
} | |
_correct_types() { | |
#enable extglob | |
shopt -s extglob | |
local _types="+(${allowed_types})" | |
#check a correct type has been passed | |
case "${xfer_type}" in | |
${_types}) | |
#disable extglob | |
shopt -u extglob | |
return 0 | |
;; | |
*) | |
#disable extglob | |
shopt -u extglob | |
return 1 | |
;; | |
esac | |
} | |
_correct_sites() { | |
#enable extglob | |
shopt -s extglob | |
local _sites="+(${allowed_sites})" | |
#check a correct type has been passed | |
case "${xfer_site}" in | |
${_sites}) | |
#disable extglob | |
shopt -u extglob | |
return 0 | |
;; | |
*) | |
#disable extglob | |
shopt -u extglob | |
return 1 | |
;; | |
esac | |
} | |
_print_usage() { | |
#print a message if arg is not correct | |
_verbose_log "Usage: ${scriptname} [${allowed_types}] [${allowed_sites}]" | |
return 0 | |
} | |
_print_version() { | |
### print the revision number | |
local _version="$(/bin/grep -Eo "^### [0-9]{8}:" ${0} | /usr/bin/cut -d: -f1 | /bin/sed -e 's/^### //g' | /usr/bin/tail -1)" | |
echo "${scriptname} - version ${_version}" | |
return 0 | |
} | |
_confirm() { | |
local _qst="${1:="Are you sure [${nostr}]"}" | |
read -er -p "${_qst}: " response | |
if printf "%s\n" "${response}" | grep -Eq "$(locale yesexpr)" ; then | |
return 0 | |
else | |
return 1 | |
fi | |
} | |
_correct_user() { | |
#make sure we're running as correct user | |
if [[ "${whoami}" == "${correct_user}" ]] ; then | |
return 0 | |
else | |
_log "not correct user: am ${whoami}, expect ${correct_user}" | |
return 1 | |
fi | |
} | |
_file_exists() { | |
#check a file exists | |
local _file="${1:?'file name not supplied'}" | |
if [[ -f "${_file}" ]] ; then | |
return 0 | |
else | |
_log "file ${_file} does not exist" | |
return 1 | |
fi | |
} | |
_file_not_empty() { | |
#check a file has contents | |
local _file="${1:?'file name not supplied'}" | |
if _file_exists "${_file}" && [[ -s "${_file}" ]] ; then | |
return 0 | |
else | |
_log "file ${_file} is empty" | |
return 1 | |
fi | |
} | |
_can_read_file() { | |
#check if we can read the contents of a file | |
local _file="${1:?'file name not supplied'}" | |
if _file_exists "${_file}" && [[ -r "${_file}" ]] ; then | |
return 0 | |
else | |
_log "file ${_file} is not readable" | |
return 1 | |
fi | |
} | |
_file_empty() { | |
local _file="${1:?'file name not supplied'}" | |
if _file_exists "${_file}" && [[ ! -s "${_file}" ]] ; then | |
return 0 | |
else | |
_log "file ${_file} is not empty (or doesn't exist)" | |
return 1 | |
fi | |
} | |
_own_file() { | |
#checks files owner and group is us | |
local _file="${1:?'file name not supplied'}" | |
if _file_exists "${_file}" && [[ -O "${_file}" && -G "${_file}" ]] ; then | |
return 0 | |
else | |
_log "file ${_file} does not match uid or gid of this process" | |
return 1 | |
fi | |
} | |
_file_younger_than() { | |
local _time="${1:?'age in mins not supplied'}" | |
local _file="${2:?'file name not supplied'}" | |
if _file_exists "${_file}" ; then | |
#find file age in minutes | |
local _fileage="$((($(/bin/date +%s) - $(/usr/bin/stat -c '%Y' "${_file}"))/60))" | |
_log "file ${_file} is ${_fileage} mins old" | |
if (( ${_fileage} < ${_time} )) ; then | |
return 0 | |
else | |
_log "file ${_file} not younger than ${_time}" | |
return 1 | |
fi | |
else | |
_log "file ${_file} age cannot be determined" | |
return 1 | |
fi | |
} | |
_file_smaller_than() { | |
local _size="${1:?'size in KB not supplied'}" | |
local _file="${2:?'file name not supplied'}" | |
if _file_exists "${_file}" ; then | |
#find file size in KB | |
local _filesize="$(/usr/bin/du --apparent-size --block-size=1K "${_file}" | /bin/cut -f1)" | |
_log "file ${_file} is ${_filesize} KB big" | |
if (( ${_filesize} < ${_size} )) ; then | |
return 0 | |
else | |
_log "file ${_file} not smaller than ${_size}" | |
return 1 | |
fi | |
else | |
_log "file ${_file} size cannot be determined" | |
fi | |
} | |
_dir_exists() { | |
#check if a dir exists | |
local _dir="${1:?'dir name not supplied'}" | |
if [[ -d "${_dir}" ]] ; then | |
return 0 | |
else | |
return 1 | |
fi | |
} | |
_dir_has_files() { | |
#check if a dir has (non-hidden) files | |
local _dir="${1:?'dir name not supplied'}" | |
if _dir_exists "${_dir}" ; then | |
if [ -n "$(ls "${_dir}")" ] ; then | |
return 0 | |
else | |
return 1 | |
fi | |
else | |
return 1 | |
fi | |
} | |
_make_dir() { | |
#make a dir and check it | |
local _dir="${1:?'dir name not supplied'}" | |
if mkdir -p "${_dir}" ; then | |
if _dir_exists "${_dir}" ; then | |
return 0 | |
else | |
return 1 | |
fi | |
else | |
return 1 | |
fi | |
} | |
_check_connection_to() { | |
local _host="${1:?'host name not supplied'}" | |
if ping -c1 ${host} > /dev/null 2>&1 ; then | |
return 0 | |
else | |
return 1 | |
fi | |
} | |
_pause() { | |
read -p "Press [enter] to continue..." | |
return 0 | |
} | |
_create_check_file() { | |
local _file="${1:?file not set}" | |
local _check_dir="${2:?check file not set}" | |
local _check_file="${_check_dir}/${_file##*/}.chk" | |
if ! /bin/date "+%Y%m%dT%H%M%S" >> "${_check_file}" ; then | |
_log "could not write date to ${_check_file}" | |
rm -rf "${_check_file}" 2>/dev/null | |
return 1 | |
fi | |
if ! /usr/bin/md5sum "${path_filename}" >> "${_check_file}" ; then | |
_log "could not write md5sum to ${_check_file}" | |
rm -rf "${_check_file}" 2>/dev/null | |
return 1 | |
fi | |
} | |
_diff_in_days() { | |
### work out diff in days between two dates | |
local _diff | |
local _date_one="${1:?date_one not parsed}" | |
local _date_two="${2:?date_two not parsed}" | |
echo $(( ( $(/bin/date +%s -d "${_date_one}") - $(/bin/date +%s -d "${_date_two}" ) ) / 86400 )) | |
#not sure how to check exit codes for above, will just have to trust it to work | |
return 0 | |
} | |
_error_file_to_dir() { | |
local _full_file="${1:?full file not set}" | |
local _error_dir="${2:?error dir not set}" | |
local _reason="${3:-'reason not given'}" | |
local _file="${_full_file##*/}" #find filename name without fullpath | |
if mv "${_full_file}" "${_error_dir}/${_file}.${datetime}" ; then | |
echo "${_reason}" > "${_error_dir}/${_file}.${datetime}.error" | |
return 0 | |
else | |
return 1 | |
fi | |
} | |
_xferd_file_to_dir() { | |
local _full_file="${1:?full file not set}" | |
local _xferd_dir="${2:?error dir not set}" | |
local _file="${_full_file##*/}" #find filename name without fullpath | |
if mv "${_full_file}" "${_xferd_dir}/${_file}.${datetime}" ; then | |
return 0 | |
else | |
return 1 | |
fi | |
} | |
######################################## | |
### lock specific functions | |
_create_lock() { | |
#lockfile with our PID | |
local _lockfile="${1:?'lockfile name not supplied'}" | |
if /usr/bin/lockfile -r 0 "${_lockfile}" ; then | |
_log "lockfile created" | |
if ! /bin/chown "${whoami}" "${_lockfile}" ; then | |
_log "lockfile cant chown file" | |
return 1 | |
fi | |
if ! /bin/chmod 644 "${_lockfile}" ; then | |
_log "lockfile cant chmod 644 file" | |
return 1 | |
fi | |
#we have to overwrite the file as the lockfile command puts a 0 in there | |
echo "${PID}" > "${_lockfile}" | |
{ | |
echo "${scriptname}" | |
echo | |
echo "Locked on: ${datetime}" | |
echo "Locked by: ${whoami}" | |
echo "Called by: ${scriptname}" | |
echo | |
} >> "${_lockfile}" | |
if ! /bin/chmod 444 "${_lockfile}" ; then | |
_log "lockfile cant chmod 444 file" | |
return 1 | |
fi | |
return 0 | |
else | |
_log "lockfile cant create or already present" | |
_log_file_contents "${_lockfile}" || true | |
return 1 | |
fi | |
} | |
_my_lockfile() { | |
#we created the lockfile on this run | |
local _lockfile="${1:?'lockfile name not supplied'}" | |
if _file_exists "${_lockfile}" ; then | |
if _own_file "${_lockfile}" && /bin/grep -q "^${PID}$" "${_lockfile}" ; then | |
_log "lockfile exists and is ours, created on this run" | |
return 0 | |
else | |
_log "lockfile exists but wasn't created on this run" | |
_log_file_contents "${_lockfile}" || true | |
return 1 | |
fi | |
else | |
_log "lockfile has disappeared or never existed" | |
return 1 | |
fi | |
} | |
_remove_lock() { | |
#remove the lock file, only if it has our pid | |
#again make sure we dont remove a lockfile we're not meant to | |
local _lockfile="${1:?'lockfile name not supplied'}" | |
if _my_lockfile "${_lockfile}" ; then | |
#the -f means we always return 0 if file present or not, mostly pointless | |
if /bin/rm -f -- "${_lockfile}" ; then | |
_log "lockfile removed" | |
return 0 | |
else | |
_log "lockfile cant be removed" | |
return 1 | |
fi | |
else | |
_log "lockfile not attempted to be removed" | |
return 1 | |
fi | |
} | |
######################################## | |
### smb functions | |
_run_smb_cmd() { | |
#store all the output in temp file so we can log | |
local _smb_auth="${1:?smb auth not set}" | |
local _smb_serv="${2:?smb serv not set}" | |
local _cmd="${3:?smb cmd not set}" | |
local _wait_time='1' #start at 1 second | |
_templog="$(/bin/mktemp)" | |
trap "rm -f \"${_templog}\"; _abort 12 \"trap called inside run_smb_cmd\"" INT TERM EXIT | |
_log "smb cmd: ${_cmd}" | |
until ${smb_prog} --authentication-file=${_smb_auth} ${_smb_serv} --command "${_cmd}" > "${_templog}" 2>&1 || [[ ${_wait_time} -eq 4 ]]; do | |
_log "smb attempt ${_wait_time}" | |
_log_file_contents "${_templog}" | |
sleep $(( _wait_time++ )) | |
done | |
if [[ ${_wait_time} -eq 4 ]] ; then | |
_log "gave up on smb cmd '${_cmd}' after ${_wait_time} attempts:" | |
_log_file_contents "${_templog}" | |
rm -f -- "${_templog}" 2>/dev/null | |
return 1 | |
fi | |
_log_file_contents "${_templog}" | |
rm -f -- "${_templog}" 2>/dev/null | |
trap - INT TERM EXIT | |
return 0 | |
} | |
_transfer_file_from_smb() { | |
#this function copies a file from smb dir to local dir | |
local _smb_auth="${1:?smb auth not set}" | |
local _smb_serv="${2:?smb serv not set}" | |
local _smb_dir="${3:?smb dir not set}" | |
local _lcd_dir="${4:?lcd dir not set}" | |
local _file="${5:?file not set}" | |
if _run_smb_cmd "${_smb_auth}" "${_smb_serv}" "prompt; cd \"${_smb_dir}\"; lcd \"${_lcd_dir}\" ; mget \"${_file}\"" ; then | |
return 0 | |
else | |
return 1 | |
fi | |
} | |
_transfer_file_to_smb() { | |
#this function copies a file from smb dir to local dir | |
local _smb_auth="${1:?smb auth not set}" | |
local _smb_serv="${2:?smb serv not set}" | |
local _smb_dir="${3:?smb dir not set}" | |
local _lcd_dir="${4:?lcd dir not set}" | |
local _file="${5:?file not set}" | |
#normally we wont use ext, so default to empty is unset | |
#we do this so we dont trigger errors for unset vars | |
local _ext="${6:=""}" | |
if _run_smb_cmd "${_smb_auth}" "${_smb_serv}" "prompt; cd \"${_smb_dir}\"; lcd \"${_lcd_dir}\" ; put \"${_file}\" \"${_file}${_ext}\"" ; then | |
return 0 | |
else | |
return 1 | |
fi | |
} | |
_move_file_to_dir_on_smb() { | |
#expects short filename to be passed | |
local _smb_auth="${1:?smb auth not set}" | |
local _smb_serv="${2:?smb serv not set}" | |
local _old_dir="${3:?smb dir not set}" | |
local _new_dir="${4:?lcd dir not set}" | |
local _file="${5:?file not set}" | |
if _run_smb_cmd "${_smb_auth}" "${_smb_serv}" "prompt; cd \"${_old_dir}\"; rename \"${_file}\" \"${_new_dir}\\${_file}.${datetime}\"" ; then | |
return 0 | |
else | |
return 1 | |
fi | |
} | |
_rename_file_on_smb() { | |
#expects short filename to be passed | |
local _smb_auth="${1:?smb auth not set}" | |
local _smb_serv="${2:?smb serv not set}" | |
local _smb_dir="${3:?smb dir not set}" | |
local _old_file="${4:?old file not set}" | |
local _new_file="${5:?new file not set}" | |
if _run_smb_cmd "${_smb_auth}" "${_smb_serv}" "prompt; cd \"${_smb_dir}\"; rename \"${_old_file}\" \"${_new_file}\"" ; then | |
return 0 | |
else | |
return 1 | |
fi | |
} | |
_delete_file_on_smb() { | |
#expects short filename to be passed | |
local _smb_auth="${1:?smb auth not set}" | |
local _smb_serv="${2:?smb serv not set}" | |
local _smb_dir="${3:?smb dir not set}" | |
local _file="${4:?file not set}" | |
if _run_smb_cmd "${_smb_auth}" "${_smb_serv}" "prompt; cd \"${_smb_dir}\"; del \"${_file}\"" ; then | |
return 0 | |
else | |
return 1 | |
fi | |
} | |
_mirror_files_to_smb() { | |
local _smb_auth="${1:?smb auth not set}" | |
local _smb_serv="${2:?smb serv not set}" | |
local _lcd_dir="${3:?smb dir not set}" | |
local _smb_dir="${3:?smb dir not set}" | |
if _run_smb_cmd "${_smb_auth}" "${_smb_serv}" "prompt ; lcd \"${_lcd_dir}\" ; cd \"${_smb_dir}\" ; tarmode ; recurse ; mput * \"" ; then | |
return 0 | |
else | |
return 1 | |
fi | |
} | |
######################################## | |
### xml functions | |
_check_xml_file() { | |
# this function is used to validate xml files for correctness | |
#use full path for file | |
local _file="${1:?file not set}" | |
#run through validator | |
#xmlwf is crap, but it's good enough for this | |
#xml exits with a 0 no matter what we do (man xmlwf) | |
#good files don't write anything to STDOUT | |
#bad files write to STDERR, but we redirect it STDOUT and check with 'wc -l' | |
if _file_exists "${_file}" ; then | |
[[ $(/usr/bin/xmlwf "${_file}" 2>&1 | wc -l) == 0 ]] | |
else | |
return 1 | |
fi | |
} | |
######################################## | |
### ftp functions | |
_run_ftp_cmd() { | |
local _ftp_user="${1:?ftp user not set}" | |
local _ftp_host="${2:?ftp serv not set}" | |
local _cmd="${3:?ftp cmd not set}" | |
_tempbat="$(/bin/mktemp)" | |
_templog="$(/bin/mktemp)" | |
trap "rm -f \"${_tempbat}\" ; rm -f \"${_templog}\" ; _abort 12 \"trap called inside run_ftp_cmd\"" INT TERM EXIT | |
_log "ftp cmd: ${_cmd}" | |
echo "${_cmd}" | tr ";" "\n" >> "${_tempbat}" | |
${ftp_prog} -o ConnectTimeout="${ftp_timeout:-60}" -o ConnectionAttempts="${ftp_attempts:-1}" -b "${_tempbat}" ${_ftp_user}@${_ftp_host} 2>&1 | _pipe_to_log | |
if [[ ${PIPESTATUS[0]} -ne '0' ]] ; then | |
_log "gave up on ftp cmd '${_cmd}':" | |
_log_file_contents "${_tempbat}" | |
_log_file_contents "${_templog}" | |
_log "no files or cant ftp files to/from ${_ftp_host}" | |
rm -f -- "${_tempbat}" 2>/dev/null | |
rm -f -- "${_templog}" 2>/dev/null | |
trap - INT TERM EXIT | |
return 1 | |
fi | |
_log_file_contents "${_tempbat}" | |
_log_file_contents "${_templog}" | |
rm -f -- "${_tempbat}" 2>/dev/null | |
rm -f -- "${_templog}" 2>/dev/null | |
trap - INT TERM EXIT | |
return 0 | |
} | |
_transfer_file_from_ftp() { | |
local _ftp_user="${1:?ftp user not set}" | |
local _ftp_serv="${2:?ftp serv not set}" | |
local _ftp_dir="${3:?ftp dir not set}" | |
local _lcd_dir="${4:?lcd dir not set}" | |
set -f | |
local _file="${5:?file not set}" | |
set +f | |
set -f | |
if _run_ftp_cmd "${_ftp_user}" "${_ftp_serv}" "lcd ${_lcd_dir} ; cd ${_ftp_dir} ; ls -la ; mget ${_file}" ; then | |
set +f | |
return 0 | |
else | |
set +f | |
return 1 | |
fi | |
} | |
_transfer_file_to_ftp() { | |
local _ftp_user="${1:?ftp user not set}" | |
local _ftp_serv="${2:?ftp serv not set}" | |
local _ftp_dir="${3:?ftp dir not set}" | |
local _lcd_dir="${4:?lcd dir not set}" | |
local _file="${5:?file not set}" | |
if _run_ftp_cmd "${_ftp_user}" "${_ftp_serv}" "lcd ${_lcd_dir} ; cd ${_ftp_dir} ; ls -la ; put ${_file} ${_file}.${datetime} ; ls -la" ; then | |
return 0 | |
else | |
return 1 | |
fi | |
} | |
_delete_file_on_ftp() { | |
local _ftp_user="${1:?ftp user not set}" | |
local _ftp_serv="${2:?ftp serv not set}" | |
local _ftp_dir="${3:?ftp dir not set}" | |
local _lcd_dir="${4:?lcd dir not set}" | |
local _file="${5:?file not set}" | |
if _run_ftp_cmd "${_ftp_user}" "${_ftp_serv}" "cd ${_ftp_dir} ; ls -la ; rm ${_file} ; ls -la " ; then | |
return 0 | |
else | |
return 1 | |
fi | |
} | |
######################################## | |
### script specific functions | |
_set_variables() { | |
### set variables | |
#local dirs and files | |
home_dir="/home/${correct_user}" | |
project_dir="${home_dir}/${projectname}" | |
#ftp settings | |
ftp_prog='/usr/bin/sftp' | |
ftp_timeout='25' #in seconds | |
ftp_attempts='2' | |
ftp_serv='ftpserver' | |
ftp_user='ftpuser' | |
stage_dir="${project_dir}/${xfer_type}/${xfer_dir}/stage" | |
xferd_dir="${project_dir}/${xfer_type}/${xfer_dir}/xferd" | |
error_dir="${project_dir}/${xfer_type}/${xfer_dir}/error" | |
case "${xfer_type}" in | |
prod) | |
ftp_dir='ftpdirprod' | |
;; | |
test) | |
ftp_dir='ftpdirtest' | |
;; | |
*) | |
_print_usage | |
return 1 | |
;; | |
esac | |
case "${xfer_site}" in | |
sitea) | |
ftp_dir="${ftp_dir}/ftpdirsitea" | |
;; | |
siteb) | |
ftp_dir="${ftp_dir}/ftpdirsiteb" | |
;; | |
*) | |
_print_usage | |
return 1 | |
;; | |
esac | |
set -f #protect the asterix | |
ftp_file='*.[xX][mM][lL]' | |
set +f | |
lockfile='/var/lock/somelock.lock' | |
min_start='1000' | |
max_start='9999' | |
return 0 | |
} | |
_log_variables() { | |
### log variables used, probably overkill | |
### but it might be useful for troubleshooting | |
_log "variables set as follows..." | |
_log "allowed_types : ${allowed_types}" | |
_log "allowed_sites : ${allowed_sites}" | |
_log "xfer_type : ${xfer_type}" | |
_log "xfer_site : ${xfer_site}" | |
_log "xfer_dir : ${xfer_dir}" | |
_log "whoami : ${whoami}" | |
_log "correct_user : ${correct_user}" | |
_log "home_dir : ${home_dir}" | |
_log "project_dir : ${project_dir}" | |
_log "lockfile : ${lockfile}" | |
_log "stage_dir : ${stage_dir}" | |
_log "xferd_dir : ${xferd_dir}" | |
_log "error_dir : ${error_dir}" | |
_log "ftp_prog : ${ftp_prog}" | |
_log "ftp_timeout : ${ftp_timeout:=60}" | |
_log "ftp_attempts : ${ftp_attempts:=1}" | |
_log "ftp_serv : ${ftp_serv}" | |
_log "ftp_user : ${ftp_user}" | |
_log "ftp_dir : ${ftp_dir}" | |
set -f | |
_log "ftp_file : ${ftp_file}" | |
set +f | |
_log "lockfile : ${lockfile}" | |
_log "min_start: ${min_start}" | |
_log "max_start: ${max_start}" | |
return 0 | |
} | |
_ask_start_number() { | |
read -er -p "File to start from [xxxx]: " file_start | |
if [[ "${file_start}" =~ ^[0-9]+$ ]] && (( file_start >= min_start && file_start <= max_start )) ; then | |
return 0 | |
else | |
return 1 | |
fi | |
} | |
######################################## | |
### | |
### main | |
### | |
######################################## | |
#main logic goes here | |
_start || exit 127 #write a start msg to the log | |
if ! _correct_types ; then | |
_print_usage | |
_abort 2 "not correct command line args" || exit 127 | |
fi | |
if ! _correct_sites ; then | |
_print_usage | |
_abort 2 "not correct command line args" || exit 127 | |
fi | |
if ! _set_variables ; then | |
_abort 3 "cant set variables" || exit 127 | |
fi | |
if ! _log_variables ; then | |
_abort 4 "cant record variables to system log" || exit 127 | |
fi | |
((verbose)) || echo "This script will do something" | |
if ! _correct_user ; then | |
_abort 5 "current user ${whoami} is not correct user ${correct_user}" || exit 127 | |
fi | |
#check and build our needed dirs | |
for _dir in "${home_dir}" "${project_dir}" "${stage_dir}" "${xferd_dir}" "${error_dir}" ; do | |
if ! _make_dir "${_dir}" ; then | |
_abort 7 "dir ${_dir} creation failed or is missing" || exit 127 | |
fi | |
done | |
if _ask_start_number ; then | |
_verbose_log "start processing from ${file_start}" | |
else | |
_verbose_log "number to start from must be between ${min_start} and ${max_start}" | |
_abort 4 || exit 127 | |
fi | |
if ! _confirm ; then | |
_abort 5 "user chose not to run" || exit 127 | |
fi | |
if ! _create_lock "${lockfile}" ; then | |
_abort 8 "cant create lockfile ${lockfile}"|| exit 127 | |
fi | |
### guts go here | |
if ! _remove_lock "${lockfile}" ; then | |
_abort 9 "cant remove lockfile ${lockfile}" || exit 127 | |
fi | |
#remove temp file if any? | |
rm -rf -- "${tempdir}" 2>/dev/null | |
((verbose)) || echo "Finished doing something" | |
_finish || exit 127 #write a finish msg to the log | |
######################################## | |
### | |
### end | |
### | |
######################################## |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment