Search code examples
command-linemakefilesftp

Including some SFTP commands in a Makefile


I use a Makefile to create pdfs of papers I'm working on. I'd also like to use make to upload the latest version to my website, which requires sftp. I thought I could do something like this (which works on the command line) but it seems that in make, the EOF is getting ignored; i.e., this

website:
    sftp -oPort=2222 [email protected] << EOF
    cd papers
    put research_paper.pdf
    EOF

generates an error message

cd papers
/bin/sh: line 0: cd: papers: No such file or directory

which I think is saying "papers" doesn't exist on your local machine i.e., the 'cd' is being executed locally, not remotely.


Solution

  • You cannot (normally) put a single command on multiple lines in a Make recipe, so here documents are a no-go. Try this instead:

    website: research_paper.pdf
        printf 'cd papers\nput $<\n' \
        | sftp -oPort=2222 [email protected]
    

    The target obviously depends on the PDF, so I made it an explicit dependency, as well.

    It's not that the << EOF is "ignored"; it gets run by a separate shell which doesn't see the subsequent lines. Then cd papers is run in a new shell instance, which doesn't know anything about the previous command. This is a common Make beginner FAQ. (GNU Make has the .ONESHELL extension to run all commands in a recipe in a single shell command, but it's obviously not portable.)