You are not logged in.
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
nothingtodo() {
[[ -z "$@" ]] && echo " there is nothing to do" && exit || continue
}
Blah. I'm stupid.
Offline
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
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
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
Offline
That makes sense. Thanks for the explanation, now my brain feels much better :]
Offline