0

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.

Shahin P
  • 372
  • 1
  • 4
  • 14
  • 1
    Why do you expect `sftp` to accept shell commands on standard input? It's not a shell interpreter. You can pass in a limited set of static commands with `sftp -b` but those are just simple text, not shell script. – tripleee Jun 13 '23 at 12:46
  • Thanks @tripleee for your comment. Any idea how to get disk available size of another target server because i used sftp connectivity to connect to server but disk available size could not able to get. – Shahin P Jun 13 '23 at 13:26
  • copy/paste your script into http://shellcheck.net and read [correct-bash-and-shell-script-variable-capitalization](https://stackoverflow.com/questions/673055/correct-bash-and-shell-script-variable-capitalization) then fix your script accordingly and then let us know if you still have a problem. – Ed Morton Jun 13 '23 at 13:34
  • You'll need `ssh` access to the remote machine. you can run cmds "against" the remote machine like `DiskFree=$(ssh usr@ip 'df -m /path/of/interest);echo "remDiskFree=${DiskFree}'` and do all your comparison and echo logic in a script that runs on your local machine. I searched for `[bash] [ssh] remote df`1 and numerous of the results had information that will help you. Good luck. – shellter Jun 13 '23 at 14:57
  • Missed a closing single-quote before the closing `)`. Good luck. – shellter Jun 13 '23 at 22:58

2 Answers2

1

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.

Md Wasi
  • 479
  • 3
  • 16
0

$FILE_SIZE is a variable that exists on one machine, $DISK_AVAIL_SIZE should exist on the other machine. You can't compare 2 variables on two different machines.

In fact, using the <<EOF syntax expands some parts of the here document before it gets sent to the other machine. In particular, all unescaped variables are expanded, as well as backticks.

Therefore, the code sent to the other machine sees

DISK_AVAIL_SIZE=Filesystem                 1B-blocks        Used    Available Use% Mounted on
/dev/mapper/xxx 148683902976 10957774848 130098786304   8% /some/path

or whatever the output of the command is on the source machine.

Backslash the backquotes and backslash the dollar sign to prevent the expansion.

DISK_AVAIL_SIZE=\`df -B1 ${SFTP_PATH}/${tgt_path}\`
if [ ${FILE_SIZE} -lt \${DISK_AVAIL_SIZE} ]
choroba
  • 231,213
  • 25
  • 204
  • 289
  • Thanks @choroba for your answer but when I echo `$DISK_AVAIL_SIZE` in the script & in logs i could not able to see value of $DISK_AVAIL_SIZE. – Shahin P Jun 13 '23 at 13:18
  • Any idea how to get disk available size of another target server because i used sftp connectivity to connect to server but disk available size could not able to get. – Shahin P Jun 13 '23 at 13:27
  • @ShahinP: Have you escaped the dollar sign in `echo "Disk Available size = \${DISK_AVAIL_SIZE}"`, too? What was the output? – choroba Jun 13 '23 at 13:35
  • yes i used this one also but not able to get `DISK_AVAIL_SIZE` of target server. – Shahin P Jun 13 '23 at 13:43
  • @ShahinP: What was the output of `echo "Disk Available size = \${DISK_AVAIL_SIZE}"`? – choroba Jun 13 '23 at 14:11