Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

remote variable question 1

Status
Not open for further replies.

nimrodman

MIS
Nov 22, 2005
43
US
Does anyone know why the following variable will not work if used on remote node, I have grappled with it for hours without any success, thanks.

MAKE_LIST=`ssh –t $USER@$RNODE “for files in cat $the_files do; cp $files $GET_DIR done”`
Echo “$MAKE_LIST”
 
Variables inside " quotes are expanded by the shell on your local system before the command is sent to the remote host, hence the actual command that will run remotely would be:

[tt]ssh –t username@remotenode "for files in cat file1 file2 file3; cp /get/dir done"[/tt]

Also, you are using cat incorrectly (you either want to use `cat $the_files` to obtain the contents of the files pointed to by the_files variable, or simply $the_files if the variable contains the list of files.

Also the for loop syntax is incorrect; you'll need a semi-colon before the do, not after it, and a semi-colon before the done. Try this:

[tt]MAKE_LIST=`ssh –t $USER@$RNODE "for files in \`cat $the_files\`; do cp \$files $GET_DIR; done"`[/tt]

The escapes protect the evaluation of the $files variable and the backquoted sections until the command is sent to the remote host.

Annihilannic.
 
I am having a hard time making the cp section to wrk, it is not picking up the $files variable, it is the last part of the code that is giving me my worst nightmare...any suggestions?
 
try

for files in ${<filename}

Mike

"Whenever I dwell for any length of time on my own shortcomings, they gradually begin to seem mild, harmless, rather engaging little things, not at all like the staring defects in other people's characters."
 
cp \$files $GET_DIR; done"` This is the part that does not work, the variable is empty, I have done all kinds of quoting and back slash, any help with this would be highly appreciated, thanks.
 
mrn, I think you meant $(<filename).

I just did this test and it seemed to work fine:

[tt]$ USER=myuser
$ RNODE=localhost
$ GET_DIR=/tmp
$ the_files=testfile
$ echo one > testfile
$ echo two >> testfile
$ echo three >> testfile
$ ssh -t $USER@$RNODE "for files in \`cat $the_files\`; do echo cp \$files $GET_DIR; done"
cp one /tmp
cp two /tmp
cp three /tmp
Connection to localhost closed.
$[/tt]

I notice in your original post that some of the quotes have been munged by some software before you pasted it in, note that the speech marks are angled “like this” when they should be "like this" for Unix to understand them? Maybe you have a funny character in there.

Annihilannic.
 
Anni, thank you for your input; I see that it worked the way you have it, my frustration so far is that it is not working for me maybe because I used find to select certain files from a list and parse that list to a new file and I am trying to make it read from that file.

In brief, that cp section is still not catching the variable $files, it gets lost in nirvana, this is a mystery to me and very frustrating to say the least, I really hope that someone can help at sowing me how to catch and use variable on a remote server.
 
maybe because I used find
Any chance you could post your actual code ?

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Where is the find output located, on the local or remote host?

Is the_files really a variable, or is it actually the filename itself?

Annihilannic.
 
Anni.. everything is sent to the remote node from the local node, everything works fine including the cat you suggested a solution to, and thanks for that, but the only piece of the code that does not work is that final cp piece, it is not seeing the return $files holds, that escapes into nirvana, it is very frustrating that I cannot figure out.
 
Below are the two methods I have used to extract the files into a storage area and then cat the files and tried to copy them into a repository, the last bit is the part that I cannot get to work, that is the cp part.

ls_files=`ssh -t $USERID@$RSYSTEM "cd $REMOTE_DIR; ls -l | grep 867 > $EXPORT_FILES; \
for files in \`cat $EXPORT_FILES\`; do && cp \$files $SOURCE_DIR; done"`

1>&2
cp_files=`ssh -t $USERID@$RSYSTEM "cd $REMOTE_DIR; \
for files in \`cat $EXPORT_FILES\`; do \
echo cp \\$files $SOURCE_DIR; done"`
 
[tt]ls_files=`ssh -t $USERID@$RSYSTEM "cd $REMOTE_DIR; ls -l | grep 867 > $EXPORT_FILES; \
for files in \`cat $EXPORT_FILES\`; do [red]&&[/red] cp \$files $SOURCE_DIR; done"`[/tt]

Leave out the [red]&&[/red] and you should be fine... Perhaps a double backslash before $files is necessary.

[tt]ls_files=`ssh -t $USERID@$RSYSTEM "cd $REMOTE_DIR; ls -l | grep 867 > $EXPORT_FILES; \
for files in \`cat $EXPORT_FILES\`; do cp \$files $SOURCE_DIR; done"`[/tt]

or

[tt]ls_files=`ssh -t $USERID@$RSYSTEM "cd $REMOTE_DIR; ls -l | grep 867 > $EXPORT_FILES; \
for files in \`cat $EXPORT_FILES\`; do cp \\$files $SOURCE_DIR; done"`[/tt]


HTH,

p5wizard
 
The last piece is not working still, that is the cp section.
 
Because you have added an extra evaluation of variables by using ls_files=` ... ` the situation has become more complex. Do you really need to do that?

Incidentally, you don't want to use ls -l or else you will be attempting to copy files called -rwxr-x--- and so-on as well.

This should work:

Code:
ls_files=`ssh -t $USERID@$RSYSTEM "cd $REMOTE_DIR; [COLOR=red]ls[/color] | grep 867 > $EXPORT_FILES; \
for files in [COLOR=red]\\[/color]\`cat $EXPORT_FILES[COLOR=red]\\[/color]\`; do cp [COLOR=red]\\[/color]\$files $SOURCE_DIR; done"`

But why not simply:

Code:
ssh -t $USERID@$RSYSTEM "cp $REMOTE_DIR/*867* $SOURCE_DIR"

Are there too many files causing a "command line too long" problem or something?

Annihilannic.
 
Anni, you are my hero! The second option you suggested is working great, thanks a lot.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top