我想从脚本中提取scp
命令并解析scp
命令中的变量。
示例脚本:
if [[ $# -ne 1 ]]; then
echo "ERROR: USAGE $0 <RUN_DATE>"
echo "abcd_efgh_rfp.sh 20171003"
exit 1
fi
RUN_DATE=$1
SLC_FILE_PATH=/a/b/c/d/
DROP_FILE_PATH=/a/
DROPZONE_HOST="a@dz.pp.com"
#Variables
sleep_time="1m"
exit_flag=0
echo "Source directory: ${DROP_FILE_PATH}"
echo "Target directory: ${SLC_FILE_PATH}"
echo "Start File pull from DropZone to SLC - date ${RUN_DATE}"
echo "sftp for the file started"
file_watcher () {
drop_file_name=a_${RUN_DATE}.txt
slc_file_name=/a/b/c/d_${RUN_DATE}.txt
dropzone_host="a@dz.pp.com"
while [[ ${exit_flag} -eq 0 ]]
do
echo "Transferring file ${drop_file_name} to SLC ${slc_file_name}"
scp -i ~/.ssh/id_rsa_dz_batch_regulatory ${dropzone_host}:${drop_file_name} ${slc_file_name}
##scp ${dropzone_host}:${drop_file_name} ${slc_file_name}
if [[ $? -eq 0 ]]; then
echo "Transfer of file - ${drop_file_name} to SLC - ${slc_file_name} is successful "
exit_flag=1
else
echo "Files aren't placed in path: ${drop_file_name}, sleeping ${sleep_time} for file to be generated"
incremental_wait=`expr $incremental_wait + 1`
if [[ $incremental_wait -eq 1 ]]; then
echo "incremental_wait=1 Transfer of dropzone file - ${drop_file_name} to SLC - ${slc_file_name} is failed "
echo "++++++++++++++++++++++ ABORTING ++++++++++++++++++++++" >> ${log_file_name} 2>&1
exit 1
fi
sleep ${sleep_time}
fi
done
}
#Copying files from dropzone to slc host
exit_flag=0
file_watcher ${DROP_FILE_PATH} ${SLC_FILE_PATH} ${DROPZONE_HOST}
所需的输出:提取scp
命令并解析变量:
scp -i ~/.ssh/id_rsa_dz_batch_regulatory \
${dropzone_host}:${drop_file_name} ${slc_file_name}
解析变量后:
scp -i ~/.ssh/id_rsa_dz_batch_regulatory \
a@dz.pp.com:a_$1.txt /a/b/c/d_$1.txt