Error while renaming the file in SFTP Session

Hi All

Below is the script I am trying to execute to rename a file in an SFTP session.

It is in GNU/Linux.

This script is for generic use and so I am passing arguments. Everything in the script works fine except "reading the filename in SFTP session"

In the below script the $Remote_server_source_path is read properly. The filename is read from the while loop. When i echo the filename, it is read correctly, but somehow it is not read in sftp session

Please help!!

#!/bin/sh
#set -v
file_path=$1
Remote_server_source_path=$2
Dest_path=$3
File_pattern=$4
LOGIN=$5
echo "File path at Laclede End :"$file_path
echo "Path FROM where the file has to be moved :" $Remote_server_source_path
echo "Path TO where the file has to be moved :" $Dest_path
echo "SFTP server Login : " $LOGIN
cd $file_path
echo $pwd
DIRNAME=$(echo `pwd` | rev | cut -d "/" -f1 | rev)
echo $DIRNAME
ls -ltr $File_pattern > tempfilelist
count=`cat tempfilelist | wc -l`
echo "Number of files found: " $count
cat tempfilelist | while read line
do
#echo $line
echo "Filename to be moved : "
filename= echo $line | awk '{print $9}'
echo $filename
sftp $LOGIN  << EOF
cd $Remote_server_source_path
pwd
rename "$Remote_server_source_path/$filename" "$Dest_path/$filename"
bye
EOF
done

---------- Post updated at 09:08 AM ---------- Previous update was at 09:07 AM ----------

I also tried removing the qoutes . It still doesnt work.

This is wrong:

filename= echo $line | awk '{print $9}'

You already do this sort of thing correctly when you set DIRNAME, but you forgot to do so here. Also the extra space after the equals ruins it too.

filename=$(echo "$line" | awk '{ print $9 }' )
1 Like

ohh.. i see it!! I didnt realise that makes a difference, because it was printing the name right.

Thankyou so much!! It saved my day!. :slight_smile:

---------- Post updated at 10:31 AM ---------- Previous update was at 10:30 AM ----------

And yes ofcourse, It worked!! :D. Thanks again Corona688!

1 Like