Operating System - HP-UX
1821638 Members
3193 Online
109633 Solutions
New Discussion юеВ

Return Value of cp command

 
Brad Marks
Super Advisor

Return Value of cp command

I have a Business Basic application program executing a system call to copy a file. The application language captures the HP-UX return value and, after execution, I check that it is zero. If it is non-zero I'm presuming that the operation failed.
A program is getting a return value of "1" although I can see that the copy does take place.
The man page for cp does not even mention a return value. Is there one?
Should I presume that a "1" is the same as a "0"?
Thanks!
Brad
It's not impossible -- it'll just cost more...
5 REPLIES 5
A. Clay Stephenson
Acclaimed Contributor

Re: Return Value of cp command

I suspect that there is a problem is the BASIC system command. Cp definitely sets an exit status.

Do this as a test:
cp myfile myfile2
echo ${?}

That should be zero.

Now:
cp myfilesdoesnotexist myfile2
echo ${?}

You should get a non-zero exit status.

I would test the BASIC system command by creating a small script that does an explicit exit 0, exit 1, etc. to see what value the BASIC program actually sees. In a few cases, when making system() calls from other languages, I had to do things like
cc = status / 256 or cc = status MOD 256 to get the "real" value.
If it ain't broke, I can fix that.
A. Clay Stephenson
Acclaimed Contributor

Re: Return Value of cp command

One other thought:

A typical Business BASIC scall can have an optional "ERR=lineref" parameter so that the BASIC can do a goto lineref if an error is detected:

status% = scall("cp myfile myfile2",err=jumpout)
....
....

jumpout:
bad% = 1

This may work for you even if the return value from scall() is unreliable.
If it ain't broke, I can fix that.
Victor BERRIDGE
Honored Contributor

Re: Return Value of cp command

ant:/home/vbe $ ll zizi
-rw-r--r-- 1 root bin 2018 Feb 17 16:03 zizi
ant:/home/vbe $ cp zizi zozo ;echo $?
0
ant:/home/vbe $ cp zaza zozo ;echo $?
cp: cannot access zaza: No such file or directory
1
ant:/home/vbe $

When works it does return 0....


All the best
Victor
Victor BERRIDGE
Honored Contributor

Re: Return Value of cp command

Sorry for my reply
It looks very silly after A. Clays post...
I was on the way to reply when someone entered my office and started to ask all sorts...
I didnt realize the time past when I pressed submit...

All the best
Victor
Brad Marks
Super Advisor

Re: Return Value of cp command

After looking at the Basic code I see that the program is attempting to copy a file that is opened by the very process trying to do the copy. I think this might be causing some unpredictable errors.
I say this because this program runs once a day and usually does fine. What I've done for a workaround is simply wait 1 second and then retry the copy; if the copy fails 4 times in a row then I issue an error message and go on my way.
Thanks to both of you for your help.
Brads
It's not impossible -- it'll just cost more...