ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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"
When I remove the "
Code:
1>/dev/null 2>&1
" the output from cp is "can't stat: no such file or directory" which is really weird because the copy did actually succeed.
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.
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
cp: can't stat '/mnt/sda1/file': No such file or directory
+ (( successA++ ))
It seems to increment the success counter despite failure!
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
does not work as expected. (Executes either way). I could separate it out into an if statement easily enough, but it seems like I should be able to execute it depending on success of previous statements.
$ successA=0 ; if ((successA++)) ; then echo success ; else echo failure ; fi
failure
That's because the post-increment returns the original value of successA which is 0, and for (()) 0 means false. You could use the pre-increment operator ((++successA)) or the comma operator ((successA++, 1)).
Last edited by ntubski; 07-16-2014 at 12:34 PM.
Reason: make example repeatable by adding successA=0 ;
$ successA=0 ; if ((successA++)) ; then echo success ; else echo failure ; fi
failure
That's because the post-increment returns the original value of successA which is 0, and for (()) 0 means false. You could use the pre-increment operator ((++successA)) or the comma operator ((successA++, 1)).
Perfect! ((successA++,1) is exactly what I needed. Thank you!
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.