1
ftp -n ${FTP_HOST} << STOP
    user ${FTP_USERNAME} ${FTP_PASSWORD}

    binary
    lcd ${FTP_FROM_DIR}
    cd ${FTP_TO_DIR}
    put ${reportFileName}   
STOP

That is my code which is not successfully copying the file to remote host but using it manually it successfully copies the file to remote host.

When ran from a script, "(local-file) usage:put local-file remote-file" appears in the console.

what could be the problem?

2
  • What does the String Expansion print for from and to dir? Commented Sep 26, 2014 at 5:49
  • from: /home/893591/SI/data/mpsftp/krnu003/forftp to: /home/893591/SI/data/mpsftp/krnu003 I'm just actually testing my ftp code within the same server, given that the remote server is not yet accessible. So what I am trying to do here is that transferring the file to another folder within the server. Commented Sep 26, 2014 at 6:01

1 Answer 1

0

I suspect part of your issue is with the way you're constructed your heredoc. Try it like so:

$ ftp -n ${FTP_HOST} << STOP
user ${FTP_USERNAME} ${FTP_PASSWORD}

binary
lcd ${FTP_FROM_DIR}
cd ${FTP_TO_DIR}
put ${reportFileName}   
STOP

If you truly want those spaces/tabs in the command then you'll need to change to this form of the heredoc:

$ ftp -n ${FTP_HOST} <<-STOP
    user ${FTP_USERNAME} ${FTP_PASSWORD}

    binary
    lcd ${FTP_FROM_DIR}
    cd ${FTP_TO_DIR}
    put ${reportFileName}   
STOP

Whenever you have spaces/tabs within your heredoc you need to make use of the <<- for to tell the shell to strip the leading spaces/tabs out prior to running the commands that are contained within.

A sidebar on using echo

When you're parsing variables within scripts and you innocently use echo to print messages you're typically getting more than just the string you're interested in. echo will also include a trailing newline character. You can see it here in this example:

$ echo $(echo hi) | hexdump -C
00000000  68 69 0a                                          |hi.|
00000003

The 0a is the ASCII code for a newline. So you'll typically want to disable this behavior by using the -n switch to echo:

$ echo -n $(echo hi) | hexdump -C
00000000  68 69                                             |hi|
00000002

Or even better upgrade to using printf.

References

17
  • tried this one but got the same result Commented Sep 26, 2014 at 7:08
  • @KevinMacaraeg - and if you put the actual values into the heredoc vs. using the variables, does it still not work? Try this just to confirm my suspicion. Commented Sep 26, 2014 at 7:34
  • it worked! i've used the actual values into the heredoc.. Commented Sep 26, 2014 at 7:41
  • @KevinMacaraeg - yes but how are they being sourced into the script that's ultimately running the above ftp? Commented Sep 26, 2014 at 7:52
  • I declared them in the script. I tried adding "tr -d '\r'" in their initialization, and it worked.. it's carriage return is the culprit after all.. do you know how carriage returns' presence in the variables can be prevented? Commented Sep 26, 2014 at 7:57

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.