cp error handling

U

Urs

Hello

What is the correct formulation of this?

`cp $srcfile $dstfile` || do {
print "Execution error cp $srcfile, $dstfile\n";
};

It is always generating "Execution error ..." message.

I would like to have this message only if cp really failed.

Thanks

Urs
 
A

Anno Siegel

Urs said:
Hello

What is the correct formulation of this?

`cp $srcfile $dstfile` || do {
print "Execution error cp $srcfile, $dstfile\n";
};

It is always generating "Execution error ..." message.

I would like to have this message only if cp really failed.

perldoc -f system

Anno
 
S

Sherm Pendley

Urs said:
What is the correct formulation of this?

`cp $srcfile $dstfile` || do {
print "Execution error cp $srcfile, $dstfile\n";
};

It is always generating "Execution error ..." message.

I would like to have this message only if cp really failed.

Backticks capture the output of the command, so the above will fail if
the cp command produces any output at all. So it will *always* fail if,
for example, you system's cp is configured to produce verbose output
(i.e. cp -v) by default.

What you want to use is system(), which returns 0 on success:

(system("cp $srcfile $dstfile") == 0) || do {
print "Execution error: cp $srcfile, $dstfile\n$!\n";
}

Note the $! variable - When something fails, it's nice to know *why* it
failed.

sherm--
 
J

John W. Krahn

Urs said:
What is the correct formulation of this?

`cp $srcfile $dstfile` || do {
print "Execution error cp $srcfile, $dstfile\n";
};

use File::Copy;

copy( $srcfile, $dstfile ) or die "Execution error copy $srcfile, $dstfile\n";



John
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
474,160
Messages
2,570,890
Members
47,423
Latest member
henerygril

Latest Threads

Top