scpコマンドを使用して、特定のパラメータに基づいてserver2($ {Server}パラメータになる)からserver3にファイルをコピーする責任があるserver1でスクリプトを実行しています。
scp -p -qo StrictHostKeyChecking=no user@${Server}:/path/to/source/files/*${EAPdate}* user@hostname3:/path/to/destination/files/
スクリプト自体は実行時にログを生成する必要があります。すべてが期待どおりに機能し、EAPdateパラメーターが期待どおりに入力されると(YYYYMMDD)、コピーコマンドは期待どおりに機能します。
ただし、パラメーターを(DDMMYYYY)として入力すると、コピーコマンドは失敗し、「該当するファイルまたはディレクトリはありません」エラーが発生します。
スクリプトは次のとおりです。
#!/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}"
scpスクリプトで「該当するファイルやディレクトリがありません」というエラーが発生した場合${EAPDate}パラメーターを使用すると、ログにこのメッセージはキャプチャーされません。理由がわからない。結果ログは、スクリプトの実行中に画面に表示されるすべてのメッセージをキャプチャするわけではありません。
私は何を見逃していますか?実行時にログに次の出力を渡すには、scpコマンドをどのように生成する必要がありますか?
- 成功:echo「ファイルコピー成功!」
- 「該当するファイルまたはディレクトリがありません」エラーのため失敗しました。 echo "ファイルが正常にコピーされませんでした。ソースパスにそのファイルやディレクトリがありません。"
ベストアンサー1
man bash
§パイプラインによると
|&を使用すると、コマンドの標準エラーが標準出力に加えて...に接続されます。
交換する必要があります
ssh -T $Server <<EOF | tee -a ${LOGPATH}/${LOGFILE}
渡す
ssh -T $Server <<EOF |& tee -a ${LOGPATH}/${LOGFILE}