#!/usr/bin/perl
@ARGV = map { s/'/\\'/g; $_; } @ARGV;
my $files = "'" . join("' '", @ARGV) . "'";
my $results = `/usr/bin/scp -rBq $files dcoppit\@coppit.org:.`;
print "Output: $results" if $results ne '';
[robg adds: I haven't tested this one.]
|
|
|
Here's a perl script that will escape filenames and send the files to a remote server using scp. I save it as scp-to-coppit.org in my user's bin folder, and make it executable (chmod a+x scp-to-coppit.org). Then from the command line, I can do this: scp-to-coppit.org File with weird char's.txt. Here's the code:
[robg adds: I haven't tested this one.]
•
[8,971 views]
Hint Options
A script to ease scp for files with odd names
Can you clarify this fun bit of perl/grep:
map { s/'/\'/g; $_; } @ARGV
A script to ease scp for files with odd names
It's just escaping all the single quote characters in the string.
A script to ease scp for files with odd names
Clarification:
A script to ease scp for files with odd names
I really wish people understood that in Perl's "map", the $_ is an alias to the original data. And best practice is never to alter $_ inside that block, because you're altering the original data, turning it from functional programming to imperative programming. You could leave the assignment to @ARGV out, and the result would still be the same! Spooky, eh? That's why you don't do that.
A script to ease scp for files with odd names
This whole script can be replaced with (much more safely, as spaces and vertical bars and all other shell metachars won't be troublesome):
It's an example of over-engineering with under-results. :)
A script to ease scp for files with odd names
This seems like a quite an odd and dangerous thing to do. It's also fairly broken: it won't work with filenames that have multiple spaces in a row, double quotes, or backslashes in them. And if you have a pipe character, you're in real trouble. That's just at first glance; it probably has plenty of other issues.
A script to ease scp for files with odd names
...or use wildcards for the weirdnesses.
Simpler and safer
A much simpler and much safer way: use "system"
System is safer because it enforces that there cannot be any command injection loopholes in the args and also it does not need to escape the meta chearaters.
A script to ease scp for files with odd names
scp-to-coppit.org File with weird char's.txtNo, you can't. Not in the shell anyway, since it will look for the closing quote and not include the quotes in the text. In this case, the shell will simply give you the secondary prompt and wait until you've typed another quote or escaped your way out (^G, ^C). And if you've typed the second quote, the error is most likely going to be "file not found". According to me, by I'm a command line user since the early 80's, it's much easier and safer to use the built in completion, e.g. using the tab key. Suppose my directory contains these three files
Then typing
will show
because that's the unique starting part. The choice has now boiled down to the first two files. At that point, typing w and another TAB will fill in the entiry name, resulting in
an you're ready to type the destination. Or you could make the script with the destination in it, but without the weird and potentially dangerous argument mangling. And it is dangerous, since the arguments are joined with single spaces and thus (under bizarre circumstances admittedly (*)) you could be overwriting an unwanted file at the destination. (*) Think of file names with more than one consecutive space and differing with another file in only the spaces, not to mention files with backslashes in their name...
A script to ease scp for files with odd names
Then how to do batch scp transfers, e.g. "scp *.txt" and the like? Tab-completion won't work in those cases.
A script to ease scp for files with odd names
That's true, but the original script doesn't do that either: if you type a * in calling, it will be expanded to contain all files in the directory.
A script to ease scp for files with odd names
Do not use this. Learn how to quote in your shell: |
SearchFrom our Sponsor...Latest Mountain Lion HintsWhat's New:HintsNo new hintsComments last 2 daysLinks last 2 weeksNo recent new linksWhat's New in the Forums?
Hints by TopicNews from Macworld
From Our Sponsors |
|
Copyright © 2014 IDG Consumer & SMB (Privacy Policy) Contact Us All trademarks and copyrights on this page are owned by their respective owners. |
Visit other IDG sites: |
|
|
|
Created this page in 0.13 seconds |
|