I am having a script is being run from server1 that has the duty to copy files from server2 (which will be parameter ${Server}) to server3 based on a specific parameters using the scp command:
scp -p -qo StrictHostKeyChecking=no user@${Server}:/path/to/source/files/*${EAPdate}* user@hostname3:/path/to/destination/files/
The script itself should create a log when it is being run. If everything is working as expected and the EAPdate parameter is being typed as it should (YYYYMMDD), the copy command works as expected.
But if the parameter is typed as (DDMMYYYY) the copy command will fail with No such file or directory error.
The script is the following:
#!/bin/bash
configuration_file=copy_EAP_files.ini
hostname=$(hostname)
script=$(basename "$0")
start_time=$(date)
start_seconds=${SECONDS}
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
LOGFILE="$(basename $0)_$(date +%Y.%m.%d_%H.%M.%S.log)"
LOGPATH=${DIR}"/logs"
mkdir -p ${LOGPATH}
echo_with_timestamp_and_log() {
message=$1
echo "$(date +"%Y.%m.%d %H:%M:%S") ${message}" | tee -a ${LOGPATH}/${LOGFILE}
}
log_time () {
echo -n "$(date +"%Y.%m.%d %H:%M:%S") "
}
read_parameter() {
variableToRead=$1
errorCodeToThow=$2
# read only lines begining with parameter name
result="$(grep "^$variableToRead" ./${configuration_file} | cut -d' ' -f2)"
if [ "${result}" == "" ]; then
echo_with_timestamp_and_log "ERROR ${2}: $1 variable not found in *.ini file"
exit $2
else
echo "${result}"
fi
}
echo_with_timestamp_and_log "Host: ${hostname}"
echo_with_timestamp_and_log "Script: ${script}"
echo_with_timestamp_and_log "Start time: ${start_time}"
echo_with_timestamp_and_log "______________ Script start _______________"
if [ "$#" -eq 0 ]; then
Server=$(read_parameter Server 101)
EAPdate=$(read_parameter EAPdate 102)
elif [ "$#" -eq 2 ]; then
Server=$1
EAPdate=$2
else
echo_with_timestamp_and_log "Illegal number of parameters"
exit 107
fi
echo_with_timestamp_and_log "server = ${Server}"
echo_with_timestamp_and_log "date = ${EAPdate}"
echo_with_timestamp_and_log "Connecting to AIS server ${Server}"
ssh -T $Server <<EOF | tee -a ${LOGPATH}/${LOGFILE}
$(typeset -f log_time)
log_time
echo "${Server}: Copying the EAP files to designated FTP path"
echo ""
echo "${Server}: Copying the files..."
scp -p -qo StrictHostKeyChecking=no user@${Server}:/path/to/source/files/*${EAPdate}* user@hostname3:/path/to/destination/files/
EOF
echo_with_timestamp_and_log "______________ Script end _________________"
elapsed_time=$((${SECONDS} - ${start_seconds}))
readable_time="$((${elapsed_time}/3600)) h $((${elapsed_time}%3600/60)) min"
echo_with_timestamp_and_log "Host: ${hostname}"
echo_with_timestamp_and_log "Script: ${script}"
echo_with_timestamp_and_log "End time: $(date)"
echo_with_timestamp_and_log "Duration: ${readable_time}"
Now, if the scp script would result with the error "No such file or directory" for the ${EAPdate} parameter, the log will not capture this message. And I do not understand why - turns out the log doesn't capture all the messages that is being seen on the screen when the script runs.
What am I missing? How should I create the scp command to pass the following output in the log based on the run:
- successful: echo "The files were copied successfully!"
- failed due to "No such file or directory" error: echo "The files were not copied successfully! No such file or directory in the source path!"