Issue
Inside shell script i need to transfer 1 file from 1 UNIX server to another UNIX server. So we need to check file size & disk available size of target server. If file size is greater than disk available size then job should get failed. So I echo both value but in if else condition it is not working properly.
#!/bin/bash
DIR=${1}
FILE=${2}
SFTP_USER=${3}
SFTP_PATH=${4}
SFTP_HOST=${5}
SFTP_PORT=${6}
tgt_path=${7}
FILE_SIZE=`stat -c %s ${DIR}/${FILE}`
echo "Size of the file = $FILE_SIZE"
sftp -o BatchMode=yes -oPort=${SFTP_PORT} ${SFTP_USER}@${SFTP_HOST} <<EOF > ${DIR}/logs/script_wr.log
cd ${SFTP_PATH}
mkdir ${tgt_path}
DISK_AVAIL_SIZE=`df -B1 ${SFTP_PATH}/${tgt_path}`
echo "Disk Available size = ${DISK_AVAIL_SIZE}"
if [ ${FILE_SIZE} -lt ${DISK_AVAIL_SIZE} ]
then
put ${DIR}/${FILE} ${SFTP_PATH}/${tgt_path}
ls -l ${SFTP_PATH}/${tgt_path}/${FILE}
else
echo "Job got failed as File size is greater than the disk available size of target server."
exit 1
fi
EOF
...............
echo "File transfer has been completed successfully for the file ${FILE}."
In this script when i debug it then Disk available size not able to generate of target server using sftp connectivity & also if else condition if [ ${FILE_SIZE} -lt ${DISK_AVAIL_SIZE} ]
is not working properly here. When i place temp.txt of 100MB file & disk available size is 85MB then it is moving 85MB file only not entire. Could you please help me here to print Disk available size of target server & if condition should work properly here in the script.
Solution
As you can't compare file size of one server & disk available size of another server. You can do 1 thing, take disk available size of target server in variable (eg:- $DISK_AVAIL_SIZE) and then you can compare with file size of main server. File size (stat -c %s) will give you in size in byte where df ${SFTP_PATH}
will give you disk available size in kb so you have to convert file size into kb, just divide file size by 1024.
................
output=$(sftp -o BatchMode=yes -oPort=${SFTP_PORT} ${SFTP_USER}@${SFTP_HOST} <<EOF
df ${SFTP_PATH}
EOF)
DISK_AVAIL_SIZE=$(echo $(output) | sed "s/sftp > df \$(output)//g" | awk '{printf("%s",$11)}')
echo ${DISK_AVAIL_SIZE}
if [ ${FILE_SIZE} -lt ${DISK_AVAIL_SIZE} ]
.....................
then you can compare file size & disk available size.
Answered By - Md Wasi Answer Checked By - Marie Seifert (WPSolving Admin)