#!/bin/bash # This script is for use on the machine where the nightly Expeditee binaries are to be # uploaded. Put this script in the same folder as the uploads are meant to go. # When any binary or its log *.out file is being uploaded via secure shell, this script # will be called. The invocation of this script should go into .ssh/authorizedkeys. # Then this script will delete all other files in this folder that have the same naming # pattern as the uploaded binary/log file, excepting any existing file with the current # date in its name (in case this turns out to be the uploaded file itself). # http://stackoverflow.com/questions/1401482/yyyy-mm-dd-format-date-in-shell-script date=`date +%Y-%m-%d` # code from Puka # the file uploaded is the one with the latest timestamp # ls -rt lists files by time, then get the last one #incoming_file=$1 incoming_file=`ls -rt *$date* 2>/dev/null | tail -n 1` echo $incoming_file # arrays # make sure -linux64 comes before linux, because we'll be matching against these # strings, so need the longest strings to be matched first. bin_suffix=("-linux64" "-linux" "-windows.exe" "-MacOS-intel-MountainLion.dmg") rke_suffix=("-linux64" "-linux" "-windows" "-mac-MountainLion") # http://stackoverflow.com/questions/2172352/in-bash-how-can-i-check-if-a-string-begins-with-some-value # http://stackoverflow.com/questions/229551/string-contains-in-bash rke_file_pattern= if [[ $incoming_file == "rke-"* ]] ; then for suffix in ${rke_suffix[@]}; do if [[ $incoming_file == *"$suffix"* ]]; then rke_file_pattern="rke-*${suffix}.out" break fi done # ONE WAY: #echo "found $rke_file_pattern" # -f to make it silent: will also be silent if there's nothing to rm #rm -f $rke_file_pattern # OR: # get rid of all files of the pattern that are not of the current date # http://stackoverflow.com/questions/10981439/reading-filenames-into-an-array # setting for fileglobbing: # for using wildcards to get matching files on filesystem into array # "The nullglob option causes the array to be empty if there are no matched." # http://stackoverflow.com/questions/10981439/reading-filenames-into-an-array # See shopt command at http://www.gnu.org/software/bash/manual/html_node/The-Shopt-Builtin.html shopt -s nullglob files=($rke_file_pattern) for file in ${files[@]}; do # set nullglob earlier, so won't be here if there's no file that matched if [[ $file != "rke-$date${suffix}.out" ]]; then rm -rf $file fi done shopt -u nullglob fi # binaries start with "Expeditee-1-" bin_file_pattern= if [[ $incoming_file == "Expeditee-"* ]] ; then for suffix in ${bin_suffix[@]}; do if [[ $incoming_file == *"$suffix" ]]; then bin_file_pattern="Expeditee-1-*$suffix" break fi done # ONE WAY: #echo "found $bin_file_pattern" # -f to make it silent: will also be silent if there's nothing to rm #rm -rf $bin_file_pattern # OR: # get rid of all files of the pattern that are not of the current date # http://stackoverflow.com/questions/10981439/reading-filenames-into-an-array shopt -s nullglob files=($bin_file_pattern) for file in ${files[@]}; do # set nullglob earlier, so won't be here if there's no file that matched if [[ $file != "Expeditee-1-$date$suffix" ]]; then echo "$file" rm -rf $file fi done shopt -u nullglob fi # clean up ._Expeditee-1-YYYY-MM-DD-MacOS-intel-MountainLion.dmg temp files #ls -la "._Expeditee*dmg" rm -rf "._Expeditee*dmg"