Operating System - HP-UX
1834137 Members
2182 Online
110064 Solutions
New Discussion

Re: rcp with variable filename

 
so.nimda
Super Advisor

rcp with variable filename

Hi,

I'm writing a script to remotely copy a file from a non-unix server. As the filename is not fixed, I'm trying to use a variable to do that.

Here's the command that I'm using :
rcp remote_host:'\folder\$filename' $filename

Executing this resulted in error "The system cannot find the file specified."

How can I correct this?

Thanks in advance.

:)
13 REPLIES 13
Enrico P.
Honored Contributor

Re: rcp with variable filename

Hi,
try to remove the "'" symbol.

The rcp work fine with the name of the file?

Enrico
so.nimda
Super Advisor

Re: rcp with variable filename

Hi Enrico,

Thanks for the reply.

If the quotation is removed, it will not work. I need to speciy the full path.

Regards
V. Nyga
Honored Contributor

Re: rcp with variable filename

Hi,

shouldn't
rcp remote_host:'\folder\$filename' .
work also?

So how do you want to set $filename?

Volkmar
*** Say 'Thanks' with Kudos ***
Enrico P.
Honored Contributor

Re: rcp with variable filename

Hi,
try to substitute the single quota ' with the double quota ". The single quota not interpreted the $filename:

FILE=test
echo "$FILE"
test
echo $FILE
test
echo '$file'
$file

Enrico
Bill Hassell
Honored Contributor

Re: rcp with variable filename

Single quotes ' (apostrophe) disable all variables witin the quotes. So the result sent to the remote host is \folder\$filename and not \folder\myfile or whatever filename you want. Now it's important to note that the reverse slash \ has a special meaning to your shell, so it is removed automatically and the next character is used without any special meaning -- it is called the escape character to escape any special meaning of the next character.

So escape the escape character like this:

rcp remote_host:\\folder\\$filename $filename

and now it should work as you expect. Copying files and executing remote commands between very different computers requires careful attention to special characters.


Bill Hassell, sysadmin
Michael Steele_2
Honored Contributor

Re: rcp with variable filename

Here's some additional eval. of rcp commands since the file transfer looks good by Mr. H.

rcpinfo -p master/client

check for these daemons,

rpcbind, status, nfs, mountd, nlockmgr, llockmgr

Note: unless your on something older like 10.20.
Support Fatherhood - Stop Family Law
Geoff Wild
Honored Contributor

Re: rcp with variable filename

Try:

cd /local/directory
rcp remote_host:/folder/${filename} .

Rgds...Geoff
Proverbs 3:5,6 Trust in the Lord with all your heart and lean not on your own understanding; in all your ways acknowledge him, and he will make all your paths straight.
so.nimda
Super Advisor

Re: rcp with variable filename

thanks all, for the replies...

will give it a try...
so.nimda
Super Advisor

Re: rcp with variable filename

Has anyone out there ever rcp a file from a windwows 2003 server?

The scenario is this :

I'm trying to copy a file (has no fixed filename, hence the variable) from a Windows 2003 server to a UX machine.

The UX machine initiates the pulling of this file.

After trying all the helpful suggestion, I realise that Windows 2003 server may not understand the $filename UX variable.

Has anyone out there done something similar?

Any help is much appreciated.

Thanks ! :)
Michael Steele_2
Honored Contributor

Re: rcp with variable filename

Bill Hassell
Honored Contributor

Re: rcp with variable filename

It's important to understand how the command line works in a shell. When you include something like $filename, that is not what will be sent to the Windows box. Instead, the shell preprocesses every command line to substitute the contents of variables, evaluate $(..) and other substitutions. To see how this works, just put echo in front of your command:

echo rcp remote_host:'\folder\$filename' $filename

Now you will see what the real command looks like. The single quotes are very special in that they turn off special character processing inside the single quotes. Do this instead:

echo rcp remote_host:"\folder\$filename" $filename

As you will see, double quotes will expand $variable names to their contents.

So you see that the Windows server never sees the $filename unless you use the single quotes to turn off the special processing. And of course $filename is meaningless to Windows (or any other remote computer).

There is another Windows 'feature' concerning directories. You sometimes have to double the backslashes, something like this: \\folder\\$filename. To eliminate the Windows ' path problems, try getting a file from the base disk as in: C:$filename


Bill Hassell, sysadmin
so.nimda
Super Advisor

Re: rcp with variable filename

Hi Bill,

Thanks for the very detailed explanation.

I now have a better idea of how the single and double quotes work.

And your tip worked !!

Appreciate your help !

Cheers !!
so.nimda
Super Advisor

Re: rcp with variable filename

Bill provided the solution.

Great job, Bill !