Sample Header Ad - 728x90

SSH for loop: parameter passed to function captured in variable is not expanded

4 votes
2 answers
461 views
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.
Asked by Nickotine (554 rep)
Jan 30, 2024, 05:26 PM
Last activity: Feb 2, 2024, 05:33 PM