cp exit status and conditional execution
Hi all,
I'm working on a Bash script for copying files and it's almost done, but I ran into a weird error. It's supposed to increment a "success" counter in the case, but the problem I have is that it seems to always think that it has succeeded. Here is the copy code below: Code:
cp $mPoint$UT $mPoint$UT_bak 1>/dev/null 2>&1 && ((successA++)) || echo "$UT not found on $mPoint" Code:
1>/dev/null 2>&1 I've tried a bunch of things with no success, like changing the && to an || (still somehow increments successA). Also tried changing it to successA = $successA + 1 and a few other versions. Any help is greatly appreciated. |
Insert a "set -x" line just above that code and see what is actually being executed.
|
reverse the redirects?
Code:
cp $mPoint$UT $mPoint$UT_bak 2>&1 1>/dev/null && ((successA++)) || echo "$UT not found on $mPoint" |
set -x:
It executes the command I expect it to. Here's what I learned: I still had test files in one of the mounted partitions that I did not expect to have. I will remove them and continue the testing, I think this may have been my problem. Thank you for the suggestion. I haven't tried reversing the redirects. Not sure what this would do, but removing them completely doesn't seem to help. UPDATE: I removed the test files, here's the output of set -x: Code:
+ cp /mnt/sda1/file /mnt/sda1/file_bak |
Are you sure you don't need some {} around your variable names?
From the looks of it, you have three variables at play here: $mPoint, $UT, and $UT_bak. Is that correct? Or is $UT_bak supposed to be ${UT}_bak? |
Yeah, there are three variables. Now that you mention it, ${UT}_bak might have been a more clever way of doing it. So now that I've resolved that there were test files in place I thought I had deleted (actually the program restored them), I can say that the
Code:
cp $mPoint$UT $mPoint$UT_bak 2>&1 1>/dev/null && ((successA++)) Code:
|| echo "$UT not found on $mPoint" |
That's because ((successA++)) is "failing":
Code:
$ successA=0 ; if ((successA++)) ; then echo success ; else echo failure ; fi |
Unable to duplicate with simplification:
Code:
cp this that 1>/dev/null 2>&1 && echo GOOD: $? || echo BAD: $? |
I'd try sg like this
Code:
if cp "$mPoint$UT" "$mPoint$UT_bak" 1>/dev/null 2>&1; then |
Quote:
|
All times are GMT -5. The time now is 11:27 PM. |