Issue with using While loop

Hi,

I am trying to move a file from remote server to local server and when the transfer completes successfully i call a script in remote server to remove the file which was successfully transferred.

I do this by first getting the list of file in remote server and move the text file to local server and read the text file line by line using while loop and scp the file and on successful transfer remove the file by calling a script which is in remote server (script has chmod + rm command).

The issue is that when there are multiple files in the server this process runs only for the first file. But when i comment out the line which calls the script to remove the files in remote server all the files are processed.

Please help.

Thanks
Guru

copy the files from remote to local server ( this will copy all the files present in the myfiles folder)

1) scp user@remote-server:/abc/myfiles/* /path/to/local/server/

remove the remote server files (which is transferred)

2) ssh user@remote-server "rm -rf /abc/myfiles/*" 

Thanks for the quick reply itkamaraj.

But i am trying to transfer files from different folder in the remote server.

can you post your script

Below is my script,

date=`date +"%Y%m%d%H%M%S"`
#TEST_FILE_LOG="`echo $1`"

if [ -s $COPY_FILES_LIST ]; then
    rm $COPY_FILES_LIST
fi

DUMMY=`ssh -l $FTP_USER $FTP_HOST /home/ftpuser/get_test_file.sh`
sudo scp -p $FTP_USER@$FTP_HOST:/home/ftpuser/test_files_list.txt $COPY_FILES_LIST

fileCount=`wc -l < $COPY_FILES_LIST`

    myFile=$COPY_FILES_LIST
    while read line
    do 

        I_CLI_CODE="`print $line | cut -f5 -d'/'`"
    
        if echo $I_CLI_CODE | egrep -q '^[0-9]+$'; then

            $JAVA_COMMAND -classpath ${PROCESSES_HOME_DIR}:${PROCESSES_HOME_DIR}:${JDBC_JAR_FILE}:. com.TestFile $DB_HOST $DB_PORT $DB_SID $DB_USER $DB_PASSWD $I_CLI_CODE $TEST_FILE_LIST
            cli_code=$I_CLI_CODE

            file=$line
            ftpfilename=`basename "$line"`
            filename=`basename "$line"`-$cli_code
        
            sudo scp -p $FTP_USER@$FTP_HOST:$line $SCPED_TEST_FILES$filename
            SCP_STATUS=$?
            if (test "$SCP_STATUS" = "0"); then
                find $SCPED_TEST_FILES*.* -type f  -print | grep -i test > $STG_TEST_FILES

                DUMMY=`ssh -l $FTP_USER $FTP_HOST /home/ftpuser/remove_test_files.sh $I_CLI_CODE $ftpfilename $file`

                myFile=$GETCLIENT_NAME_STATUS
                while read line
                do

                    cli_name="`print $line | cut -f2 -d'/'`"
                    cli_status="`print $line | cut -f3 -d'/'`" 
                    myFile=$STG_TEST_FILES
                    while read line 
                    do

                        scpd_filename=`basename "$line"`

                        if (test "$scpd_filename" = "$filename"); then

                            if [ -d $SCPED_TEST_FILES"$cli_name"_$cli_code ]
                            then
                            echo "$DIR directory  exists!"
                            else
                                mkdir $SCPED_TEST_FILES"$cli_name"_$cli_code
                            fi

                        ## Code to move the file to client specific directory goes here ##

                        fi


                    done < $STG_TEST_FILES

                done < $GETCLIENT_NAME_STATUS

            fi

        else

            print " "                                                                    >> $TEST_FILE_LIST
            print 'Failed to move the Test file from the path' "$line"                                        >> $TEST_FILE_LIST
            print " "                                                                    >> $TEST_FILE_LIST

            print " "                                                                    >> $TEST_FILE_LOG
            print 'Failed to move the Test file from the path' "$line"                                         >> $TEST_FILE_LOG
            print " "                                                                    >> $TEST_FILE_LOG

        fi

    done < $COPY_FILES_LIST 

if you comment the DUMMY line, then is it working ?

change it as below and give a try

 
DUMMY=$(ssh -l $FTP_USER $FTP_HOST "/home/ftpuser/remove_test_files.sh $I_CLI_CODE $ftpfilename $file")

ya it works when i comment the DUMMY line...

Sorry it doesn't work with the changed code also :frowning:

I don't remember the exact reason, but, when ssh exits it gives a shell exit code or something like that... so you need to tell it to keep going after it's finished with the first file.

Try this, take this off

< $COPY_FILES_LIST

Add this before your while read

exec < $COPY_FILES_LIST

Thanks jtollefson, but cant get it working. Actually the script doesn't exit immediately after the ssh it executes the script fully but the looping is not happening after the first file...

---------- Post updated at 03:26 PM ---------- Previous update was at 02:00 PM ----------

Thanks for all ur help .... got the issue fixed by adding -n to ssh....

Thank u once again...:b: