Sign up ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free.

I'm trying to run a command ( gerrit query ) in bash and assign that to a variable. I'm using this is a bash script file & I want to handle the case that if the command throws an error( i.e if the gerrit query fails), I should be able to handle the same.

For example:

var=`ssh -p $GERRIT_PORT_NUMBER $GERRIT_SERVER_NAME gerrit query --current-patch-set $PATCHSET_ID`

I do know that I can check the last exit status using $? in bash, but for the above case, the assignment to the variable over-rides the earlier exit status ( i.e the gerrit query failure status) and the above command never fails. It is always true.

Can you let me know if there is a way to handle the exit status of a command even when it is assigned to a variable in bash.

Update:
My assumption was wrong here that an assignment was causing the overriding of the exit status and Charles example and explanation in his answer are correct. The real reason for the exit status being overridden was I was piping the output of the above command to a sed script which was the culprit in overriding the exit status. I found the following which helped me to resolve the issue. http://unix.stackexchange.com/questions/14270/get-exit-status-of-process-thats-piped-to-another/73180#73180 bash: pipe output AND capture exit status

Complete command that I was trying.

var=ssh -p $GERRIT_PORT_NUMBER $GERRIT_SERVER_NAME gerrit query --current-patch-set $PATCHSET_ID | sed 's/message//'

share|improve this question
    
Can you provide a reproducer for this bug that does not involve gerrit (for instance, running /bin/false, or sh -c "exit 3")? –  Charles Duffy Oct 8 '14 at 23:30
    
What exactly do you mean by "Gerrit query fails"? That the query returns zero results or that there's a syntax problem with the query or an internal server error? I'd argue that the former case doesn't constitute an error condition. –  Magnus Bäck Oct 9 '14 at 5:41
    
Hey Magnus, The gerrit query failure ("Gerrit query fails") can be caused by any issue like unable to contact the GERRIT_SERVER, can be an internal server error or even if the GERRIT_PORT or GERRIT_SERVER_NAME is incorrect or if the PATCHSET_ID is invalid or non-existent. –  Jose Oct 10 '14 at 18:45

1 Answer 1

up vote 3 down vote accepted

The assertion made in this question is untrue; assignments do not modify exit status. You can check this yourself:

var=$(false); echo $?

...will correctly emit 1.


That said, if an assignment is done in the context of a local, declare, or similar keyword, this may no longer hold true:

f() { local var=$(false); echo $?; }; f

...will emit 0, and is worked around by separating out the local from the assignment:

f() { local var; var=$(false); echo $?; }; f

...which correctly returns 1.


SSH itself also returns exit status correctly, as you can similarly test yourself:

ssh localhost false; echo $?

...correctly returns 1.


The reasonable conclusion, then, is that gerrit itself is failing to convey a non-successful exit status. This bug should be addressed through gerrit's support mechanisms, rather than as a bash question.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.