Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"sh: : unknown operand" in Yocto

Tags:

shell

yocto

The following works in Ubuntu but not Yocto (Poky).

root@system:~/# x='abc'
root@system:~/# y=''
root@system:~/# [[ $(echo $x) != '' ]] && echo true
true
root@system:~/# [[ $(echo $y) != '' ]] && echo true
sh: : unknown operand

In Ubuntu the last line returns nothing (as expected). Any ideas why it's throwing an error in Yocto?

like image 362
geotheory Avatar asked Dec 13 '15 00:12

geotheory


2 Answers

The problem seems to be that $(echo $y) is expanding to an empty string, and then [[ isn't handling it correctly. The solution to that would be to quote the command substitution like

[[ "$(echo "$y")" != '' ]] && echo true

though it's probably better still to use printf than echo so you might do it as

[[ "$(printf '%s' "$y")" != '' ]] && echo true

just in case $y might end up with special characters that can trip up echo or similar

like image 152
Eric Renouf Avatar answered Oct 24 '22 00:10

Eric Renouf


Apparently, busybox ash has a rather simplistic implementation of [[. It is the same as [ except that it expects a ]] instead of ] final argument. This misses the point of why [[ can be useful at all: [[ is supposed to be a keyword with special parsing and using it looks more beautiful and avoids various pitfalls (while adding some of its own). I guess they added it so a few more bash scripts run unmodified on busybox ash.

To avoid confusion, I recommend not using [[ in busybox at all. Use [ and quote all command substitutions and parameter expansions.

like image 43
jilles Avatar answered Oct 23 '22 22:10

jilles