1

I have a simple system command to copy file from one folder to another:

my $cmd = "xcopy /Y c:\DBs\Support\db.bak c:\jenkins\workdir\sql-bak-files";

When I try to run the following system commands, all fails:

1. my $res = qx/$cmd/; 2. my $res = qx($cmd); 3. using back ticks All tries returned the error: Error number -1, error message: "Bad file descriptor".

When trying to use system($cmd) the error was Error number 65280, error message: "No such file or directory".

This perl code is running via Jenkins (ver 2.190.1) and perl v5.26.0. This problem started after migrating the code from mercurial to git, but I don't think it's related. It worked before, but now always fail :(

2
  • Re "Error number 65280, error message: "No such file or directory"", $! ("No such file or directory") is meaningless when the error isn't -1. 65280 (0xFF00) means the child ran, but returned exit code 255 (0xFF) Commented Nov 5, 2019 at 17:14
  • And if I have variables inside the cmd? "xcopy /Y \"$text_file\" $text_local_shared_path" ? Commented Nov 5, 2019 at 20:25

1 Answer 1

3

A backslash has a special meaning in a Perl quoted string. It is used to escape the following character - to "turn off" any special meaning. If you want to use a backslash in a Perl quoted string, then you need to use another backslash to escape it.

my $cmd = 'xcopy /Y c:\\DBs\\Support\\db.bak c:\\jenkins\\workdir\\sql-bak-files';

Alternatively, Perl recognises forward slashes in Windows paths, so it might be easier to replace your code with this:

my $cmd = 'xcopy /Y c:/DBs/Support/db.bak c:/jenkins/workdir/sql-bak-files';

Note that in both cases I have replaced your double-quotes with single-quotes. This has no effect on your problem, but it seems strange to use double-quoted strings if you're not using any of their special characteristics (like the expansion of variables).

Update: To debug a problem like this, you can try printing the string.

$ perl -E'say "xcopy /Y c:\DBs\Support\db.bak c:\jenkins\workdir\sql-bak-files"'
xcopy /Y c:DBsSupportdb.bak c:jenkinsworkdirsql-bak-files
Sign up to request clarification or add additional context in comments.

3 Comments

Re "Perl recognises forward slashes in Windows paths", Not really. Perl doesn't know anything about paths. It's Windows itself that recognizes forward slashes as a valid path separator. That said, many Windows commands require paths using / as a separator to be quoted since / normally denotes the start of a command line option. Unfortunately, that trick doesn't work with xcopy. It simply doesn't accept / as a path separator.
And if I have variables inside the cmd? "xcopy /Y \"$text_file\" $text_local_shared_path" ?
@ShaiAlon: Or use qq[...] instead of "..." to simplify things.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.