You are not logged in.

#1 2011-12-11 15:55:22

Spyhawk
Member
Registered: 2006-07-07
Posts: 485

[solved] Different coding style result in different behavior.

Hi there,

So I have that function in a bash script:

nothingtodo() {
    if [[ -z "$@" ]]; then
        echo " there is nothing to do" && exit
    fi
}

that is called by the following piece of code:

random_function() {
...
nothingtodo ${array[@]}
}

So if ${array[@]} is empty, the script print "there is nothing to do" and exit, otherwise the script keeps going. And that works well.

Because I'm lazy, I'd like to use the short, "one-liner" coding style which should be strictly equivalent to the above:

nothingtodo() {
   [[ -z "$@" ]] && echo " there is nothing to do" && exit
}

However, using the short coding style result in a different behavior and the script always exit, even though ${array[@]} is not empty ("var" exist):

+ nothingtodo var
+ [[ -z var ]]
+ exit

Furthermore, I've seen that a "workaround" to make the "short" coding style work is to add some code after the nothingtodo() call:

random_function() {
...
nothingtodo ${array[@]}
echo randomstring
}
+ nothingtodo var
+ [[ -z var ]]
+ echo randomstring
randomstring
+ ...

So, there's something I clearly don't get at all. I'm lost, and I don't have a clue about what's (not) happening here. I could go with the "long" coding style without problem, but I'd like to go to bed a bit less stupid tonight. I guess that I somehow probably violated some basic coding rules. Does anyone could explain me what's happening here? Thanks in advance :]

Last edited by Spyhawk (2011-12-11 16:44:28)

Offline

#2 2011-12-11 16:00:26

Spyhawk
Member
Registered: 2006-07-07
Posts: 485

Re: [solved] Different coding style result in different behavior.

nothingtodo() {
    [[ -z "$@" ]] && echo " there is nothing to do" && exit || continue
}

Blah. I'm stupid. big_smile

Offline

#3 2011-12-11 16:02:34

falconindy
Developer
From: New York, USA
Registered: 2009-10-22
Posts: 4,111
Website

Re: [solved] Different coding style result in different behavior.

I can't reproduce what you're seeing, but it seems to me like you wanted to group together the echo and exit, not leave them all as a compound:

[[ -z $var ]] && { echo 'nothing to do'; exit; }

edit: continue makes no sense outside of a loop.....

Last edited by falconindy (2011-12-11 16:15:25)

Offline

#4 2011-12-11 16:33:15

Spyhawk
Member
Registered: 2006-07-07
Posts: 485

Re: [solved] Different coding style result in different behavior.

falconindy wrote:

edit: continue makes no sense outside of a loop.....

You're right. Got a bunch of error, as you might have expected. However, grouping echo and exit doesn't seem to work either. I always though that "[[ condition ]] && do_that_if_condition_is_ok" was sufficient, but it seems that's not always the case.

The really silly thing here is that echoing something *after* the condition makes it work:

doesn't work ->
nothingtodo() {
    [[ -z "$@" ]] && echo " there is nothing to do" && exit
}

works ->
nothingtodo() {
    [[ -z "$@" ]] && echo " there is nothing to do" && exit
    echo test
}

/me becomes crazy.

Edit: Could the fact that nothingtodo() is called from another function the reason of that strange behavior?

Last edited by Spyhawk (2011-12-11 16:37:42)

Offline

#5 2011-12-11 16:40:28

falconindy
Developer
From: New York, USA
Registered: 2009-10-22
Posts: 4,111
Website

Re: [solved] Different coding style result in different behavior.

Yes... the behavior of the other function would come into play.

Offline

#6 2011-12-11 16:44:12

Spyhawk
Member
Registered: 2006-07-07
Posts: 485

Re: [solved] Different coding style result in different behavior.

The following works:

nothingtodo() {
    [[ -z "$@" ]] && echo " there is nothing to do" && exit || return 0
}

Somehow, the function always returned status "1" and the script stopped. Seems that I better be more careful with those "return 0/1" in the future.
Thanks, and "solved".

Offline

#7 2011-12-11 17:11:12

falconindy
Developer
From: New York, USA
Registered: 2009-10-22
Posts: 4,111
Website

Re: [solved] Different coding style result in different behavior.

Yes, it always returned 1 because the evaluation failed. Unless you explicitly call return with a value, bash returns the exit status of the last run command  -- in this case, a failed test.

Offline

#8 2011-12-11 17:19:18

Spyhawk
Member
Registered: 2006-07-07
Posts: 485

Re: [solved] Different coding style result in different behavior.

That makes sense. Thanks for the explanation, now my brain feels much better :]

Offline

Board footer

Powered by FluxBB