LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Using variables in SFTP (https://www.linuxquestions.org/questions/linux-newbie-8/using-variables-in-sftp-4175524440/)

zachvac 11-05-2014 10:37 AM

Using variables in SFTP
 
ok I'm new to linux scripting but not new to programming.

Gotten most of this from google searches:
-----------------------------------------------------
#!/bin/bash

#argument, MMYYYY of previous month

#some stuff, $1 variable works fine here

sftp [location] << 'ENDSFTP'

cd [location]
get *$1*.txt
ENDSFTP
#other stuff down here
----------------------------------------------------------

If I want to use the input variables like $1 it works fine outside the sftp block, but inside when I try to do this it is not replacing $1 with the script argument variable. Basically someone else puts this text file on their server and as part of my script I want to be able to do this with an argument and not have to change the code every month. I have similar code to do this that is daily. One approach I've done is just getting *.txt and then deleting all the other ones using the variable outside the sftp block but there's got to be an easier way right?

Thanks.

schneidz 11-05-2014 10:44 AM

my opinion is that sftp prompts human users for keyboard input. whereas scp is designed for machine automation.

i think generically the problem is that the heredoc invokes a subshell in which $1 is not within its scope.

zachvac 11-05-2014 12:09 PM

Quote:

Originally Posted by schneidz (Post 5265000)
my opinion is that sftp prompts human users for keyboard input. whereas scp is designed for machine automation.

i think generically the problem is that the heredoc invokes a subshell in which $1 is not within its scope.

This makes sense. Do you know of any way I can get around it? With multiple commands I need to run while connected, can I use scp and if I could wouldn't I run into the same issue?


All times are GMT -5. The time now is 05:18 PM.