Home > Enterprise >  Why is my batch script using SFTP only running the first line?
Why is my batch script using SFTP only running the first line?

Time:12-24

I have a batch script, in which I am logging into an SFTP server and moving around some files. To do this, I've copied the public key so that there's no user interaction between me and the server. The script should just enter the hostname, the username and the port, and then move around some files, without entering a password. However, when I run my script, only the first line is running (the SFTP command). Here is the script:

sftp -oPort=PORT USER@HOST_IP_ADDRESS
pwd
cd mcservers/earth/plugins
pwd
rm SomeFile.jar
put "C:\Users\USER\Documents\THE_PATH\target\AnotherFile.jar"
exit

I've tried putting an ampersand after my lines but it didn't work. What can I do?

CodePudding user response:

Batch files run each line as a command, wait for them to complete and then execute other lines. They do not simulate keyboard input.

So what your batch file does it that it runs sftp in an interactive mode and waits for it to complete. That never happens, until you type bye.

If you want to automate the sftp commands, one way to do that is to store the sftp commands to separate file (sftp.txt) and execute it using -b switch:

sftp -b sftp.txt -oPort=PORT USER@HOST_IP_ADDRESS
  • Related