I have several servers which have several files with deploy date info I need to parse, and get a local copy of the files which are 2 months old or older.
#!/bin/bash
# split on new line not space
# don't want to quote everything
# globbing desired
IFS=$'\n'
servers=(
blue
red
)
parseDate() {
grep 'deploy_date:' $file | \
sed ... | \
# much more in here...
}
getOldFiles() {
local #all vars
currentDateInSecs=$(date +%s)
twoMonthsInSecs=526000
for server in ${servers[@]}; do
oldfile=$(
ssh $server "
$(declare -f parseDate)
for file in /dir/$server/file*; do
dateInSecs=$(date -jf '%b/%e/%Y' $(parseDate) +%s)
timeDiff=$((\$currentDateInSecs - \$dateInSecs))
((\$timeDiff >= \$twoMonthsInSecs)) &&
cat \$file
done
"
)
[ -n $oldFile ] &&
cat $oldFile ~/oldFiles/${server}-${file}.txt
done
Problem:
Current issue is with dateInSecs=$(date -jf '%b/%e/%Y' $(parseDate \$file) +%s)
.
When parseDate \$file
is in $()
the $file
variable is not expanded, it works fine without command substitution, but I need it.
How do I fix this?
Info:
This isn't a script, they're in my ~/.bash_profile
This is a script (among other scripts) in a git repo which is sourced from ~/bash_profile
(I have an install script which sets sources using $PWD
) so people can use these commands directly instead of cd'ing to the git repo (which has many other things not applicable to the them).
Runs from Macos to CentOS servers.
date
on the CentOS machine won't have it, most likely. That will be the next problem. Nickotine, are you sure yourdate -jf '%b/%e/%Y'
command works when not in the$()
? Have you installed BSDdate
on the CentOS server?fn() { echo thing; }; ssh remoteHost "$(declare -f fn); fn"
. Wild!